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, archived log folder path, 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 Archived Log Folder Path

from RsLogMod import Configure

# Set the path to the desired folder for storing old logs
Configure.set_archive_path('/new/archive/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.1.tar.gz (8.8 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.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for rslogmod-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c20a67da4965a39f664774d1441c601179d521110c21603613d19957de44d97d
MD5 5fe72182a7416b0254062a3232310128
BLAKE2b-256 45a822686bf700f19811da21702a518002507e91c2d766bcd31af51797b2780d

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for RsLogMod-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a9b9b2f16c2da4993cf8638d91a73e38e559fe1b58c1facd02c0ae3d0f87093d
MD5 384f793afda5f5fd9763a4bd496721b2
BLAKE2b-256 922cd4c6377e8d9eb247ffde97822d762cff59f1e3b2e811c2d992cd80147d89

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