python logger日志通过字典配置 随笔

安装PyYAML模块:

pip install PyYAML

随便起个名字 *.yaml  内容如下(此文件中不能有汉字)

version: 1
formatters:
  simple:
#    format: '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    format: '%(asctime)s- %(filename)s -line:%(lineno)d- %(levelname)s -%(process)d -%(message)s'

handlers:
  console:
    class: logging.StreamHandler
    level: DEBUG
    formatter: simple
    stream: ext://sys.stdout
  console_err:
    class: logging.StreamHandler
    level: ERROR
    formatter: simple
    stream: ext://sys.stderr


  file:
    class: logging.handlers.RotatingFileHandler # split by file size
    formatter: simple
    filename: logtest.log
    maxBytes: 1024  # save logfile size
    backupCount: 3 # hold count

  file_lhm:
    class: logging.handlers.RotatingFileHandler # split by file size
    formatter: simple
    filename: www/david/log/lhm/loglhm.log
    maxBytes: 1024  # save logfile size
    backupCount: 3 # hold count

  file_ck:
    class: logging.handlers.TimedRotatingFileHandler # split by file log time
    #  https://blog.csdn.net/shao326/article/details/84617986  scan params means
    formatter: simple
    filename: www/david/log/ck1788/logck.log
    interval: 1 # wait how many 'when'
    backupCount: 5  # hold count
    when: midnight  # when split logfile


  file_tag:
    class: logging.handlers.TimedRotatingFileHandler # split by file log time
    #  https://blog.csdn.net/shao326/article/details/84617986  scan params means
    formatter: simple
    filename: www/david/log/tag/logtag.log
    interval: 1 # wait how many 'when'
    backupCount: 5  # hold count
    when: midnight  # when split logfile

  file_tb_log:
    class: logging.handlers.TimedRotatingFileHandler # split by file log time
    #  https://blog.csdn.net/shao326/article/details/84617986  scan params means
    formatter: simple
    filename: www/david/log/tb_log/logtb.log
    interval: 1 # wait how many 'when'
    backupCount: 5  # hold count
    when: midnight  # when split logfile

  file_zs_change:
    class: logging.handlers.TimedRotatingFileHandler # split by file log time
    #  https://blog.csdn.net/shao326/article/details/84617986  scan params means
    formatter: simple
    filename: www/david/log/zs_change/logzs.log
    interval: 1 # wait how many 'when'
    backupCount: 5  # hold count
    when: midnight  # when split logfile

loggers:
  simpleExample:
    level: DEBUG
    handlers: [file]
    propagate: yes

  lhmExample:
    level: DEBUG
    handlers: [file_lhm]
    propagate: yes

  ck1788Example:
    level: DEBUG
    handlers: [file_ck]
    propagate: yes

  tagExample:
    level: DEBUG
    handlers: [file_tag]
    propagate: yes

  tb_logExample:
    level: DEBUG
    handlers: [file_tb_log]
    propagate: yes

  zs_changeExample:
    level: DEBUG
    handlers: [file_zs_change]
    propagate: yes

  fileExample2:
    level: DEBUG
    handlers: [file]
    propagate: yes

root:
  level: DEBUG
  handlers: [console_err]
# please scan https://www.cnblogs.com/yyds/p/6885182.html

使用

import logging
import logging.config
import yaml
# yaml.warnings({'YAMLLoadWarning': True})

with open('logconf.yml', 'r') as f_conf:
    dict_conf = yaml.safe_load(f_conf) # 使用安全模式不提示waring 影响不大
logging.config.dictConfig(dict_conf)

logger = logging.getLogger('ck1788Example')
logger.debug('debug message')
logger.info('info message')
logger.warning('info message')
# logger.warn('warn message') # 会有提示
logger.error('error message')
# logger.critical('critical message')

当然可以参考这个大神的文章:https://www.cnblogs.com/yyds/p/6901864.html

还有log日志几种配置方式 :https://www.cnblogs.com/yyds/p/6885182.html

你可能感兴趣的:(python)