forked from oneThousand1000/HairMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
66 lines (50 loc) · 2.26 KB
/
logger.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# python3.7
"""Utility functions for logging."""
import os
import sys
import logging
__all__ = ['setup_logger']
def setup_logger(work_dir=None, logfile_name='log.txt', logger_name='logger'):
"""Sets up logger from target work directory.
The function will sets up a logger with `DEBUG` log level. Two handlers will
be added to the logger automatically. One is the `sys.stdout` stream, with
`INFO` log level, which will print improtant messages on the screen. The other
is used to save all messages to file `$WORK_DIR/$LOGFILE_NAME`. Messages will
be added time stamp and log level before logged.
NOTE: If `work_dir` or `logfile_name` is empty, the file stream will be
skipped.
Args:
work_dir: The work directory. All intermediate files will be saved here.
(default: None)
logfile_name: Name of the file to save log message. (default: `log.txt`)
logger_name: Unique name for the logger. (default: `logger`)
Returns:
A `logging.Logger` object.
Raises:
SystemExit: If the work directory has already existed, of the logger with
specified name `logger_name` has already existed.
"""
logger = logging.getLogger(logger_name)
if logger.hasHandlers(): # Already existed
raise SystemExit(f'Logger name `{logger_name}` has already been set up!\n'
f'Please use another name, or otherwise the messages '
f'may be mixed between these two loggers.')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter("[%(asctime)s][%(levelname)s] %(message)s")
# Print log message with `INFO` level or above onto the screen.
sh = logging.StreamHandler(stream=sys.stdout)
sh.setLevel(logging.INFO)
sh.setFormatter(formatter)
logger.addHandler(sh)
if not work_dir or not logfile_name:
return logger
if os.path.exists(work_dir):
raise SystemExit(f'Work directory `{work_dir}` has already existed!\n'
f'Please specify another one.')
os.makedirs(work_dir)
# Save log message with all levels in log file.
fh = logging.FileHandler(os.path.join(work_dir, logfile_name))
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
return logger