Skip to main content

DM-Logger

Urls

Features

  • Flexible log formatting with customizable components
  • Automatic error location display for ERROR and CRITICAL levels
  • Log file rotation based on size
  • Separate streams for DEBUG/INFO and WARNING/ERROR/CRITICAL messages
  • Support for additional parameters in log messages
  • Exception logging with automatic error location detection

Installation

pip install dm-logger

Usage

Basic Usage

For simple usage, just create a logger with a name:

from dm_logger import DMLogger

# Create logger with default settings
logger = DMLogger()  # default name "Main"
# or
logger = DMLogger("my_app")  # with custom name

# Logging different levels
logger.debug("Debug message")
logger.info("Info message")
logger.warning("Warning message")
logger.error("Error message")
logger.critical("Critical message")

# Logging with additional parameters
logger.info("Processing", task_id=123, status="running")
# Output: 01-01-2025 11:22:33.555 [INFO] [Main] {task_id: 123, status: 'running'} -- Processing

# Logging exceptions
try:
    result = 1 / 0
except Exception as e:
    logger.error(e)  # Will automatically show error location
    # Output: 01-01-2025 11:22:33.555 [ERROR] [Main] (test.py:10) ZeroDivisionError: division by zero

Default settings:

  • Console output enabled (std_logs=True)
  • File logging disabled (file_logs=False)
  • Logging level set to DEBUG
  • Shows datetime, level, and logger name

Logging Level Configuration

You can set the logging level globally or per logger instance. You can also independently configure console (std_level) and file (file_level or write_config.level) logging levels:

from dm_logger import DMLogger, WriteConfig

# Set global configurations for all logger instances
DMLogger.logging_level = "DEBUG"         # Default fallback level
DMLogger.std_logging_level = "INFO"      # Global console level (default: None -> logging_level)
DMLogger.write_config = WriteConfig(     # Global file config for all loggers
    file_name="main.log",
    write_mode="a",
    max_MB=10,
    level="DEBUG"                        # Global file level
)

# Create logger with independent console and file levels:
# Console only displays INFO and above, while file captures all DEBUG logs!
logger = DMLogger(
    "my_app",
    std_logs=True,
    file_logs=True,
    std_level="INFO",                    # Console receives INFO, WARNING, ERROR, CRITICAL
    file_level="DEBUG"                   # Shorthand: sets write_config.level="DEBUG"
)

# You can also pass a custom WriteConfig per logger instance:
logger2 = DMLogger(
    "analytics",
    file_logs=True,
    write_config=WriteConfig(file_name="analytics.log", level="INFO")
)

# Override general level for a specific logger:
logger3 = DMLogger("app3", level="WARNING")  # Both console and file will use WARNING

Formatting Configuration

FormatterConfig controls which components to show in logs. You can set it either globally for all loggers or individually per logger instance:

from dm_logger import DMLogger, FormatterConfig

formatter_config = FormatterConfig(
    # Default values:
    show_datetime=True,    # Show datetime (DD-MM-YYYY HH:MM:SS.mmm)
    show_level=True,       # Show level [DEBUG], [INFO], etc.
    show_name=True,        # Show logger name [my_app]
    show_location=False    # Show call location (module.function:line)
                           # Always shown for ERROR/CRITICAL!
)

logger = DMLogger(
    "my_app",
    formatter_config=formatter_config  # If not specified, default config is used
)

You can also set the formatter config globally for all loggers:

from dm_logger import DMLogger, FormatterConfig

# Global formatter config for all logger instances
DMLogger.formatter_config = FormatterConfig(
    show_datetime=True,
    show_level=True,
    show_name=False,  # Disable logger name globally
    show_location=False
)

# Create logger with global config
logger1 = DMLogger("app1")  # Will use global config (no logger names shown)

# Override config for specific logger
logger2 = DMLogger(
    "app2",
    formatter_config=FormatterConfig(show_name=True)  # This logger will show its name
)

# Example output from logger1: "01-01-2025 11:22:33.555 [INFO] Message"
# Example output from logger2: "01-01-2025 11:22:33.555 [INFO] [app2] Message"

File Logging Configuration

To enable file logging, set file_logs=True. WriteConfig controls file logger behavior:

from dm_logger import DMLogger, WriteConfig

write_config = WriteConfig(
    # Default values:
    file_name="main.log",  # Log file name
    write_mode="w",        # "w" - new file at startup, "a" - append
    max_MB=5,              # Maximum file size
    max_count=10           # Number of files for rotation
)

logger = DMLogger(
    "my_app",
    file_logs=True,            # Enable file logging
    write_config=write_config  # If not specified, default config is used
)

Log files are stored in the .logs directory in the current working directory. To change directory for all log files:

from dm_logger import DMLogger

# Important: set before creating any loggers
DMLogger.LOGS_DIR_PATH = "path/to/logs"  # New directory for all logs

# Now you can create loggers
logger = DMLogger("my_app")

Multiple Loggers

You can create multiple loggers with different configurations:

from dm_logger import DMLogger, FormatterConfig, WriteConfig

# Console only
console_logger = DMLogger(
    "console",
    level="INFO",       # Different log level
    std_logs=True,      # To console (default)
    file_logs=False     # No file (default)
)

# File only
file_logger = DMLogger(
    "file",
    std_logs=False,     # No console
    file_logs=True,     # With file
    write_config=WriteConfig(
        file_name="background.log",
        write_mode="a"  # Append to file
    )
)

# Split logging: Clean console (INFO+) and full debug trace file (DEBUG+)
service_logger = DMLogger(
    "service",
    std_logs=True,
    file_logs=True,
    std_level="INFO",     # Console only shows INFO, WARNING, ERROR, CRITICAL
    file_level="DEBUG",   # File captures everything including DEBUG
    write_config=WriteConfig(file_name="service.log", write_mode="a")
)

service_logger.debug("Packet trace: #1234")          # Stored in service.log, omitted from console!
service_logger.info("Service started successfully")  # Printed to console AND stored in file!

# With custom formatting
debug_logger = DMLogger(
    "debug",
    formatter_config=FormatterConfig(
        show_datetime=False,  # No datetime
        show_location=True    # Always show call location
    )
)

Log Format

Default format:

01-01-2025 11:22:33.555 [INFO] [my_app] -- message
01-01-2025 11:22:33.555 [ERROR] (main.process:45) -- error message
01-01-2025 11:22:33.555 [ERROR] (utils.calc:10) -- ZeroDivisionError: division by zero

Log components (in order of appearance):

  • Datetime: DD-MM-YYYY HH:MM:SS.mmm
  • Level: [DEBUG], [INFO], [WARNING], [ERROR], [CRITICAL]
  • Logger name: [my_app]
  • Call location: (module.function:line)
    • Always shown for ERROR/CRITICAL
    • For other levels only if show_location=True
  • Additional parameters: {param: value}
  • Message: -- message
    • For exceptions -- ExceptionType: error message

Metadata

Release files for dm-logger 0.6.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dm-logger 0.6.7
File Size Uploaded Provenance Actions
dm_logger-0.6.7.tar.gz 7.0 kB Without provenance Details for dm_logger-0.6.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dm-logger 0.6.7
File Interpreter ABI Platform Actions
dm_logger-0.6.7-py3-none-any.whl Python 3 none any Details for dm_logger-0.6.7-py3-none-any.whl

Total release size: 14.4 kB

Release files / dm_logger-0.6.7.tar.gz

Download URL dm_logger-0.6.7.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
edccadab844abff681e450fdcf59495213e92acb8ccb98e94053f2f87d3bd709
BLAKE2b-256 checksum
How to use checksums
13f50e766995db21eb2358cdd1bf084326db37d9ce63d74362b78ef9027936b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Release files / dm_logger-0.6.7-py3-none-any.whl

Download URL dm_logger-0.6.7-py3-none-any.whl
Size 7.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3b5afff9bbae67cf1a103ae1284a1ee362acf843e02effcbe2294f754967a397
BLAKE2b-256 checksum
How to use checksums
927bd54365701e71f485f692fb03cfad2bb1e1bb74b439e700d45a1a37f0842b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.7

Security for dm-logger 0.6.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page