Skip to content

Instantly share code, notes, and snippets.

@DaveIW2034
Created June 20, 2019 15:14
Show Gist options
  • Save DaveIW2034/54ab5f27b5e12c9fc276842cdd492dbd to your computer and use it in GitHub Desktop.
Save DaveIW2034/54ab5f27b5e12c9fc276842cdd492dbd to your computer and use it in GitHub Desktop.
动态生成 logger位置
import logging
import logging.config
def configure_logger(name, log_path):
logging.config.dictConfig({
'version': 1,
'formatters': {
'default': {'format': '%(asctime)s - %(levelname)s - %(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S'}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'default',
'stream': 'ext://sys.stdout'
},
'file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'default',
'filename': log_path,
'maxBytes': 1024,
'backupCount': 3
}
},
'loggers': {
'default': {
'level': 'DEBUG',
'handlers': ['console', 'file']
}
},
'disable_existing_loggers': False
})
return logging.getLogger(name)
alog = configure_logger('default', 'log13.txt')
alog.debug('debug message!')
alog.info('info message!')
alog.error('error message')
alog.critical('critical message')
alog.warning('warning message')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment