Rockwool logger
Project description
rocklogger
A simple and flexible logging utility for Python applications with singleton pattern support.
PyPI
https://pypi.org/project/rocklogger/
Installation
pip install rocklogger
Usage
Basic Usage
from rocklogger import Rocklogger
# Initialize the logger using the singleton pattern
# level can be 'info', 'debug', 'warning', or 'error'
logger = Rocklogger.get_instance(level='debug', use_date_in_filename=True).get_logger()
# Log messages at different levels
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
Log Files
The logger creates two log files in a 'logs' directory in the same location as your script:
your_script_name_YYYYMMDD.log- Contains all log messagesyour_script_name_error_YYYYMMDD.log- Contains only error level messages
If use_date_in_filename is set to False, the date will not be included in the filename.
Logging Exceptions
try:
# Some code that might raise an exception
result = 10 / 0
except Exception as e:
logger.error(f"An error occurred: {e}", exc_info=True)
Singleton Pattern
Rocklogger implements the singleton pattern, ensuring that only one logger instance exists across your application:
# In your main script
from rocklogger import Rocklogger
logger = Rocklogger.get_instance(level='debug').get_logger()
# In another module
from rocklogger import Rocklogger
# This will use the same instance created in the main script
logger = Rocklogger.get_instance().get_logger()
Benefits:
- Ensures consistent logging configuration across your application
- Log files are named after the script that first created the logger
- Prevents multiple log file handlers from being created
Important Note About Process Isolation
The singleton pattern works within a single Python process. When modules are imported within the same process, they will share the same logger instance:
# main.py
from rocklogger import Rocklogger
logger = Rocklogger.get_instance(level='debug').get_logger()
logger.info('Main script')
import module_a # Will use the same logger instance
import module_b # Will also use the same logger instance
However, if you run separate Python scripts as independent processes, each process will have its own singleton instance:
# These will create separate logger instances
python script1.py
python script2.py
For applications with multiple entry points, consider creating a central logging module that's imported by all other modules.
Closing the Logger
When you're done with the logger, you can close it (optional):
# This is automatically done when the Rocklogger instance is garbage collected
logger.close()
Resetting the Logger (for testing)
If you need to reset the logger (mainly for testing purposes):
Rocklogger.reset()
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
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 rocklogger-0.5.tar.gz.
File metadata
- Download URL: rocklogger-0.5.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a55c5e75b76351f061f7cf9008587058cc4bf73036cf390bc1de510d4823fac
|
|
| MD5 |
aaa6071855d8c046f409c22c075a6f69
|
|
| BLAKE2b-256 |
84f80787ffa1f8147240ac1dd24c4a62e323c19b21e054cba3edc4335cb342b4
|
File details
Details for the file rocklogger-0.5-py3-none-any.whl.
File metadata
- Download URL: rocklogger-0.5-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b58421db786bc87d379c4fd8123ec3f6ec11856cebdb3a8e59be8619a2bc01e2
|
|
| MD5 |
e2afde97f30a61c089e220a256157f19
|
|
| BLAKE2b-256 |
daa29ab0b73460a368560e207acc2a5124bb4299281d53cff1b77ae5ab5cd774
|