log_safe is a Python library that provides safe and efficient logging capabilities for multiprocessing applications. It ensures that logging is thread-safe and process-safe, making it ideal for complex, multi-process Python applications
Project description
log_safe
log_safe
is a Python library that provides safe and efficient logging capabilities for multiprocessing applications. It ensures that logging is thread-safe and process-safe, making it ideal for complex, multi-process Python applications.
Features
- Thread-safe and process-safe logging
- Centralized log collection from multiple processes
- Customizable logging configurations for both listener and worker processes
- Automatic initialization of logging in child processes
- Watchdog mechanism to prevent hanging listener process daemons
- Seamless integration with Python's built-in
multiprocessing
andconcurrent.futures
modules
Quick Start
Here's a simple example of how to use log_safe
:
from log_safe import initialize_safe_logging
import logging
import multiprocessing
initialize_safe_logging()
def worker_function():
logger = logging.getLogger()
logger.info("This is a log message from a worker process")
if __name__ == "__main__":
logger = logging.getLogger()
logger.info("Starting main process")
with multiprocessing.Pool(processes=2) as pool:
pool.apply_async(worker_function)
pool.apply_async(worker_function)
logger.info("Main process completed")
Usage
- Initialize logging before using any multiprocessing features:
from log_safe import initialize_safe_logging
initialize_safe_logging()
- Use with
multiprocessing.Pool
,ProcessPoolExecutor
, ormultiprocessing.Process
as you normally would.log_safe
automatically patches these to ensure proper logging initialization in all child processes.
Custom Configuration
You can provide custom configurations for both the listener and worker processes:
from log_safe import initialize_safe_logging
listener_config = {
# ... listener configuration ...
}
worker_config = {
# ... worker configuration ...
}
initialize_safe_logging(listener_config, worker_config)
Considerations
- Always call
initialize_safe_logging()
in the main process before creating any child processes. - The library uses global variables to maintain the logging state. Be cautious when modifying global state in your application.
- The watchdog mechanism will shut down the logging system if it's idle for too long (default is 10 hours). Adjust this if needed for long-running applications.
For more detailed information, use cases, and advanced usage, please refer to the full documentation on GitHub.
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
File details
Details for the file log_safe-0.1.2.tar.gz
.
File metadata
- Download URL: log_safe-0.1.2.tar.gz
- Upload date:
- Size: 9.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd31fd1983dab4eaa911153027a5cc95dbd0f2c4757eb60a6d8e12b3ff922433 |
|
MD5 | 7522dbd0a72e01055769ff56548a3548 |
|
BLAKE2b-256 | ac20e1945b1c5eb1166fada97664e5ae6b0392303f08784b50fb1411c70a2f3c |
File details
Details for the file log_safe-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: log_safe-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.11.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 56b2aae942cf54b926fa51bb6cd2ab0bf4dce02a1bb554e3a68634643f260b4f |
|
MD5 | ff92df85417e536f82e3149eccfe1947 |
|
BLAKE2b-256 | 49c0326b975ca659897d4c5ecbaa596051396c40a45041a2f97f90c301320174 |