Skip to content

Instantly share code, notes, and snippets.

@gregglind
Created April 25, 2011 17:34
Show Gist options
  • Save gregglind/940863 to your computer and use it in GitHub Desktop.
Save gregglind/940863 to your computer and use it in GitHub Desktop.
centralized logging
"""use get_logger:
from this_logging import get_logger; logger = get_logger(__name__)
# then you can:
logger.debug("foo")
# and if you want to change the logging level:
import logging
logger.parent.setLevel(logging.DEBUG)
"""
from os.path import dirname, join, exists
import logging
import logging.config
logging_conf = join(dirname(__file__), 'logging.conf')
if exists(logging_conf):
logging.config.fileConfig(logging_conf)
else:
import sys
# i'd prefer funcName to lineno, but not available until 2.5
fmt = "%(asctime)s,%(msecs)03d %(levelname)-5.5s [%(name)s:%(lineno)d] %(message)s"
logging.basicConfig(stream=sys.stderr, level=logging.WARNING, format=fmt)
get_logger = logging.getLogger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment