Skip to main content

Basic logging configurations to easily use in many projects

Project description

Python 3.7 PyPI version License: MIT Twitter Follow

Basic Python Logging Configuration

This is a Python pip package that provides a simple function called config() that sets up the logging configuration for a Python project.

How it Works

The config() function takes in three optional arguments: logging_level, libs_to_silence, and env_var_hide_timestamp.

  • logging_level specifies the minimum logging level that should be recorded. The default value is "INFO".
  • libs_to_silence is a list of external libraries whose logging should be silenced. By default, several commonly used libraries are silenced.
  • env_var_hide_timestamp is an environment variable that can be set to hide the timestamp in the log messages.

The function then proceeds to set up the logging configuration based on the provided arguments. It first sets the logging level for external libraries to logging.ERROR.

Next, it checks the LOGGING_LEVEL environment variable and uses its value if it is set, otherwise it uses the logging_level argument.

The log format is then set depending on whether env_var_hide_timestamp is set or not. If it is set, the format only includes the log level, filename, and line number. Otherwise, the format includes the timestamp, log level, filename, and line number.

Finally, the function creates a StreamHandler that logs to the standard output, sets the formatter to the log format, adds the handler to the logger, and sets the logging level to the specified level.

The logger is then returned so that it can be used throughout the project to log messages at the specified level.

Overall, logging_basic_config simplifies the process of configuring logging in a Python project, making it easier to customize the logging level and format, and to silence external libraries.

Example

Install package

pip install logging-basic-config

Using the library

import logging_basic_config

logging = logging_basic_config.config(logging_level="DEBUG")

logging.debug("Debug message")
logging.info("Info message")
logging.warning("Warning message")
logging.error("Error message")
logging.critical("Critical message")

In additional files

Once the logging_basic_config.config(logging_level="DEBUG") code was called in your main or entrypoint call, you only need to import logging and use it in other files.

import logging

logging.info("This is another info message")

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

logging_basic_config-1.4.3.tar.gz (3.5 kB view hashes)

Uploaded Source

Built Distribution

logging_basic_config-1.4.3-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

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