-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging_example_01.py
More file actions
124 lines (87 loc) · 2.89 KB
/
logging_example_01.py
File metadata and controls
124 lines (87 loc) · 2.89 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
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
import logging
import logging.config
import tempfile
import yaml
def main1():
logging.info("Don't show this")
logging.basicConfig(level=logging.INFO) # b/c we have already logged this is ineffective
logging.info("Don't show this")
logging.error("Show this")
def main2():
logging.basicConfig(level=logging.INFO)
logging.info("Show this")
logging.error("Show this")
logging.debug("Don't show this")
def main3():
with tempfile.NamedTemporaryFile() as tf:
logging.basicConfig(filename=tf.name, level=logging.INFO)
logging.info(f"using file ${tf.name}")
logging.info("test")
with open(tf.name) as f:
print(f.readlines())
def main4():
logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s - %(message)s", level=logging.INFO)
logging.info("hello")
def main5():
logger = logging.getLogger("main5")
logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s - %(message)s", level=logging.INFO)
logger.info("hello")
class ABC:
def __init__(self):
self.logger = logging.getLogger(self.__class__.__name__)
logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s - %(message)s", level=logging.INFO) # (****)
def logf(self):
self.logger.info("logf called")
class BCD:
def __init__(self):
self.logger = logging.getLogger(self.__class__.__name__)
logging.basicConfig(format="%(asctime)s %(levelname)s %(name)s - BBB %(message)s", level=logging.INFO)
def logf(self):
self.logger.info("logf called")
def main6():
abc = ABC()
abc.logf()
bcd = BCD()
bcd.logf() # Note: still uses the basicConfig from the ABC call
# rerunning with line (****) commented out, shows no log for abc.logf, but does show the log for bcd.logf and
# uses the form of logging from BCD
def main7():
bcd = BCD()
abc = ABC()
abc.logf()
bcd.logf()
def main8():
with open("logging_1.yaml") as log_conf_file:
log_conf = yaml.load(log_conf_file)
logging.config.dictConfig(log_conf)
loga = logging.getLogger("loga")
loga.info("yoyoyo")
logb = logging.getLogger("logb")
logb.info("tralalal")
def f(x, log=logging.getLogger("f_logger")):
log.info(f"f called with {x}")
def main9(log_conf_filename="logging_1.yaml"):
with open(log_conf_filename) as log_conf_file:
log_conf = yaml.load(log_conf_file)
logging.config.dictConfig(log_conf)
loga = logging.getLogger("loga")
loga.info("yoyoyo")
f(2)
f(3, loga)
def ff(x):
logging.basicConfig(level=logging.DEBUG)
logging.debug("hi", stack_info=True)
return x
def main10():
ff(3)
def main11():
la = logging.getLogger("loggera")
la.setLevel(logging.WARN)
la.info("hi")
la.setLevel(logging.INFO)
la.info("hi")
if __name__ == '__main__':
#main9("logging_2.yaml")
#main10()
main11()
print("hi")