An open API service indexing awesome lists of open source software.

https://github.com/healkeiser/fxlog

A custom logging module for Python that supports colorized output and log file rotation.
https://github.com/healkeiser/fxlog

logging python utility

Last synced: 4 months ago
JSON representation

A custom logging module for Python that supports colorized output and log file rotation.

Awesome Lists containing this project

README

          

![Logo](https://raw.githubusercontent.com/healkeiser/fxlog/main/fxlog/images/icons/fxlog_logo_background_dark.svg#gh-light-mode-only)
![Logo](https://raw.githubusercontent.com/healkeiser/fxlog/main/fxlog/images/icons/fxlog_logo_background_light.svg#gh-dark-mode-only)

fxlog


A custom logging module for Python that supports colorized output and log file rotation.




##



  


License: MIT  

Build   


PyPI version
  


PyPI Downloads
  

Last Commit  


  

GitHub Stars  

## Table of Contents

- [About](#about)
- [Installation](#installation)
- [How-to Use](#how-to-use)
- [Save Log Files](#save-log-files)
- [Do Not Save Log Files](#do-not-save-log-files)
- [Set Log Level](#set-log-level)
- [Set Formatter](#set-formatter)
- [Contact](#contact)

## 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](https://pypi.org/project/fxlog) and can be installed via `pip`:

```shell
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](#footnote1), import the `fxlog` module, and set the log directory where log files will be stored:

```python
from fxlog import fxlogger

fxlogger.set_log_directory('path/to/log/directory')
```

E.g.,

```python
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:

```python
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:

```python
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:

```python
from fxlog import fxlogger

fxlogger.clear_logs()
```

> [!NOTE]
> 1 The log files are constructed with the following naming convention: `_--.log`.

### Do Not Save Log Files

If you don't want to save the log files, you can use the `fxlog` module as follows:

```python
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:

```python
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:

```python
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_color` is set to `True` but 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`:

```python
from fxlog import fxlogger

logger = fxlogger.configure_logger('my_logger', enable_separator=True)
logger.debug('This is a debug message')
```



## Contact

Project Link: [fxlog](https://github.com/healkeiser/fxlog)




GitHub
  


LinkedIn
  


Behance
  


Twitter
  


Instagram
  


Gumroad
  


Email
  


Buy Me A Coffee