logging initializer
Initialize Python logging with RWS Datalab defaults. A tiny module to make it easy to define your logger settings!
How to log in Python applications
If you already know how to log and just want to use the LoggingInitializer, see :ref: using-LI.
At the start of each module, import the Logging library and generate a logger:
import logging
logger = logging.getLogger(__name__)
This generates an object which knows from which module you’re logging. It will reference the module in the log.
In your code, tell the logger to generate log messages where needed:
logger.debug("Going to do something important now.")
try:
something_important()
except:
logger.error("Something important didn't work!!!")
logger.info("Something important is done.")
The methods debug(), info() and error() (we also have warning() and critical()) generate log messages at different log levels. Log levels are used to communicate the importance of a log line, to keep logs relevant, and as small as possible (but not smaller).
You will probably choose different log levels on different environments: on a production server, most teams will only log “WARNING”, “ERROR” and “CRITICAL”; on a testing server usually also “INFO” so it’s easier to follow what’s going on, and when debugging… “DEBUG”. When writing log messages, choose your log levels with this in mind.
The default setting for LoggingInitializer is INFO, which means that statements logged with logger.debug() will not be logged by default.
For more information on how to use the Python logger and info on the different log levels, see this helpful tutorial in the Python docs.
Using LoggingInitializer
Include the LoggingInitializer in your application to quickly set parameters for the Python logger.
At the start of your program, populate a LoggingConfig object with the required parameters.
Name |
Description |
Required? |
Default value |
|---|---|---|---|
identifier |
Identifying the program being logged, will be used in file name of the log |
Yes |
|
directory |
Directory for the log file |
No |
None |
log_level |
Default log level for log lines without a specified level |
No |
“INFO” |
log_format |
Format for log messages |
No |
“%(levelname)s:%(asctime)s - %(name)s - %(message)s” |
date_format |
Format for log timestamps |
No |
“%Y/%m/%d %H:%M:%S %p” |
backup_count |
Number of log files to be kept before a file is re-used |
No |
50 |
Below an example of a logger that writes to StdOut and to a file is portrayed. The user executing the program should have write rights on the specified directory!
config = {
"identifier": "my_lovely_program",
"directory": "/var/logs",
}
logging_config = LoggingConfig(**config)
Then create the LoggingInitializer object:
logging_initializer = LoggingInitializer(logging_config)
If you want to suppress the logging to StdOut (only log to a file), use Quiet Mode:
logging_initializer = LoggingInitializer(logging_config, quiet=True)
If you want to suppress the logging to a file (only log to StdOut), simply don’t specify a directory in the LoggingConfig.
logging_config = LoggingConfig(identifier="your-package-name")
logging_initializer = LoggingInitializer(logging_config)
For more info on setting the log format and date format, see the Python docs. If another application is ingesting the logs, do not change the format unannounced.
Installation
To install logging-initializer, do:
git clone https://gitlab.com/rwsdatalab/projects/logging-initializer.git
cd logging-initializer
pip install .
Run tests (including coverage) with:
pip install -r requirements-dev.txt
python setup.py test
Documentation
Find the full documentation here.
License
Copyright (c) 2022-2023, Rijkswaterstaat
Metadata
Release files for logginginitializer 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| logginginitializer-1.2.0.tar.gz | 18.2 kB | Without provenance | Details for logginginitializer-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| logginginitializer-1.2.0-py3-none-any.whl | Python 3 | none | any | Details for logginginitializer-1.2.0-py3-none-any.whl |
Total release size: 24.6 kB
Release files / logginginitializer-1.2.0.tar.gz
| Download URL | logginginitializer-1.2.0.tar.gz |
|---|---|
| Size | 18.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
88c466fc2103546f9f561251d00ce0480c4ce6373cc598ddb561e42a5da82a15
|
|
BLAKE2b-256 checksum How to use checksums |
97d7eb87f103d19bebccbffde67ddbebfc3a5cc1b21b47bc587db6990829ceec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.12
|
Release files / logginginitializer-1.2.0-py3-none-any.whl
| Download URL | logginginitializer-1.2.0-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ed68086323f6823f28fd59805a16204a627d767542eef07b5e7d77d8b76802cd
|
|
BLAKE2b-256 checksum How to use checksums |
1029e410a4c71f279d39c81b2812d2b389dc9f10d262a95667b0c09dc4fd86d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.12
|
Security for logginginitializer 1.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.