Last active
April 12, 2016 09:03
-
-
Save tuxedocat/eb0accd23fc6e1d1ee05a7843d4160de to your computer and use it in GitHub Desktop.
Logging test
This file contains 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
# 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() |
This file contains 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
%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