Skip to main content

Loguru based custom logging package (beans_logging) for python projects.

Project description

Python Logging (beans_logging)

Loguru based custom logging package (beans_logging) for python projects.

Features

  • Loguru based logging - https://pypi.org/project/loguru
  • Custom basic logging module
  • Logging to files (all, error, json)
  • Custom logging formats
  • Custom options as a config
  • Colorful logging
  • Multiprocess compatibility (Linux, macOS - 'fork', Windows - 'spawn')

Installation

1. Prerequisites

  • Python (>= v3.7)
  • PyPi (>= v21)

2. Install beans-logging

A. [RECOMMENDED] PyPi install

# Install or upgrade beans-logging package:
pip install --upgrade beans-logging

# To uninstall package:
pip uninstall -y beans-logging

B. Manually add to PYTHONPATH (Recommended for development)

# Clone repository by git:
git clone https://github.com/bybatkhuu/python_logging.git beans_logging
cd beans_logging

# Install python dependencies:
pip install --upgrade pip
cat requirements.txt | xargs -n 1 -L 1 pip install --no-cache-dir

# Add current path to PYTHONPATH:
export PYTHONPATH="${PWD}:${PYTHONPATH}"

C. Manually compile and setup (Not recommended)

# Clone repository by git:
git clone https://github.com/bybatkhuu/python_logging.git beans_logging
cd beans_logging

# Building python package:
pip install --upgrade pip setuptools wheel
python setup.py build
# Install python dependencies with built package to current python environment:
python setup.py install --record installed_files.txt

# To remove only installed beans-logging package:
head -n 1 installed_files.txt | xargs rm -vrf
# Or to remove all installed files and packages:
cat installed_files.txt | xargs rm -vrf

3. Configuration (You can skip this step, if you don't want to configure)

cp -v .env.example [PROJECT_DIR]/.env
cd [PROJECT_DIR]
vi .env
mkdir -vp [PROJECT_DIR]/configs

cp -v logger.yml [PROJECT_DIR]/configs/logger.yml
rm -vf logger.yml
cd [PROJECT_DIR]
vi configs/logger.yml

Usage/Examples

Simple example

sample.py:

from beans_logging import logger

logger.info('Logging info.')
logger.success('Success.')
logger.warning('Warning something.')
logger.error('Error occured.')
logger.critical('CRITICAL ERROR.')


def divide(a, b):
    _result = a / b
    return _result

try:
    divide(10, 0)
except Exception as err:
    logger.exception("Failed to divide:")

Advanced example

configs/logger.yml:

logger:
    level: "TRACE"
    use_color: true
    use_icon: false
    use_backtrace: true
    std_format_str: "[<c>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</c> | <level>{lvlname:<5}</level> | <w>{file}</w>:<w>{line}</w>]: <level>{message}</level>"
    use_log_file: true
    logs_dir: ./logs
    file_format_str: "[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {lvlname:<5} | {file}:{line}]: {message}"
    rotate_when:
        at_hour: 0
        at_minute: 0
        at_second: 0
    rotate_file_size: 10000000  # 10MB
    backup_file_count: 50
    file_encoding: utf8
    all_log_filename: "{app_name}.std.all.log"
    err_log_filename: "{app_name}.std.err.log"
    use_log_json: true
    use_custom_json: true
    json_all_log_filename: "{app_name}.json.all.log"
    json_err_log_filename: "{app_name}.json.err.log"

.env:

ENV=development
DEBUG=true

APP_NAME=app
PY_LOGS_DIR="./logs"

main.py:

from beans_logging import logger

logger.trace('Tracing...')
logger.debug('Debugging...')
logger.info('Logging info.')
logger.success('Success.')
logger.warning('Warning something.')
logger.error('Error occured.')
logger.critical('CRITICAL ERROR.')


def divide(a, b):
    _result = a / b
    return _result

def nested(c):
    try:
        divide(5, c)
    except ZeroDivisionError:
        logger.exception("Show me, what value is wrong:")

nested(0)

Running Tests

To run tests, run the following command:

python -m unittest tests/test_*.py

Environment Variables

You can use the following environment variables inside .env file:

ENV=development
DEBUG=true
APP_NAME=app
PY_LOGS_DIR="/var/log/app"

Configuration

You can use the following sample configuration:

logger:
    level: "INFO"
    use_color: true
    use_icon: false
    use_backtrace: true
    std_format_str: "[<c>{time:YYYY-MM-DD HH:mm:ss.SSS Z}</c> | <level>{lvlname:<5}</level> | <w>{file}</w>:<w>{line}</w>]: <level>{message}</level>"
    use_log_file: false
    logs_dir: ./logs
    file_format_str: "[{time:YYYY-MM-DD HH:mm:ss.SSS Z} | {lvlname:<5} | {file}:{line}]: {message}"
    rotate_when:
        at_hour: 0
        at_minute: 0
        at_second: 0
    rotate_file_size: 10000000  # 10MB
    backup_file_count: 50
    file_encoding: utf8
    all_log_filename: "{app_name}.std.all.log"
    err_log_filename: "{app_name}.std.err.log"
    use_log_json: false
    use_custom_json: false
    json_all_log_filename: "{app_name}.json.all.log"
    json_err_log_filename: "{app_name}.json.err.log"

References

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

beans_logging-1.0.0.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

beans_logging-1.0.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file beans_logging-1.0.0.tar.gz.

File metadata

  • Download URL: beans_logging-1.0.0.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for beans_logging-1.0.0.tar.gz
Algorithm Hash digest
SHA256 f9f61291348991abe2af9e572b82b6bb1b7385a990902e8ced55e7daaa979f03
MD5 953b44f57d308a5d43144d1fcc485f58
BLAKE2b-256 d6f96ca4bacebaef3bf699b1f2cbedbb37b3eca443257d7ef20f9d68b856a538

See more details on using hashes here.

File details

Details for the file beans_logging-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for beans_logging-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bbfe0f46dda9720b9c06dd87c38aa64b83bc68efd9e63da39bfc1772f9abbf37
MD5 ad9e844d49a4edeecd467925c2f43f36
BLAKE2b-256 71ca39c5c432060c6611062d2c9adda9f14425eb8502bd7318b880e9f938604f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page