comparison util/Logger.py @ 29:7e8a8b732db3 draft

planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
author yating-l
date Wed, 16 May 2018 18:04:20 -0400
parents
children
comparison
equal deleted inserted replaced
28:6aa28a85cc38 29:7e8a8b732db3
1 import os
2 import sys
3 import json
4 import logging
5 import logging.config
6
7 #from util.Filters import TraceBackFormatter
8
9 class Logger(object):
10 def __init__(self, tool_directory, debug="False", extra_files_path=None):
11 self.tool_directory = tool_directory
12 self.default_level = logging.INFO
13 self.debug = debug
14 self.extra_files_path = extra_files_path
15
16 def setup_logging(self):
17 """Setup logging configuration
18 reference: https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
19 """
20 config_path = os.path.join(self.tool_directory, 'logging.json')
21 default_level=logging.INFO
22 if self.debug.lower() == "true":
23 default_level=logging.DEBUG
24 if os.path.exists(config_path):
25 with open(config_path, 'rt') as f:
26 config = json.load(f)
27 config["handlers"]["console"]["level"] = default_level
28 if self.extra_files_path:
29 for i in config["handlers"]:
30 if "filename" in config["handlers"][i]:
31 config["handlers"][i]["filename"] = os.path.join(self.extra_files_path, config["handlers"][i]["filename"])
32 logging.config.dictConfig(config)
33 else:
34 logging.warn("Extra files path is not set. The log files will exist at current working directory instead of final output folder")
35 else:
36 logging.basicConfig(level=default_level)
37 logging.warn("Cannot find logging configuration file!\n")
38