52 lines
1.1 KiB
Python
52 lines
1.1 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
# \file %filename%.py
|
|
# \brief TODO
|
|
# \author Florent Guiotte <florent.guiotte@gmail.com>
|
|
# \version 0.1
|
|
# \date 24 avril 2018
|
|
#
|
|
# from https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
|
|
|
|
import os
|
|
import logging.config
|
|
from pathlib import Path
|
|
|
|
import yaml
|
|
|
|
def setup_logging(
|
|
default_path='logging.yaml',
|
|
default_level=logging.WARN,
|
|
env_key='LOG_CFG'
|
|
):
|
|
"""Setup logging configuration
|
|
|
|
"""
|
|
path = default_path
|
|
value = os.getenv(env_key, None)
|
|
if value:
|
|
path = value
|
|
if os.path.exists(path):
|
|
with open(path, 'rt') as f:
|
|
config = yaml.safe_load(f.read())
|
|
makedirs(config)
|
|
logging.config.dictConfig(config)
|
|
else:
|
|
logging.basicConfig(level=default_level)
|
|
|
|
|
|
def makedirs(dic):
|
|
files = finddirs(dic)
|
|
for f in files:
|
|
d = Path(*f.parts[:-1])
|
|
d.mkdir(parents=True, exist_ok=True)
|
|
|
|
def finddirs(dic, key='filename'):
|
|
r = list()
|
|
value = dic.get(key)
|
|
if value : r.append(Path(value))
|
|
for k, v in dic.items():
|
|
if isinstance(v, dict):
|
|
r.extend(finddirs(v))
|
|
return r
|