Skip to main content

LogFlex, a robust and flexible logging extension for Python, enhances the standard logging module by providing streamlined configuration via environmental variables and TOML files. This library supports comprehensive log management with features like dynamic log levels, multiple output handlers (console, file, and Syslog), and automatic log rotation. Ideal for developers looking to implement advanced logging practices easily in any Python application.

Project description

LogFlex

LogFlex is a flexible and powerful logging extension for Python applications, enhancing the built-in logging capabilities with advanced configuration options and support for multiple output handlers.

Features

  • Customizable Log Levels: Set different log levels for different output handlers.
  • File and Console Logging: Easily log to files and the console with simple configuration.
  • Syslog Integration: Send logs to a Syslog server for centralized logging.
  • Colorized Output: Make console output more readable with customizable colorization.
  • Automatic File Rotation: Rotate log files based on time or size automatically.

Installation

Install LogFlex using pip:

pip install logflex

Configuration

LogFlex uses a combination of a TOML configuration file and environment variables to customize its behavior. Here is how you can set up your logging:

Config File (config.toml)

Create a config.toml in your project root or specify its path during logger initialization. Here's an example configuration:

[general]
log_level = "DEBUG"
verbose = false
trace = false

[general.color_settings]
enable_color = true
datefmt = "%Y-%m-%d %H:%M:%S"
reset = true
log_colors.DEBUG = "cyan"
log_colors.INFO = "green"
log_colors.WARNING = "yellow"
log_colors.ERROR = "red"
log_colors.CRITICAL = "red,bg_white"
style = "%"

[file_handler]
logdir = "./logs"
logfile = "app.log"
dedicate_error_logfile = true

[syslog_handler]
use_syslog = true
syslog_address = "localhost"
syslog_port = 514 # UDP Port
syslog_facility = 'LOG_USER'

Environment Variables

You can also configure LogFlex using environment variables, which override the settings from config.toml. Here are some of the variables you can set:

GENERAL_LOG_LEVEL=DEBUG
GENERAL_VERBOSE=false
GENERAL_TRACE=false
FILEHANDLER_LOGDIR=/var/log/myapp
FILEHANDLER_LOGFILE=app.log
SYSLOGHANDLER_USE_SYSLOG=true
SYSLOGHANDLER_SYSLOG_ADDRESS=localhost
SYSLOGHANDLER_SYSLOG_PORT=514
COLOR_ENABLE_COLOR=true
COLOR_DATEFMT="%Y-%m-%d %H:%M:%S"
COLOR_RESET=true
COLOR_LOG_COLORS_DEBUG="cyan"
COLOR_LOG_COLORS_INFO="green"
COLOR_LOG_COLORS_WARNING="yellow"
COLOR_LOG_COLORS_ERROR="red"
COLOR_LOG_COLORS_CRITICAL="red,bg_white"
COLOR_STYLE="%"
FILEHANDLER_LOGDIR="./logs"
FILEHANDLER_LOGFILE="app.log"
FILEHANDLER_WHEN='midnight'
FILEHANDLER_INTERVAL=7
FILEHANDLER_BACKUP_COUNT=7
FILEHANDLER_DEDICATE_ERROR_LOGFILE=true
SYSLOGHANDLER_USE_SYSLOG=true
SYSLOGHANDLER_SYSLOG_ADDRESS='localhost'
SYSLOGHANDLER_SYSLOG_PORT=514
SYSLOGHANDLER_SYSLOG_FACILITY='LOG_USER'

Usage

Basic Setup

from logflex import CustomLogger

# Initialize logger with default settings
logger = CustomLogger('exampleModule', loglevel='INFO')

# Write logs
logger.info("This is an info message")
logger.error("This is an error message")

Using Stream Handler

from logflex import CustomLogger

# Initialize logger for console output with debug level
logger = CustomLogger('exampleModule', loglevel='DEBUG', verbose=True, trace=False, color_enable=True)

# Console output
logger.debug("This message will appear in the console.")

Using File Handler

from logflex import CustomLogger

# Initialize logger with file output
logger = CustomLogger('exampleModule', loglevel='WARNING', file_logdir='./log', file_logfile='app.log',
                      file_when='midnight', file_interval=7, file_backup_count=7, file_dedicate_error_logfile=False)

logger.warning("This message will be logged to a file.")

Using Syslog Handler

from logflex import CustomLogger

# Initialize logger with Syslog handler
logger = CustomLogger('exampleModule', loglevel='CRITICAL', use_syslog=True, syslog_address='localhost', syslog_port=514,
                      syslog_facility='LOG_LOCAL0')


# Syslog output
logger.critical("This critical message will be sent to the configured Syslog server.")

Contributing

Contributions to LogFlex are welcome! Please fork the repository and submit a pull request with your changes or improvements.

License

LogFlex is released under the MIT License. See the LICENSE file for more details.

Support

For support, please open an issue on the GitHub repository or contact the maintainers directly.

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

logflex-0.1.6.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

logflex-0.1.6-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file logflex-0.1.6.tar.gz.

File metadata

  • Download URL: logflex-0.1.6.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for logflex-0.1.6.tar.gz
Algorithm Hash digest
SHA256 b798b60eee0d19044233ae20e80e53f93181162c5a8e2a72fbb3e1ac8a22e03d
MD5 33b10b6c52ea5febd241db6bd5bbb55e
BLAKE2b-256 24183e6a79ff644d02795cd3f0ccea373f4e0aeb635bc059bd2d86b7a8a09687

See more details on using hashes here.

File details

Details for the file logflex-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: logflex-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.5

File hashes

Hashes for logflex-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d746f9eb84a7e99e3d4aab914418064803225f9c4e47769de53e4744fe11d755
MD5 a2b198123006d6c8d21d86f9078c567a
BLAKE2b-256 c28dedd4d53617980a7ab299e504b3dadfbfb57bd4c72c5f2f7bbb6009d7c6ef

See more details on using hashes here.

Supported by

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