Basics: Logging in Python
T K Sourab
17th March, 2018
Why logging?
- What happened?
- When it happened?
- Where it happened?
- How important is it?
Why logging?
YOU ARE NOT THE ONLY ONE USING YOUR APPLICATION.
(Unless it's a college project or written in perl)
Python logging module
- Released in 2002
- since Python 2.3
- Was built keeping in mind the Apache Log4j project
Features
- EXTREMELY configurable
- Supports different destination with different formats
- Triggers or alerts on specific condition
- Performance metric
TERMS
- Logging levels
- Logger
- LogRecord
-
Handlers
- Formatters
Logging Levels
-
DEBUG Detailed information, of no interest when everything is working well but invaluable when diagnosing problems.
-
INFO Affirmations that things are working as expected, e.g. "service has started" or "indexing run complete". Often ignored.
-
WARNING There may be a problem in the near future, and this gives advance warning of it. But the application is able to proceed normally.
-
ERROR The application has been unable to proceed as expected, due to the problem being logged
-
CRITICAL This is a serious error, and some kind of application meltdown might be imminent.
Logger
- Controls handlers and namespaces
LogRecord
- Contains attributes of the message
Formatter
- Formats the LogRecord to string format.
Handlers
- Specifies where to send the logRecord.
How logging works?
Ways to configure Logging
- Manually in code
- FileConfig
- DictConfig
Resources
https://docs.python.org/3.5/library/logging.html
Shukriya
@sourabhtk37
sourabhtk37@gmail.com
github.com/sourabhtk37
Basics: Logging in Python
By tk sourabh
Basics: Logging in Python
Talk on logging in python
- 2,314