Python:使用json配置文件设置logger的配置(logging.config)
2018-11-18 10:56
369 查看
[code]#!/usr/bin/env python # coding:UTF-8 """ @version: python3.x @author:曹新健 @contact: 617349013@qq.com @software: PyCharm @file: 使用配置文件设置logger的配置(logging.config).py @time: 2018/11/16 13:26 """ import os,logging,sys,time,json import logging.config def singleton(cls): instances = {} def _singleton(*args,**kwargs): if cls not in instances: instances[cls] = cls(*args,**kwargs) return instances[cls] return _singleton @singleton class Logger(): def __init__(self,logfile=None): self.logger = logging.getLogger("simple_example") with open("logconf.json","r") as config: LOGGING_CONFIG = json.load(config) logging.config.dictConfig(LOGGING_CONFIG) if __name__ == "__main__": lg = Logger() lg.logger.warning("aaa1") lg.logger.error("bbb2") lg.logger.debug("ccc3")
[code]logconf.json { "version":1, "handlers":{ "console_streamHandler":{ "class":"logging.StreamHandler", "level":"DEBUG", "formatter":"myFormatter", "stream":"ext://sys.stdout" }, "console1_fileHandler":{ "class":"logging.handlers.RotatingFileHandler", "level":"DEBUG", "formatter":"myFormatter", "filename":"log.log", "mode": "w+", "maxBytes": 524288000, "backupCount": 20, "encoding":"utf8" } }, "formatters":{ "myFormatter":{ "format":"%(asctime)s %(name)s %(levelname)s %(filename)s %(lineno)d %(thread)d %(threadName)s %(process)d %(message)s" } }, "loggers":{ "simple_example":{ "level":"DEBUG", "handlers":["console_streamHandler","console1_fileHandler"] } } }
阅读更多
相关文章推荐
- Python配置文件解析模块ConfigParser使用实例
- Python logging模块使用配置文件记录日志
- 将所有程序设置XML集中到一个单独XML配置文件的方法:使用appSettings元素的configSource元素
- Python使用自带的ConfigParser模块读写ini配置文件
- 使用python的ConfigParser模块方便的读取配置文件
- python程序调试与测试使用JSON文件进行日志配置
- 【Python】配置文件解析模块ConfigParser使用实例
- Webpack 4.XXX 配置文件webpack.config.js和package.json【使用方法总结】
- python的ConfigParser读取设置配置文件
- Python使用自带的ConfigParser模块读写ini配置文件
- Python模块之Logging(五)——在配置文件中为Logger配置多个handler
- Python中logging.config配置文件解读
- 如何使用svconfigeditor.exe编辑WCF Service配置文件--配置Diagnostics进行WCF Service的log和Trace(三)
- ASP.NET:使用web.config文件进行配置
- 关于Web.config文件对个性化设置Profile的配置
- 使用Python的logging.config
- ASP.Net2.0使用Log4Net(二)使用单独的配置文件,不放在web.config
- struts 使用多个配置文件 struts-config.xml
- Web.Config 文件中的设置配置网页身份验证
- .net中使用Flash上传文件时的Web.Config的配置注意,精