This is my custom logger
Project description
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 either globally for all loggers or individually per logger instance:
from dm_logger import DMLogger
# Set global logging level for all logger instances
DMLogger.logging_level = "INFO" # Default is "DEBUG"
# Create logger with global level
logger1 = DMLogger("app1") # Will use global level (INFO)
# Override level for specific logger
logger2 = DMLogger("app2", level="DEBUG") # This logger will use DEBUG level
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
)
)
# 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
- For exceptions
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dm_logger-0.6.6.tar.gz.
File metadata
- Download URL: dm_logger-0.6.6.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
190b2709ad67dec828fdd7fd7ad865375f8137fa0b4ca2cdc06cb64670ae11f9
|
|
| MD5 |
eefce25d28aa360718ce05a23d46774d
|
|
| BLAKE2b-256 |
70498b40262f0d504d021c1930cadf9b75e26d90cf4113806a60465fa1d12f57
|
File details
Details for the file dm_logger-0.6.6-py3-none-any.whl.
File metadata
- Download URL: dm_logger-0.6.6-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fc402bd463218c6a9eaeb23b956187ed1190252ebf2e8d2de7cd15e153658c74
|
|
| MD5 |
ce41cfd2351c24dbd9fdd46257595d1c
|
|
| BLAKE2b-256 |
7317a1e542651e95ab353e2c0f68f63d3209b6a5cf0643360b9fafd14e5dd556
|