|
| 1 | +## Logging |
| 2 | +```python |
| 3 | +When you transfer money, there are transfer records. |
| 4 | +If something goes wrong, people can read the log and has a chance to figure out what happened. |
| 5 | +Likewise, logging is important for system developing, debugging and running. |
| 6 | +When a program crashes, if there is no logging record, you have little chance to understand what happened. |
| 7 | +You can use the logging functions by just importing logging. |
| 8 | +``` |
| 9 | +### Logging Levels |
| 10 | +``` |
| 11 | +There are many logging levels. |
| 12 | +The numeric values of logging levels are given in the following table. |
| 13 | +These are primarily of interest if you want to define your own levels, and need them to have specific values relative to the predefined levels. |
| 14 | +If you define a level with the same numeric value, it overwrites the predefined value; the predefined name is lost. |
| 15 | +``` |
| 16 | +|Level| Numeric value| |
| 17 | +|-----|------| |
| 18 | +CRITICAL|50 |
| 19 | +ERROR|40 |
| 20 | +WARNING|30 |
| 21 | +INFO|20 |
| 22 | +DEBUG|10 |
| 23 | +NOTSET|0 |
| 24 | +```python |
| 25 | +They are usually used with: |
| 26 | +logging.basciConfig(level=logging.DEBUG) Or |
| 27 | +logging.basciConfig(level=logging.INFO) |
| 28 | +``` |
| 29 | +### Steps |
| 30 | +```python |
| 31 | +Create a file and enter the logging code and then run the code , output will appear on the shell. |
| 32 | +If you are using any file name like: |
| 33 | +logging.basicConfig(filename='example.log',level=logging.DEBUG) |
| 34 | +Then you can see your output in the file named ‘example’ . |
| 35 | +However the default level is WARNING |
| 36 | +``` |
| 37 | +#### Case 1 |
| 38 | +```python |
| 39 | +If no level was mentioned: |
| 40 | +It does not take the .info information |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +#### Case 2 |
| 45 | +```python |
| 46 | +When level was debug, all the statements were executed. |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +```python |
| 51 | +Here we can see that logging is similar to print statements |
| 52 | +But.. |
| 53 | +It works when the program is a simple script, but for complex systems, you better not to use this approach. First of all, you cannot leave only important messages in the log, you may see a lots of garbage messages in the log, but can’t find anything useful.You also cannot control those print statements without modifying code, you may forgot to remove those unused prints. |
| 54 | + |
| 55 | +It can be used with some formatting functions also: |
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | + |
0 commit comments