-
Notifications
You must be signed in to change notification settings - Fork 4
/
log.py
47 lines (34 loc) · 1.29 KB
/
log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# coding: utf-8
# This file is part of https://github.com/marcus67/gitsynchista
import logging
import logging.handlers
import logging.config
import sys
import os
import shutil
import json
import log_stat
LOGGING_FILENAME = "etc/log_config.json"
LOGGING_TEMPLATE_FILENAME = "etc/log_config_template.json"
def open_logging(module_name, reload = False):
global logger
if reload or not log_stat.get_log_started():
log_stat.set_log_started(True)
copy_template = os.path.exists(LOGGING_TEMPLATE_FILENAME) and not os.path.exists(LOGGING_FILENAME)
if copy_template:
shutil.copyfile(LOGGING_TEMPLATE_FILENAME, LOGGING_FILENAME)
logging_config_json_file = open(LOGGING_FILENAME)
parsed_logging_data = json.load(logging_config_json_file)
logging.config.dictConfig(parsed_logging_data)
logger = logging.getLogger('log')
if copy_template:
logger.info("Copied logging configuration template %s" % LOGGING_TEMPLATE_FILENAME)
logger.info("Loaded logging configuration from %s" % LOGGING_FILENAME)
logger.info("Starting logging")
return logging.getLogger(module_name)
def test():
logger = open_logging('test', True)
logger.error("This is an error")
logger.warning("This is a warning")
if __name__ == '__main__':
test()