Created
August 3, 2020 08:21
-
-
Save vlee-harmonicinc/599df60ec861db8bd244c5b090a27366 to your computer and use it in GitHub Desktop.
Usually, I write INFO log to file and warn to stdout. Since those option is exclusive in basicConfig function, need to create two handler separately.
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
def setup_logger(log_file): | |
log_format = '[%(asctime)s][%(process)5d][%(levelname)s][%(filename)s L%(lineno)d]%(message)s' | |
# write to file | |
file_handler = logging.FileHandler(log_file, 'w') | |
file_handler.setLevel(logging.INFO) | |
# stream to stdout | |
stdout_handler = logging.StreamHandler(sys.stdout) | |
stdout_handler.setLevel(logging.WARN) | |
stdout_handler.setFormatter(logging.Formatter(fmt=log_format)) | |
logging.basicConfig(level=logging.INFO, format=log_format, handlers=[file_handler, stdout_handler]) | |
if __name__ == '__main__': | |
config = load_config() | |
setup_logger(config['log']) | |
logger = logging.getLogger('main') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment