-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlog-samples.py
More file actions
64 lines (52 loc) · 2.02 KB
/
Copy pathlog-samples.py
File metadata and controls
64 lines (52 loc) · 2.02 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
import logging
import logging.config
import yaml
class LoggerFactory():
def __init__(self):
return
@staticmethod
def create_logger(level=logging.INFO):
# https://docs.python.org/2/library/logging.html#logrecord-attributes
logging.basicConfig(level=level, format='%(asctime)s %(name)s %(levelname)-5s [%(module)s.%(funcName)s()] %(message)s')
logger = logging.getLogger()
logger.setLevel(level)
return logger
@staticmethod
def create_logger_by_config_file(config_log_file_name):
with open(config_log_file_name) as logging_config_file:
logging_config = yaml.load(logging_config_file)
logging.config.dictConfig(logging_config)
logger = logging.getLogger()
return logger
class Logic():
def __init__(self, logger):
self.logger = logger
def run(self, x, y):
self.logger.info("Logic.run() started")
if (y==0):
self.logger.warn("y==0")
r = x*y
self.logger.info("Logic.run() x*y = %s", r)
if self.logger.isEnabledFor(logging.DEBUG):
self.logger.debug("Logic.run() x = %s", x)
self.logger.debug("Logic.run() y = %s", y)
self.logger.info("Logic.run() completed")
return r
if __name__ == "__main__":
main_logger = LoggerFactory.create_logger(level=logging.WARNING)
main_logger.info("short log")
logic = Logic(main_logger)
logic.run(2, 0)
main_logger = LoggerFactory.create_logger(level=logging.INFO)
main_logger.info("info log")
logic = Logic(main_logger)
logic.run(2, 0)
main_logger = LoggerFactory.create_logger_by_config_file(r".\debug-logging-config.yml")
main_logger.info("detailed log, created by config file")
logic = Logic(main_logger)
logic.run(2, 0)
main_logger = LoggerFactory.create_logger_by_config_file(r".\seq-logging-config.yml")
main_logger.info("detailed log, created by config file")
logic = Logic(main_logger)
logic.run(2, 0)
main_logger.info("finished")