Created
June 20, 2019 15:14
-
-
Save DaveIW2034/54ab5f27b5e12c9fc276842cdd492dbd to your computer and use it in GitHub Desktop.
动态生成 logger位置
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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