-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_logging.txt
More file actions
69 lines (45 loc) · 3.31 KB
/
Copy pathpython_logging.txt
File metadata and controls
69 lines (45 loc) · 3.31 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
Commands:
1. import logging
This imports the python log module
2. logging.basicConfig(level=logging.DEBUG)
This sets the default level for logging
logging.DEBUG is just a constant. NOT the same as logging.debug
3. logging.basicConfig(filename='...', level=logging.DEBUG)
Instead of outputting the log messages to the console, this command redirects the output to the the file specified by filename
When you log to a file, the file will keep the old log statements, and just adds the newstatements in. Allow us to see our logged information overtime and over multiple runs
4. logging.basicConfig(filename='logging-output.txt', level=logging.INFO, format='%(filename)s:%(levelname)s:%(message)s')
This allows you to format the output
5. logging.info('...')
Logs the output at info level
logging.debug(), logging.warning(), logging.error()
Commands 1 to 5 are for root logger
# Custom logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formater = logging.Formatter('%(name)s:%(filename)s:%(levelname)s:%(message)s')
file_handler = logging.FileHandler('logging-out.txt')
file_handler.setFormatter(formater)
file_handler.setLevel(logging.DEBUG)
logger.addHandler(file_handler)
- You have to set the logger level or else it will be defaulted to logger.WARNING
- You don't have to set the file_handler level unless you want to
- If you decide to set both the logger and file_handler levels, then the log message has to satisfy both conditions
in order to go through
Notes:
logging levels
- allows us to specify exactly what we want to log by separating these into categories. There are 5 standard logging levels:
debug, info, warning, error, and critical
DEBUG: Detailed information, typically of interest only when diagnosing problems
INFO: Confirmation that things are working as expected
WARNING: An indication that something unexpected happened, or indicative of some problem in the near future (e.g. 'disk space low'). The software is still working as expected
ERROR: Due to a more serious problem, the software has not been able to perform some function
CRITICAL: A serious error, indicating that the program itself may be unable to continue running
Default level is set to warning. Captures anything a warning or above (ERROR and CRITICAL)
- If not specified, the default logger is the root logger. It is a good practice to configure specific loggers that can be configured seperately
- You would want to have specific loggers because you want to be able to configure each one of them to a specific need
- If you were to just use the root logger, and you import another library/module that also uses the root logger. Then the basicConfig you
set won't be able to override the basciConfig from that import library/module. Instead, the log statements you have will be printed according
to the root logger that was configured in imported library/module.
- Let's say you have two different handlers: FileHandler and StreamHandler. If neither of those have setLevels, the levels will go according to the level you
set for the logger object. In that case, the logs will be printed to the specified file and also to the specified output stream.
However, if FileHandler and StreamHandler are set, then the loggging messages will have to satisfy their levels (in respect to their specific handler) in addition to the logger's set level.