-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.py
41 lines (38 loc) · 1.18 KB
/
Logging.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
"""
Set up logger for logging
"""
import logging
def setup_logger(logger_name, log_file, level=logging.INFO, stream=True):
"""
Set up logger for logging
Parameters
----------
logger_name: 'string'
name of logger
log_file: 'string'
file path for logging file
level: 'logging.type'
Level of logging, default is INFO
stream: 'bool'
Stream logs as well as write to file
Returns
-------
fileHandler: 'logging.FileHandler'
Logging handler for file
streamHandler: 'logging.StreamHandler'
If stream is True, logging handler for streaming
"""
# Create logger with name logger_name
l = logging.getLogger(logger_name)
# Format logger to output time - level - message
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# Create file handler
fileHandler = logging.FileHandler(log_file, mode='w')
fileHandler.setFormatter(formatter)
l.setLevel(level)
l.addHandler(fileHandler)
# If desired create stream handler
if stream:
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(formatter)
l.addHandler(streamHandler)