-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathlog.py
More file actions
74 lines (62 loc) · 2.23 KB
/
log.py
File metadata and controls
74 lines (62 loc) · 2.23 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
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
"""
Logging utilities for diffsync.
This module contains helpers and wrappers for making logging more consistent across applications.
How to use me:
>>> from diffsync.log import initialize_logging
>>> log = initialize_logging(level="debug")
"""
import logging.config
APP = "diffsync"
def initialize_logging(config=None, level="INFO", filename=None):
"""Initialize logging using sensible defaults.
Args:
config (dict): User provided configuration dictionary.
level (str): The level of logging for STDOUT logging.
filename (str): Where to output debug logging to file.
"""
if not config:
config = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"standard": {
"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s",
"datefmt": "%Y-%m-%dT%H:%M:%S%z",
},
"debug": {
"format": "%(asctime)s [%(levelname)s] [%(module)s] [%(funcName)s] %(name)s: %(message)s",
"datefmt": "%Y-%m-%dT%H:%M:%S%z",
},
},
"handlers": {
"standard": {
"class": "logging.StreamHandler",
"formatter": "standard",
"level": level.upper(),
},
},
"loggers": {
"": {
"handlers": ["standard"],
"level": "DEBUG",
}
},
}
# If a filename is passed in, let's add a FileHandler
if filename:
config["handlers"].update(
{
"file_output": {
"class": "logging.FileHandler",
"formatter": "debug",
"level": "DEBUG",
"filename": filename,
}
}
)
config["loggers"][""]["handlers"].append("file_output")
# Configure the logging
logging.config.dictConfig(config)
# Initialize root logger and advise logging has been initialized
log = logging.getLogger(APP)
log.debug("Logging initialized.")