Skip to content

Instantly share code, notes, and snippets.

@tuxedocat
Last active April 12, 2016 09:03
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 tuxedocat/eb0accd23fc6e1d1ee05a7843d4160de to your computer and use it in GitHub Desktop.
Save tuxedocat/eb0accd23fc6e1d1ee05a7843d4160de to your computer and use it in GitHub Desktop.
Logging test
# coding: utf-8
import logging
import logging.config
import yaml
import os
def setup_logging(config_path='../config.yaml', default_level=logging.DEBUG):
"""Setup logging configuration
"""
logger = logging.getLogger(__name__)
try:
with open(config_path, 'rt') as f:
config = yaml.safe_load(f.read())
logging.config.dictConfig(config.get('logging'))
logger.debug('Loaded config.yaml')
except (IOError, OSError, KeyError):
logging.basicConfig(level=default_level)
logger.debug('Error occurred when loading config.yaml')
finally:
logger.debug('Logger setting is initiated.')
setup_logging()
%YAML 1.2
---
# ---------------------------------
# Config for App.
# ---------------------------------
app:
foo: "something"
bar: [1, 2]
# ---------------------------------
# Config for logging
# ---------------------------------
# See http://docs.python.org/3.5/library/logging.config.html#configuration-dictionary-schema
logging:
version: 1
disable_existing_loggers: False
formatters:
simple:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
handlers:
console:
class: logging.StreamHandler
level: DEBUG
formatter: simple
stream: ext://sys.stdout
debug_file_handler:
class: logging.handlers.RotatingFileHandler
level: DEBUG
formatter: simple
filename: debug.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
error_file_handler:
class: logging.handlers.RotatingFileHandler
level: WARNING
formatter: simple
filename: errors.log
maxBytes: 10485760 # 10MB
backupCount: 20
encoding: utf8
loggers:
mymodule:
level: DEBUG
handlers: [console, debug_file_handler, error_file_handler]
propagate: no
root:
level: DEBUG
handlers: [console, debug_file_handler, error_file_handler]
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment