Created
December 22, 2018 12:37
-
-
Save changyeon/d90b3895a2a65caff914ced5a863226a to your computer and use it in GitHub Desktop.
Python logging example
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
#!/usr/bin/env python3 | |
import logging | |
def setup_logger(name, filename=None): | |
logger = logging.getLogger(name) | |
logger.setLevel(logging.DEBUG) | |
fmt_string = ('[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >' | |
' %(message)s') | |
fmt = logging.Formatter(fmt_string) | |
sh = logging.StreamHandler() | |
sh.setFormatter(fmt) | |
logger.addHandler(sh) | |
if filename is not None: | |
fh = logging.FileHandler(filename) | |
fh.setFormatter(fmt) | |
logger.addHandler(fh) | |
return logger | |
if __name__ == "__main__": | |
logger = setup_logger('test', filename='test.log') | |
logger.error('test') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment