-
-
Save mitchellbusby/31756213209ac025397b5809c24233fb to your computer and use it in GitHub Desktop.
Getting setup with proper python logging
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
# ---------------------------------------------------------------------------- # | |
# Standard setup stuff. In general you will want to alter the formatters | |
# | |
[formatters] | |
keys: detailed,simple | |
[handlers] | |
keys: console | |
[formatter_simple] | |
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s | |
#format: %(name)s:%(levelname)s: %(message)s | |
[formatter_detailed] | |
format: %(name)s:%(levelname)s %(module)s:%(lineno)d: %(message)s | |
[handler_console] | |
class: StreamHandler | |
args: [] | |
formatter: simple | |
# ---------------------------------------------------------------------------- # | |
# Custom loggers - Add your module's logger below these. | |
# | |
# As you reuse this example from project to project, the below is what you will | |
# change. (Everything above can likely stay the same) | |
# | |
# | |
# FIRST add your abbreviated name to the next section. This has to align | |
# with your logger's name. So if you add 'diablo' to this list, your logger | |
# section needs to look like [logger_diablo] | |
# | |
[loggers] | |
keys: root,diablo,main,network | |
# | |
# Next add a new logging section below these. The qualname is what your | |
# python module will use to get its logger via | |
# diablo = logging.getLogger('diablo') | |
# | |
# The default for any loggers that are not specified below | |
[logger_root] | |
level: DEBUG | |
handlers: console | |
# Root diablo logger | |
[logger_diablo] | |
level: DEBUG | |
qualname: diablo | |
handlers: console | |
# This will prevent double (or worse) logging | |
propagate: 0 | |
# ---------------------------------------------------------------------------- # | |
# As you add main modules to your application, you need to add loggers for them. | |
# That means adding them below, and to the list above. | |
# | |
# In general, you get one logger per python file (module). So the below is for | |
# main.py | |
# | |
[logger_main] | |
level: DEBUG | |
qualname: diablo.main | |
handlers: console | |
# This will prevent double (or worse) logging | |
propagate: 0 | |
# Another diablo module 'network' | |
[logger_network] | |
level: DEBUG | |
qualname: diablo.network | |
handlers: console | |
# This will prevent double (or worse) logging | |
propagate: 0 |
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
#!/usr/bin/env python | |
''' An example using our above logging.conf ''' | |
import logging | |
import logging.config | |
# !!!NOTE!!! Only ever need to do this once, in one file per project, | |
# and it MUST be done prior to anything using the logging module. | |
# Therefore you must import other app level modules after this. | |
logging.config.fileConfig('logging.conf') | |
import diablo.network | |
# Instantiate our logger for use only in this file | |
_log = logging.getLogger('diablo.main') | |
def some_function(): | |
# Print a log statement | |
_log.debug('Running function "some_function"') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment