Skip to main content

Initialize Python logging with RWS Datalab defaults

Project description

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.

parameters for the LoggingConfig object

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logginginitializer-1.2.0.tar.gz (18.2 kB view details)

Uploaded Source

Built Distribution

logginginitializer-1.2.0-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file logginginitializer-1.2.0.tar.gz.

File metadata

  • Download URL: logginginitializer-1.2.0.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for logginginitializer-1.2.0.tar.gz
Algorithm Hash digest
SHA256 88c466fc2103546f9f561251d00ce0480c4ce6373cc598ddb561e42a5da82a15
MD5 f2737245e69a48b7fe64c16b670290f9
BLAKE2b-256 97d7eb87f103d19bebccbffde67ddbebfc3a5cc1b21b47bc587db6990829ceec

See more details on using hashes here.

File details

Details for the file logginginitializer-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for logginginitializer-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ed68086323f6823f28fd59805a16204a627d767542eef07b5e7d77d8b76802cd
MD5 00fd3d36ca868c7a48bf38176d5ee09c
BLAKE2b-256 1029e410a4c71f279d39c81b2812d2b389dc9f10d262a95667b0c09dc4fd86d7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page