Created
March 15, 2016 19:39
-
-
Save timster/1befdff48bedeac0fb66 to your computer and use it in GitHub Desktop.
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
# This is a logging configuration that will log to both the console and a file (based on 'handlers'). | |
# The 'loggers' section describes what levels get output: | |
# textloading module will only output DEBUG messages (and above) | |
# requests module will only output WARN messages (and above) | |
# everything else will output INFO messages (and above) | |
LOGGING = { | |
'version': 1, | |
'disable_existing_loggers': True, | |
'loggers': { | |
'': { | |
'handlers': ['file', 'console'], | |
'propagate': True, | |
'level': 'INFO' | |
}, | |
'textloading': { | |
'level': 'DEBUG' | |
}, | |
'requests': { | |
'level': 'WARN' | |
}, | |
}, | |
'formatters': { | |
'standard': { | |
'datefmt': '%Y-%m-%d %H:%M:%S', | |
'format': '%(asctime)s = %(name)s = %(levelname)s = %(message)s' | |
} | |
}, | |
'handlers': { | |
'console': { | |
'formatter': 'standard', | |
'class': 'logging.StreamHandler' | |
}, | |
'file': { | |
'filename': 'log_filename.log', | |
'formatter': 'standard', | |
'mode': 'w', | |
'class': 'logging.FileHandler' | |
} | |
} | |
} | |
# Run this ONE TIME to configure logging: | |
from logging.config import dictConfig | |
dictConfig(LOGGING) | |
# In any module, you can do the following: | |
import logging | |
logger = logging.getLogger(__name__) # to use current module name, OR: | |
logger = logging.getLogger('module.name') # to use a different module name | |
# Then once you do that, you can spit out your logging messages at any level: | |
logger.debug('I am a debug message.') | |
logger.error('This is an error message.') | |
# And there's a special case for exception handling. This will log at ERROR level, but also, | |
# the exception info is passed along with the log message, so you can use %(exc_info)s | |
# in the log formatter in the logging dict | |
try: | |
# do stuff that causes error | |
except Exception: | |
logger.exception('I am an error message') | |
# Quick exmaple. Let's say I have the following in textloading/taxiways/extract.py: | |
import logging | |
logger = logging.getLogger(__name__) | |
logger.info('starting process') | |
# That would send out a log message that looks like this: | |
# 2016-03-15 12:12:52 = textloading.taxiways.extract = INFO = starting process |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment