5 Things About Python Logging
This repository accompanies a talk given at the SFPython meet-up (here).
It is a collection of basic examples for how to use Python's logging API.
Loggers
- Simple logging: loggers/simple/main.py
- Using logger namespaces (or ancestry): loggers/namespaces/main.py
- Using __name__ for your namespaces: loggers/free_namespaces/main.py
Handlers
- Simple stream handlers: handles/streams/main.py
- Simple file handlers: handlers/files/main.py
- Handler levels vs. logger levels: handlers/levels/main.py
- Log messages over a network: handlers/network/main.py
Formatters
- Basic message formatting: formatters/message/main.py
- Date/Time formatting: formatters/date_time/main.py
BOOKMARK THESE SITES:
- Python string format cheat-sheet: https://pyformat.info/
- Python
LogRecord
attribute list: https://docs.python.org/2.7/library/logging.html#logrecord-attributes - Python
strftime
format strings: https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior
Filters
- Basic filtering: filters/simple/main.py
- Injecting
LogRecord
attributes: filters/inject/main.py
Configuration
- Using
basicConfig
: configuration/basicConfig/main.py - Using
dictConfig
: configuration/dictConfig/main.py - Using
fileConfig
: configuration/fileConfig/main.py
Example Configuration
.ini
/.conf
file: configuration/fileConfig/config.ini- Native Python
dict
: configuration/dictConfig/main.py
Bonus - Python Log Config-Change Server
This is completely bonkers. Python has a built-in logging configuration update server.
This means you can change the logging config for an entire runtime on the fly, without restarting the process.
NOTE: This example uses Flask. If you want to run main.py
, please be sure to pip install Flask
first.
- Python Log Server: log_server_WAT/main.py