Skip to content

Instantly share code, notes, and snippets.

@pmav99
Last active February 11, 2018 21:49
Show Gist options
  • Select an option

  • Save pmav99/58285e6e21449e6c9bae21b6a56af6a1 to your computer and use it in GitHub Desktop.

Select an option

Save pmav99/58285e6e21449e6c9bae21b6a56af6a1 to your computer and use it in GitHub Desktop.
yaml based configuration for python logging
#!/usr/bin/env python
# module: <package>/__init__.py
# author: Panagiotis Mavrogiorgos <pmav99,gmail>
import logging.config
import pathlib
import yaml
try:
from yaml import CLoader as YLoader, CDumper as YDumper
except ImportError:
from yaml import YLoader, YDumper
import munch
# paths
ROOT_DIR = pathlib.Path(__file__).parent.parent.resolve()
CONFIG_FILE = (ROOT_DIR / "config.yaml").resolve()
_CONFIG = None
def apply_types(config):
""" This is a hack, but having e.g. paths as Pathlib objects is convenient... :P """
for key, value in config.directories.items():
config.directories[key] = ROOT_DIR / value
for key, value in config.files.items():
config.files[key] = ROOT_DIR / value
return config
def load_config(config_file):
with open(config_file, "rb") as fd:
config = yaml.load(fd, Loader=YLoader)
return config
def get_config():
""" Return the app's configuration. """
global _CONFIG
if _CONFIG is None:
config = munch.munchify(load_config(CONFIG_FILE))
config = apply_types(config)
_CONFIG = config
return _CONFIG
def setup_logging():
config = get_config()
logging.config.dictConfig(config.logging)
setup_logging()
__all__ = [
"get_config",
"setup_logging",
]
no_threads: 6
no_processes: 3
requests_per_second: 5
directories:
cache: ./cache
data: ./data
files:
file1: ./file1.txt
file2: ~/home/user/file2
logging:
version: 1
disable_existing_loggers: true
formatters:
brief:
format: "%(levelname)-8s; %(name)-35s; %(message)s"
single_line:
format: "%(asctime)s; %(levelname)-8s; %(name)-35s; %(funcName)-20s;%(lineno)4d: %(message)s"
multi_process:
format: "%(asctime)s; %(levelname)-8s; [%(thread;)s]; %(name)-35s; %(funcName)-20s;%(lineno)-4d: %(message)s"
multi_thread:
format: "%(asctime)s; %(levelname)-8s; [%(process)d]; %(name)-35s; %(funcName)-20s;%(lineno)-4d: %(message)s"
verbose:
format: "%(asctime)s; %(levelname)-8s; [%(process)d - %(thread)s]; %(name)-35s; %(funcName)-20s;%(lineno)4d: %(message)s"
multi_line:
format: "Level: %(levelname)s\nTime: %(asctime)s\nProcess: %(process)d\nThread: %(thread)s\nLogger: %(name)s\nPath: %(module)s:%(lineno)d\nFunction :%(funcName)s\nMessage: %(message)s\n"
handlers:
console:
level: DEBUG
class: logging.StreamHandler
formatter: verbose
stream : ext://sys.stdout
errors:
level: WARNING
class: logging.handlers.WatchedFileHandler
formatter: verbose
filename: /tmp/edgar_errors.log
mode: a
encoding: utf-8
smtp:
level: ERROR
class: logging.handlers.SMTPHandler
formatter: multi_line
mailhost: [127.0.0.1, 25]
fromaddr: [email protected]
toaddrs: [[email protected]]
subject: Something went wrong
loggers:
custom_logger:
level: DEBUG
requests:
level: WARNING
root:
level: DEBUG
handlers:
- console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment