Quick Start Guide
Get up and running with Apathetic Python Logger in minutes.
Basic Usage
The simplest way to use Apathetic Python Logger is to register a logger name and get a logger instance:
from apathetic_logging import getLogger, registerLogger
# Register your logger
registerLogger("my_app")
# Get the logger instance
logger = getLogger()
# Start logging!
logger.info("Application started")
logger.debug("Debug information")
logger.warning("This is a warning")
logger.error("An error occurred")
Module-Level Convenience Functions
You can also use module-level convenience functions that log to the root logger:
import apathetic_logging
# Standard levels
apathetic_logging.debug("Debug message")
apathetic_logging.info("Info message")
apathetic_logging.warning("Warning message")
apathetic_logging.error("Error message")
apathetic_logging.critical("Critical message")
# Custom levels
apathetic_logging.trace("Trace message")
apathetic_logging.detail("Detail message")
apathetic_logging.brief("Brief message")
These functions automatically ensure the root logger is an apathetic logger and handle configuration if needed.
Log Levels
Apathetic Python Logger supports the following log levels (in order of verbosity):
traceโ Most verbose, for detailed tracingdebugโ Debug informationinfoโ General informational messages (default)warningโ Warning messageserrorโ Error messagescriticalโ Critical errorssilentโ Disables all logging
Setting Log Levels
You can set the log level in several ways:
1. Environment Variable
export LOG_LEVEL=debug
python my_app.py
2. Programmatically
logger.setLevel("debug") # Case-insensitive
logger.setLevel(logging.DEBUG) # Or use logging constants
3. Using Context Manager
Temporarily change the log level for a specific block:
with logger.useLevel("debug"):
logger.debug("This will be shown")
logger.trace("This will also be shown if trace is enabled")
Colorized Output
Apathetic Python Logger automatically detects if your terminal supports colors and enables colorized output by default.
Color Detection
Colors are enabled when:
- Output is a TTY (terminal)
FORCE_COLORenvironment variable is set to1,true, oryes
Colors are disabled when:
NO_COLORenvironment variable is set- Output is redirected to a file or pipe
Manual Control
from apathetic_logging import ApatheticLogging
# Create logger with colors explicitly enabled/disabled
logger = ApatheticLogger("my_app", enable_color=True)
Tag Formatting
Log messages are automatically prefixed with tags:
[TRACE]โ Gray tag[DEBUG]โ Cyan tagโ ๏ธโ Warning emojiโโ Error emoji๐ฅโ Critical emoji
Dual-Stream Handling
Apathetic Python Logger automatically routes log messages to the appropriate stream:
- stdout โ Used for normal output
- stderr โ Used for errors and warnings
This ensures proper separation of output and error streams, which is important for CLI tools.
Advanced Features
Dynamic Log Levels
Log at different levels dynamically:
logger.logDynamic("warning", "This is a warning")
logger.logDynamic(logging.ERROR, "This is an error")
Conditional Exception Logging
Only show full tracebacks when debug mode is enabled:
try:
risky_operation()
except Exception:
logger.errorIfNotDebug("Operation failed")
# Full traceback only shown if debug/trace is enabled
Temporary Level Changes
Use a context manager to temporarily change log levels:
# Only set level if it's more verbose (won't downgrade from trace to debug)
with logger.useLevel("debug", minimum=True):
logger.debug("This will be shown")
Integration with CLI Tools
For command-line applications, you can integrate with argparse:
import argparse
from apathetic_logging import getLogger, registerLogger
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--log-level", default="info")
args = parser.parse_args()
registerLogger("my_cli")
logger = getLogger()
# Logger will automatically use args.log_level
level = logger.determineLogLevel(args=args)
logger.setLevel(level)
logger.info("CLI tool started")
Drop-in Replacement for stdlib logging
Apathetic Python Logger can be used as a drop-in replacement for Pythonโs standard library logging module:
# Instead of: import logging
import apathetic_logging as logging
# Works just like stdlib logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("my_app") # Note: getLogger is CamelCase in stdlib
logger.info("Hello, world!")
logger.debug("Debug message")
logger.warning("Warning message")
Note: When using
getLogger(None), the logger name is auto-inferred from the calling module (improved behavior). To get the root logger, usegetLogger("")instead. For stdlib-compatible behavior wheregetLogger(None)returns the root logger, enable Compatibility Mode.
Next Steps
- Read the API Reference for complete documentation
- Check out Examples for more advanced patterns
- See Contributing if you want to help improve the project