-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.py
39 lines (30 loc) · 1 KB
/
util.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
import os
import logging
fh = None
ch = None
def get_logger(app_name="Some Application", logfolder=".", fname="log_file.log"):
"""
Returns a function which logs messages to console and also to a file (in PWD).
For more info, see: https://docs.python.org/3.6/howto/logging-cookbook.html#logging-cookbook
"""
logger = logging.getLogger(app_name)
logger.setLevel(logging.DEBUG)
global fh
global ch
if fh is not None:
fh.flush()
fh.close()
print("Flushed and closed FH.")
fh = logging.FileHandler(logfolder + os.sep + fname)
fh.setLevel(logging.DEBUG)
if ch is None:
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
print("Created new stream Handler.")
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
return logger