Mercurial > repos > rmarenco > hubarchivecreator
annotate util/Logger.py @ 30:e7c4be523cb7 draft
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 2f1b3cc8c9cb463e460f7f79efe99ed472700ecf
author | yating-l |
---|---|
date | Tue, 19 Jun 2018 13:02:32 -0400 |
parents | 7e8a8b732db3 |
children |
rev | line source |
---|---|
29
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
1 import os |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
2 import sys |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
3 import json |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
4 import logging |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
5 import logging.config |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
6 |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
7 #from util.Filters import TraceBackFormatter |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
8 |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
9 class Logger(object): |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
10 def __init__(self, tool_directory, debug="False", extra_files_path=None): |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
11 self.tool_directory = tool_directory |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
12 self.default_level = logging.INFO |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
13 self.debug = debug |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
14 self.extra_files_path = extra_files_path |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
15 |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
16 def setup_logging(self): |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
17 """Setup logging configuration |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
18 reference: https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/ |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
19 """ |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
20 config_path = os.path.join(self.tool_directory, 'logging.json') |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
21 default_level=logging.INFO |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
22 if self.debug.lower() == "true": |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
23 default_level=logging.DEBUG |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
24 if os.path.exists(config_path): |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
25 with open(config_path, 'rt') as f: |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
26 config = json.load(f) |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
27 config["handlers"]["console"]["level"] = default_level |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
28 if self.extra_files_path: |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
29 for i in config["handlers"]: |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
30 if "filename" in config["handlers"][i]: |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
31 config["handlers"][i]["filename"] = os.path.join(self.extra_files_path, config["handlers"][i]["filename"]) |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
32 logging.config.dictConfig(config) |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
33 else: |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
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") |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
35 else: |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
36 logging.basicConfig(level=default_level) |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
37 logging.warn("Cannot find logging configuration file!\n") |
7e8a8b732db3
planemo upload for repository https://github.com/goeckslab/hub-archive-creator commit 1a81ebd0ddea950b84af3fc830e9267a4814b29f
yating-l
parents:
diff
changeset
|
38 |