A custom logging module for Python that supports colorized output and log file rotation. Includes features such as configurable log levels, custom formatters, and automatic deletion of old log files.
Project description
Table of Contents
About
A custom logging module for Python that supports colorized output and log file rotation. Includes features such as configurable log levels, custom formatters, and automatic deletion of old log files.
Installation
The package is available on PyPI and can be installed via pip:
python -m pip install fxlog
How-to Use
You can use the fxlog module in your Python scripts as follows:
Save Log Files
If you want to save the log files 1, import the fxlog module, and set the log directory where log files will be stored:
from fxlog import fxlogger
fxlogger.set_log_directory('path/to/log/directory')
E.g.,
import os
from pathlib import Path
from fxlog import fxlogger
_PACKAGE_NAME = "package_name"
DATA_DIR = (
Path(os.getenv("APPDATA")) / _PACKAGE_NAME
if os.name == "nt"
else Path.home() / f".{_PACKAGE_NAME}"
)
LOG_DIR = DATA_DIR / "logs"
LOG_DIR.mkdir(parents=True, exist_ok=True)
fxlogger.set_log_directory(LOG_DIR)
[!NOTE] This only needs to be done once in your package.
Then, you can use the fxlog module to create a logger object and log messages to the console and a log file:
from fxlog import fxlogger
logger = fxlogger.configure_logger('my_logger')
logger.debug('This is a debug message')
To delete old log files, you can use the fxlog module as follows:
from fxlog import fxlogger
fxlogger.delete_old_logs(7) # Delete log files older than 7 days
You can also clear all log files in the log directory:
from fxlog import fxlogger
fxlogger.clear_logs()
[!NOTE] 1 The log files are constructed with the following naming convention:
<logger_name>_<year>-<month>-<day>.log.
Do Not Save Log Files
If you don't want to save the log files, you can use the fxlog module as follows:
from fxlog import fxlogger
logger = fxlogger.configure_logger('my_logger', save_to_file=False)
logger.debug('This is a debug message')
Set Log Level
You can set the log level of all loggers by using the set_loggers_level function:
from fxlog import fxlogger
fxlogger.set_loggers_level(fxlogger.DEBUG) # You can also use `logging.DEBUG`
Set Formatter
By default, the output looks like this:
You can enable a colored output by setting the enable_color parameter to True. The messages will be colorized according to their log levels:
from fxlog import fxlogger
logger = fxlogger.configure_logger('my_logger', enable_color=True)
logger.debug('This is a debug message')
[!NOTE] Colors are not saved in log files.
[!WARNING] If
enable_coloris set toTruebut the terminal does not support colorized output, the messages will be displayed in their original form.
You can also enable a separator between log messages by setting the enable_separator parameter to True:
from fxlog import fxlogger
logger = fxlogger.configure_logger('my_logger', enable_separator=True)
logger.debug('This is a debug message')
Contact
Project Link: fxlog
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 fxlog-0.2.1.tar.gz.
File metadata
- Download URL: fxlog-0.2.1.tar.gz
- Upload date:
- Size: 1.7 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a84e2bb80e31ca3f17325ef8a7b0de8f3831750fe10bb080e3d21b90393721b7
|
|
| MD5 |
80130d92f3add0a0b0537b2184677242
|
|
| BLAKE2b-256 |
0edde3e43e65520a3864819fe80a5ead1499b88de1e5b82d6cb14a8b44256817
|
File details
Details for the file fxlog-0.2.1-py3-none-any.whl.
File metadata
- Download URL: fxlog-0.2.1-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9f2424451e3348a4182359f4d5aeef207bc69004d4d6cf36d715c3a8bc93be33
|
|
| MD5 |
887a6af61f86614288d2355fea9548e4
|
|
| BLAKE2b-256 |
fe130d0055586df6acb9c8cd4e546a53acaf0d38281ed63bc2ceefca4c1897c2
|