/
loggers.py
executable file
·131 lines (112 loc) · 3.45 KB
/
loggers.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
"""
Purpose:
Library for getting loggers of specific configurations in Python.
This will utilize Pythons built-in logging library and will return
an instance of logging that can be implemented in any code
that already implements the logging class.
"""
# Python Library Imports
import inspect
import sys
import logging
###
# Define Loggers
###
def get_stdout_logging(
log_level=logging.INFO,
log_msg_fmt="%(asctime)s.%(msecs)03d %(levelname)s %(message)s",
log_date_fmt="%a, %d %b %Y %H:%M:%S",
log_prefix=None,
):
"""
Purpose:
Get Logger to standard out.
Args:
log_level (log level from logging): Minimum level for messages to log
log_msg_fmt (String): Mesage format for all logs with variable
substitution for known logging options
log_date_fmt (Stringg): Dateformat to append to message
log_prefix (String): prefix to append to message
Return:
logging (Python logging object): Configured logging object
Examples:
>>> logging = get_stdout_logging()
or
>>> logging =\
get_stdout_logging(
log_level=logging.DEBUG,
log_prefix='[test_script]: '',
)
"""
if log_prefix:
log_msg_fmt = f"{log_prefix}{log_msg_fmt}"
logging.getLogger().setLevel(log_level)
logging.basicConfig(
stream=sys.stdout, level=log_level, format=log_msg_fmt, datefmt=log_date_fmt
)
return logging
def get_file_logger(
log_file=None,
log_filemode="a",
log_level=logging.INFO,
log_msg_fmt="%(asctime)s %(levelname)s %(message)s",
log_date_fmt="%a, %d %b %Y %H:%M:%S",
log_prefix=None,
):
"""
Purpose:
Get Logger to file
Args:
log_level (log level from logging): Minimum level for messages to log
log_msg_fmt (String): Mesage format for all logs with variable
substitution for known logging options
log_date_fmt (Stringg): Dateformat to append to message
log_prefix (String): prefix to append to message
Return:
logging (Python logging object): Configured logging object
Examples:
>>> logging = get_file_logger()
or
>>> logging =\
get_file_logger(
log_level=logging.ERROR,
prefix='[test_script]: '',
log_file='./script_im_writing.log'
)
"""
# If no log_file is specified, find the calling file and use that name
if not log_file:
curframe = inspect.currentframe()
calframe = inspect.getouterframes(curframe, 2)
log_file = calframe[1].filename.replace(".py", ".log")
if log_prefix:
log_msg_fmt = f"{log_prefix}{log_msg_fmt}"
logging.getLogger().setLevel(log_level)
logging.basicConfig(
filename=log_file,
filemode=log_filemode,
level=log_level,
format=log_msg_fmt,
datefmt=log_date_fmt,
)
return logging
###
# Log Management
###
def clear_log_handlers():
"""
Purpose:
Remove previous log handlers and configurations. This
will ensure that any new logging that is configured
will take precedence, as logging is first setting
is not overwritten.
Args:
N/A
Return:
N/A
"""
root = logging.getLogger()
if root.handlers:
for handler in root.handlers:
root.removeHandler(handler)