Skip to main content

A versatile logging module for Python projects

Project description

RsLogMod

RsLogMod is a flexible and powerful Python logging module designed to help manage log files with features such as log rotation, customizable log paths, and various log levels. It's designed to be simple to use while offering extensive customization options.

Table of Contents

Installation

To install the module:

pip install rslogmod

Import the module into your project:

from RsLogMod import rlog, Configure

Getting Started

Here’s a quick example to get you started with logging:

from RsLogMod import rlog, Configure

# set a folder path this path will be saved in the rlogmod config file so this only needs to be done once
Configure.set_log_folder_path('path/to/target/folder')

# Log a simple message
rlog(log_name='my_log', log_level=1, log_entry='This is a log entry.')

Explanation

  • log_name: The name of the log file (e.g., my_log).
  • log_level: The log level, where 1 corresponds to INFO.
  • log_entry: The message you want to log.

This will create a log file named my_log.log (if it doesn't already exist) in the configured directory with the entry formatted as shown below.

Example Output

If the example above is executed with log_level=1, the log entry will look like this in the log file:

[INFO] 2024-12-02 13:20: This is a log entry.

Here’s how the output would differ for each log level:

  • log_level=0: [DEBUG] 2024-12-02 13:20: This is a log entry.
  • log_level=1: [INFO] 2024-12-02 13:20: This is a log entry.
  • log_level=2: [ERROR] 2024-12-02 13:20: This is a log entry.
  • log_level=3: [CRITICAL] 2024-12-02 13:20: This is a log entry.
  • log_level=4: [SEC-ALERT] 2024-12-02 13:20: This is a log entry.
  • log_level=5: [SEC-BREACH] 2024-12-02 13:20: This is a log entry.

Default Behavior

RsLogMod comes with the following default behaviors:

  1. Log Rotation Enabled by Default: Log rotation is enabled by default. This means that when a log file reaches the maximum size, it will automatically be archived, and a new log file will be created.

  2. Default Maximum Log Size: The default maximum log file size is set to 50 MB. This can be adjusted using the configuration settings.

  3. No Log Folder Path Set: If you do not set a log folder path using the Configure.set_log_folder_path() method, RsLogMod will only log to the terminal and will not create any log files.

  4. Configuration Persistence: Once you set the log folder path, this path will be stored in the configs.json file, and all subsequent logs will be written to files within that directory. You only need to set the log folder path once; it will persist across sessions unless explicitly changed.

These defaults ensure that RsLogMod is ready to use out of the box, but also easily customizable for different environments and needs.

Log Levels & Prefixes

RsLogMod supports several log levels, each with its own prefix:

  • 0: [DEBUG]
    • For detailed debugging information.
  • 1: [INFO]
    • For general information about program execution.
  • 2: [ERROR]
    • For errors that occur during execution.
  • 3: [CRITICAL]
    • For critical issues that need immediate attention.
  • 4: [SEC-ALERT]
    • For security-related alerts.
  • 5: [SEC-BREACH]
    • For security breaches or serious security issues.

Customization

To customize how RsLogMod operates, such as changing the log file directory, setting the maximum log file size, or enabling log rotation, you can use the Configure class.

Set Log Folder Path

from RsLogMod import Configure

# Set log folder path
Configure.set_log_folder_path('/new/log/directory')

Set Maximum Log Size

from RsLogMod import Configure

# Set max size for log files to 20 MB
Configure.set_log_file_max_size(20)

Enable or Disable Log Rotation

from RsLogMod import Configure

# Enable log rotation
Configure.enable_log_rotation(True)

Display Configuration

from RsLogMod import Configure

# Display the current configuration
Configure.display()

Explanation

  • Configure: This class manages the configuration settings for RsLogMod, such as where log files are stored, how large they can grow before being rotated, and whether log rotation is enabled.
  • set_log_folder_path(path: str): Sets the directory where logs are saved.
  • set_log_file_max_size(mega_bytes: int): Sets the maximum size for log files in megabytes.
  • enable_log_rotation(value: bool): Enables or disables log rotation.
  • display(): Prints the current configuration in a JSON format for easy viewing.

Configuration

RsLogMod uses a configs.json file to store its settings. Here’s an example configuration file:

{
  "log_rotation": true,
  "max_size_in_mega_bytes": 10,
  "log_folder": "/path/to/logs",
  "archive_folder": "/path/to/logs/archived"
}
  • log_rotation: Enables or disables log rotation.
  • max_size_in_mega_bytes: The maximum size of a log file before it is rotated.
  • log_folder: The directory where log files will be stored.
  • archive_folder: The directory where old log files will be stored after rotation.

These settings can be updated programmatically using the Configure class, as shown in the examples above.

License

This project is licensed under the MIT License. See the LICENSE file for details.

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

rslogmod-1.0.0.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

RsLogMod-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file rslogmod-1.0.0.tar.gz.

File metadata

  • Download URL: rslogmod-1.0.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for rslogmod-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e043bcc04add538c230c0777d33d59a5b207089981112fc7031e425742692c48
MD5 3439913016c664c6db6248eef19185c2
BLAKE2b-256 81419d9b6e98d65a754b8eede078b3d67d866407accaf5a781693dfdcb44e28a

See more details on using hashes here.

File details

Details for the file RsLogMod-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: RsLogMod-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for RsLogMod-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f6a5fc3cbb02eae92b3bbc286446885dd5aae09d139af8c8eba9962ca1eea69
MD5 0803df49a5f5cfc6fc022041a85454f9
BLAKE2b-256 2222ef4025421afea1c7f60c2d6c60a12d3a61c11e3537b849cb5a5d10843c09

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