-
Notifications
You must be signed in to change notification settings - Fork 2
/
logging_tutorial.py
executable file
·68 lines (52 loc) · 1.67 KB
/
logging_tutorial.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
import logging
#%%
logger = logging.getLogger("my_app")
id(logger)
another_logger = logging.getLogger("my_app")
id(another_logger)
id(logging.Logger("my_app"))
#%%
logging_levels = [logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL]
for logging_level in logging_levels:
print(f"Logging Level {logging._levelToName[logging_level]}: {logging_level}")
#%%
logger = logging.getLogger("my_app")
logger.setLevel(logging.WARNING)
#%%
# Add a file handler
fh = logging.FileHandler("my_app_logging.log")
logger.addHandler(fh)
# Send some log events
logger.error("This is an error message.")
logger.critical("This is a critical message.")
logger.info("This is an info message.")
# Check the log
with open("my_app_logging.log") as file:
print(file.read())
#%%
# for handler in logger.handlers:
# logger.removeHandler(handler)
# Add a stream handler
sh = logging.StreamHandler()
logger.addHandler(sh)
logger.critical("Just a random critical event.")
#%%
logger.critical("This is a critical message.")
logger.error("This is an error message.")
logger.warning("This is a warning message.")
logger.info("This is an info message.")
logger.debug("This is a debug message.")
#%%
logger.removeHandler(sh)
# Create a new file handler with formatting
fh = logging.FileHandler("formatted_log.log")
fh_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
fh.setFormatter(fh_formatter)
logger.addHandler(fh)
# Logging events
logger.warning("Warning message with formatting")
logger.error("Error message with formatting")
logger.critical("Critical message with formatting")
# Check the log
with open("formatted_log.log") as file:
print(file.read())