-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathLogFileSetup.py
More file actions
37 lines (30 loc) · 1.72 KB
/
LogFileSetup.py
File metadata and controls
37 lines (30 loc) · 1.72 KB
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
import logging
def configure_logging(logfile_path):
''' Initialize logging defaults for in-notebook messages and
'log.info' file written to store intermediate results during analysis of each defect
To use, the following lines must be added to the code:
import LogFileSetup as lfs
logger = lfs.configure_logging(os.path.join(PATH-TO-LOGFILE-DIR, "log"))
Usage example in notebook: logger.info("MESSAGE")
'''
# Set default format for each line of log messages within notebook
notebook_formatter = logging.Formatter("[%(levelname)s] [Cell line num: %(lineno)s] %(message)s")
# Set default format for each line in log.info file (look into methods to outputt cell num, not just line num in cell)
# info_file_formatter = logging.Formatter("[%(levelname)s] [Notebook cell num: %(???)s] [Cell line num: %(lineno)s] %(message)s")
# Initialise log.info for defect processing information
defect_logger = logging.getLogger()
# For log.info file
info_file_handler = logging.FileHandler(logfile_path + ".info", mode='w')
info_file_handler.setLevel(logging.INFO)
# info_file_handler.setFormatter(info_file_formatter)
# For messages within notebook
notebook_handler = logging.StreamHandler()
notebook_handler.setLevel(logging.INFO)
notebook_handler.setFormatter(notebook_formatter)
# Remove default handlers and add custom ones (for log.info file and messages in notebooks)
list(map(defect_logger.removeHandler, defect_logger.handlers[:]))
list(map(defect_logger.removeFilter, defect_logger.filters[:]))
defect_logger.setLevel(logging.INFO)
defect_logger.addHandler(info_file_handler)
defect_logger.addHandler(notebook_handler)
return defect_logger