Created
October 6, 2014 01:16
-
-
Save llazzaro/959e65492b9beaa20738 to your computer and use it in GitHub Desktop.
python logger initialize code
This file contains hidden or 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 | |
def init_logging(): | |
logger = logging.getLogger('logger_name') | |
logger.setLevel(logging.INFO) | |
# create file handler which logs even debug messages | |
fh = logging.FileHandler('logger_name.log') | |
fh.setLevel(logging.INFO) | |
# create console handler with a higher log level | |
ch = logging.StreamHandler() | |
ch.setLevel(logging.INFO) | |
# create formatter and add it to the handlers | |
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') | |
fh.setFormatter(formatter) | |
ch.setFormatter(formatter) | |
# add the handlers to the logger | |
logger.addHandler(fh) | |
logger.addHandler(ch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment