annotate util/Logger.py @ 83:0bc486356e2a draft

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