Skip to content

Instantly share code, notes, and snippets.

@earthday
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earthday/19adfd910406bbc0403d to your computer and use it in GitHub Desktop.
Save earthday/19adfd910406bbc0403d to your computer and use it in GitHub Desktop.
Python logging example
import os
import logging
import codecs
import yaml
# config the logging
current_folder = os.path.dirname(os.path.realpath(__file__))
logging_config_filename = os.path.join(current_folder, 'python_logging_example.yaml')
logging.config.dictConfig(yaml.load(codecs.open(logging_config_filename, 'r', 'utf-8')))
logger = logging.getLogger('commonLog')
# example
logger.info("This is a info log.")
logger.error("This is a error log.")
version: 1
formatters:
simple:
format: '%(levelname)-8s %(name)-15s %(message)s'
datefmt: '%Y-%m-%d %H:%M:%S'
normal:
format: '%(asctime)s|%(levelname)-8s|%(name)-15s|%(message)s'
datefmt: '%Y-%m-%d %H:%M:%S'
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
file:
class : logging.handlers.RotatingFileHandler
formatter: normal
filename: /var/log/logname.log
level: DEBUG
maxBytes: 10485760 # 10MB
backupCount: 10
loggers:
commonLog:
level: DEBUG
handlers: [console, file]
propagate: 1
root:
level: DEBUG
handlers: [console]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment