LOG_DIR= os.path.join(BASE_DIR,"logs") host_ip="localhost"JSON_LOGGING_FORMAT=json.dumps({"ip":"%(ip)s","app":"%(app)s","level":"%(levelname)s","trace":"%(stack_msg)s","filepath":"%(pathname)s","line_number":"%(lineno)s","time":"%(asctime)s","message":"%(messa...
importloggingimportjsonimporttimeclassJsonFormatter(logging.Formatter):defformat(self,record):log_obj={'time':self.formatTime(record),'level':record.levelname,'message':record.getMessage(),'name':record.name,}returnjson.dumps(log_obj)# 创建一个 loggerlogger=logging.getLogger('my_logger')logger.s...
logging.basicConfig(level=logging.DEBUG, format=LOG_FORMAT, datefmt=DATE_FORMAT, handlers=**) logging.basicConfig的参数: filename:设置日志输出的文件,默认输出到控制台。 filemode:设置打开日志文件的方式,默认为“a”,即追加。 format:设置日志输出的字符串格式, 具体的格式有如下几种: %(name)s:日志记录...
logger.info("Start print log") logger.debug("Do something") logger.warning("Something maybe fail.") logger.info("Finish") 4.使用logging配置 4.1 通过JSON加载日志配置 logging.json: { "version":1, "disable_existing_loggers":false, "formatters":{ "simple":{ "format":"%(asctime)s - %(n...
json_formatter . JSONFormatter [ formatter_json_pretty ] format = pretty class = format . json_formatter . JSONFormatter 如下的一段异常代码: fileConfig ( 'log_conf.ini' ) log = logging . getLogger ( __name__ ) try : a = 1
Formatter 就是对Record 专门格式化的对象,它有一个format 方法,我们实现这个方法就能 做到不同的输出,我的需求是做json 格式的log 其实关键就在写一个Formatter 就好了 classFormatter(object):converter=time.localtimedef__init__(self,fmt=None,datefmt=None):iffmt:self._fmt=fmtelse:self._fmt="%(message...
args=('log/test_case_log.log','a')[handler_hand03]class=handlers.RotatingFileHandler level=INFOformatter=form01 args=('log/test_case_log.log','a',10*1024*1024,3)[formatters]keys=form01,form02[formatter_form01]format=%(asctime)s-%(filename)s-[line:%(lineno)d]-%(levelname)s-[Log...
importlogging.configimportstructlogfromstructlogimportconfigure,processors,stdlib,threadlocal# logging.config.fileConfig('logconfig.ini')logging.config.dictConfig({'version':1,'disable_existing_loggers':False,'formatters':{'json':{'format':'%(message)s %(lineno)d %(filename)s','class':'pythonjs...
{'level':'ERROR','formatter':'json','class':'logging.handlers.TimedRotatingFileHandler','filename':'{0}/{1}_error.log'.format(log_path,service_name),'when':"d",'interval':1,'encoding':'utf8','backupCount':30,'filters':['error_filter']}},# 记录器'loggers':{'full_logger':{'...
StreamHandler() formatter = jsonlogger.JsonFormatter() logHandler.setFormatter(formatter) logger.addHandler(logHandler) The fmt parser can also be overidden if you want to use an alternate from the default. class CustomJsonFormatter(jsonlogger.JsonFormatter): def parse(self): return eval(self._...