Skip to content

Commit da95f78

Browse files
committed
Added logging configuration
1 parent 6eb1025 commit da95f78

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

module/config/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
@created on: 10/03/17,
3+
@author: Prathyush SP,
4+
@version: v0.0.1
5+
6+
Description:
7+
8+
Sphinx Documentation Status:
9+
10+
..todo::
11+
12+
"""

module/config/module_logging.yaml

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
version: 1
2+
disable_existing_loggers: true
3+
4+
5+
filters:
6+
info_filter:
7+
() : module.logging_config_manager.InfoFilter
8+
error_filter:
9+
() : module.logging_config_manager.ErrorFilter
10+
11+
formatters:
12+
standard:
13+
format: "%(asctime)s - %(levelname)s - %(name)s(%(lineno)d) - %(message)s"
14+
error:
15+
format: "%(asctime)s - %(levelname)s <PID %(process)d:%(processName)s> %(name)s.%(funcName)s(): %(message)s"
16+
17+
handlers:
18+
console:
19+
class: logging.StreamHandler
20+
level: DEBUG
21+
formatter: standard
22+
filters: [info_filter]
23+
stream: ext://sys.stdout
24+
25+
error_console:
26+
class: logging.StreamHandler
27+
level: ERROR
28+
formatter: error
29+
filters: [error_filter]
30+
stream: ext://sys.stderr
31+
32+
33+
info_file_handler:
34+
class: logging.handlers.RotatingFileHandler
35+
level: INFO
36+
formatter: standard
37+
filename: /tmp/info.log
38+
maxBytes: 10485760 # 10MB
39+
backupCount: 20
40+
encoding: utf8
41+
42+
error_file_handler:
43+
class: logging.handlers.RotatingFileHandler
44+
level: ERROR
45+
formatter: error
46+
filename: /tmp/errors.log
47+
maxBytes: 10485760 # 10MB
48+
backupCount: 20
49+
encoding: utf8
50+
51+
debug_file_handler:
52+
class: logging.handlers.RotatingFileHandler
53+
level: DEBUG
54+
formatter: standard
55+
filename: /tmp/debug.log
56+
maxBytes: 10485760 # 10MB
57+
backupCount: 20
58+
encoding: utf8
59+
60+
critical_file_handler:
61+
class: logging.handlers.RotatingFileHandler
62+
level: CRITICAL
63+
formatter: standard
64+
filename: /tmp/critical.log
65+
maxBytes: 10485760 # 10MB
66+
backupCount: 20
67+
encoding: utf8
68+
69+
warn_file_handler:
70+
class: logging.handlers.RotatingFileHandler
71+
level: WARN
72+
formatter: standard
73+
filename: /tmp/warn.log
74+
maxBytes: 10485760 # 10MB
75+
backupCount: 20
76+
encoding: utf8
77+
78+
root:
79+
level: NOTSET
80+
handlers: [console, error_console]
81+
propogate: no
82+
83+
loggers:
84+
module:
85+
level: INFO
86+
handlers: [info_file_handler, error_file_handler, critical_file_handler, debug_file_handler, warn_file_handler]
87+
propogate: no
88+

module/logging_config_manager.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
| **@created on:** 09/08/17,
4+
| **@author:** prathyushsp,
5+
| **@version:** v0.0.1
6+
|
7+
| **Description:**
8+
|
9+
|
10+
| **Sphinx Documentation Status:** Complete
11+
|
12+
..todo::
13+
--
14+
"""
15+
import logging.config
16+
import os
17+
import yaml
18+
import sys
19+
20+
21+
class ErrorFilter(logging.Filter):
22+
"""
23+
| **@author:** Prathyush SP
24+
| Error Filter
25+
"""
26+
27+
def filter(self, record):
28+
"""
29+
| Filter Method
30+
:param record: Record Object
31+
:return: Boolean
32+
"""
33+
return record.levelno in [logging.ERROR, logging.CRITICAL]
34+
35+
36+
class InfoFilter(logging.Filter):
37+
"""
38+
| **@author:** Prathyush SP
39+
| Info Filter
40+
"""
41+
42+
def filter(self, record):
43+
"""
44+
| Filter Method
45+
:param record: Record Object
46+
:return: Boolean
47+
"""
48+
return record.levelno in [logging.INFO, logging.DEBUG, logging.WARN]
49+
50+
51+
def setup_logging(default_path='logging.yaml', default_level=logging.INFO, env_key='LOG_CFG'):
52+
"""
53+
| **@author:** Prathyush SP
54+
| Logging Setup
55+
"""
56+
path = default_path
57+
value = os.getenv(env_key, None)
58+
if value:
59+
path = value
60+
if os.path.exists(path):
61+
with open(path, 'rt') as f:
62+
try:
63+
config = yaml.safe_load(f.read())
64+
logging.config.dictConfig(config)
65+
# todo: Prathyush SP - Enable when coloredlogs package supports filters (https://github.com/xolox/python-coloredlogs/issues/32)
66+
# coloredlogs.install(fmt=config['formatters']['standard']['format'], stream=sys.stdout,
67+
# level=logging.INFO, logger=logging.getLogger().addFilter(InfoFilter()))
68+
except Exception as e:
69+
print('Error in Logging Configuration. Using default configs', e)
70+
logging.basicConfig(level=default_level, stream=sys.stdout)
71+
# todo: Prathyush SP - Enable when coloredlogs package supports filters (https://github.com/xolox/python-coloredlogs/issues/32)
72+
# coloredlogs.install(level=default_level, stream=sys.stdout)
73+
else:
74+
logging.basicConfig(level=default_level, stream=sys.stdout)
75+
# todo: Prathyush SP - Enable when coloredlogs package supports filters (https://github.com/xolox/python-coloredlogs/issues/32)
76+
# coloredlogs.install(level=default_level, stream=sys.stdout)
77+
print('Failed to load configuration file. Using default configs')

0 commit comments

Comments
 (0)