Mercurial > repos > yating-l > gonramp_apollo_tools
comparison util/Logger.py @ 0:ce4f91831680 draft default tip
planemo upload for repository https://github.com/Yating-L/suite_gonramp_apollo.git commit 5367a00befb467f162d1870edb91f9face72e894
author | yating-l |
---|---|
date | Fri, 16 Feb 2018 10:57:13 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:ce4f91831680 |
---|---|
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 """ | |
18 Setup logging configuration | |
19 reference: https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/ | |
20 """ | |
21 config_path = os.path.join(self.tool_directory, 'logging.json') | |
22 default_level=logging.INFO | |
23 if self.debug.lower() == "true": | |
24 default_level=logging.DEBUG | |
25 if os.path.exists(config_path): | |
26 with open(config_path, 'rt') as f: | |
27 config = json.load(f) | |
28 config["handlers"]["console"]["level"] = default_level | |
29 if self.extra_files_path: | |
30 for i in config["handlers"]: | |
31 if "filename" in config["handlers"][i]: | |
32 config["handlers"][i]["filename"] = os.path.join(self.extra_files_path, config["handlers"][i]["filename"]) | |
33 logging.config.dictConfig(config) | |
34 else: | |
35 logging.config.dictConfig(config) | |
36 logging.warn("Extra files path is not set. The log files will exist at current working directory instead of final output folder") | |
37 else: | |
38 logging.basicConfig(level=default_level) | |
39 logging.warn("Cannot find logging configuration file!\n") | |
40 |