def get_logger(name): config = Config() if config.disable_3rd_party_logs: dictConfig({ 'version': 1, 'disable_existing_loggers': True, }) level = os.getenv("LOGGING_LEVEL", "DEBUG") message_format = "[%(asctime)s] [%(levelname)s] %(message)s" timestamp_format = "%Y-%m-%dT%H:%M:%S" formatter = Formatter(fmt=message_format, datefmt=timestamp_format) formatter.converter = time.gmtime handler = StreamHandler() handler.setFormatter(formatter) logger = Logger(name, level=level) logger.addHandler(handler) return logger
def create_logger(): log_level = os.getenv("LOG_LEVEL", logging.INFO) log_format = "[%(asctime)s] [%(levelname)s] %(message)s" log_timestamp_format = "%Y-%m-%dT%H:%M:%S%z" formatter = Formatter(log_format, log_timestamp_format) formatter.converter = time.gmtime handler = StreamHandler() handler.setFormatter(formatter) default_logger = logging.getLogger() default_logger.setLevel(log_level) default_logger.addHandler(handler) return default_logger
def get_logger(name): """ Configuration logger for sonar """ level = os.getenv("LOGGING_LEVEL", "DEBUG") message_format = "[%(asctime)s] [%(levelname)s] %(message)s" timestamp_format = "%Y-%m-%dT%H:%M:%SZ" formatter = Formatter(fmt=message_format, datefmt=timestamp_format) formatter.converter = time.gmtime handler = StreamHandler() handler.setFormatter(formatter) logger = Logger(name, level=level) logger.addHandler(handler) return logger
def set_logger(modname): BASE_DIR = os.path.realpath(os.path.dirname(__file__)) LOG_DIR = os.path.join(BASE_DIR, 'logs') # ログファイルディレクトリ # ログファイルディレクトリがなければ作成する if not os.path.exists(LOG_DIR): os.mkdir(LOG_DIR) ################# # logger 設定 logger = getLogger(modname) logger.setLevel(DEBUG) log_fmt = Formatter('%(asctime)s %(name)s %(lineno)d [%(levelname)s][%(module)s][%(funcName)s] %(message)s', datefmt='%m-%d %H:%M:%S.%s,%03d') def customTime(*args): utc_dt = utc.localize(datetime.datetime.utcnow()) my_tz = timezone("Asia/Tokyo") converted = utc_dt.astimezone(my_tz) return converted.timetuple() log_fmt.converter = customTime strm_handler = StreamHandler() strm_handler.setLevel(DEBUG) strm_handler.setFormatter(log_fmt) logger.addHandler(strm_handler) str_hour = str(datetime.datetime.now().hour) if datetime.datetime.now().hour >= 10 \ else '0' + str(datetime.datetime.now().hour) str_minute = str(datetime.datetime.now().minute) if datetime.datetime.now().minute >= 10\ else '0' + str(datetime.datetime.now().minute) rotate_file_handler = RotatingFileHandler( filename=os.path.join(LOG_DIR, '{}_{}{}_log.log'.format(str(datetime.datetime.now().date()) ,str_hour ,str_minute)), maxBytes=1024 * 1024 * 5, backupCount=10 ) rotate_file_handler.setLevel(INFO) rotate_file_handler.setFormatter(log_fmt) logger.addHandler(rotate_file_handler) return logger
def __init__(self, level_name): """ ログフォーマットの指定をします。 :param level_name: ログレベル """ level = getLevelName(level_name) self.logger = getLogger() self.logger.setLevel(level) formatter = Formatter( '%(levelname)s %(error_code)s %(asctime)s %(message)s', '%Y-%m-%dT%H:%M:%SZ') formatter.converter = time.gmtime if self.logger.handlers: for handler in self.logger.handlers: handler.setFormatter(formatter) else: handler = StreamHandler() handler.setLevel(level) handler.setFormatter(formatter) self.logger.addHandler(handler)