Skip to main content

Easy python logging wrapper to use best pracitces, reduce code and add additional fields

Project description

Log Plus

https://img.shields.io/pypi/v/logplus.svg https://img.shields.io/travis/jmrichardson/logplus.svg Documentation Status

Easy python logging wrapper to use best pracitces, reduce code and add additional fields.

Log Plus overview:

  • Provide permanent and ephimeral context fields in each log entry
  • Provide a simple way to consistently log with best practices
  • Avoid cumbersome boiler plate code across each application
  • Configure python logging using a simple YAML file
  • Simple and clean usage
  • Free software: MIT license
  • Documentation: https://logplus.readthedocs.io.
  • Python >= 3.5

Features

Inspired by Python logging best practices with JSON steirods.

  • Utilizes standard python loggging
  • Uses YAML logging configuration file
  • Logs in JSON format
  • Add permanent and ephemeral context fields

Quick Start

Install logplus

pip install logplus

Import logplus package

import logplus

Define logger with log file location and permanent context fields. Permanent context fields are static and will be added to all log entries. For example, you may want to log the arguments provided to the script as well as an instance identifier (uuid)

log = logplus.setup('test.log', arg1=arg1, uuid=uuid)

Add a log entry. The log entry will contain the previous arguments as well as the field “count” with the value of count.

log.info("Application message", count=count)

Here is a simple application example test.py

import logplus

arg1 = "argument1"
uuid = "23RS2F"
log = logplus.setup('test.log', arg1=arg1, uuid=uuid)
count = 1
log.info("Application message: with count", count=count)
log.info("Application message: no count")

Which resulted in the following log entries in “test.log”:

{"asctime": "2018-08-22 16:51:28,794", "filename": "test.py", "lineno": 7, "levelname": "INFO", "message": "Application message: with count", "arg1": "argument1", "uuid": "23RS2F", "count": 1}
{"asctime": "2018-08-22 16:51:28,795", "filename": "test.py", "lineno": 8, "levelname": "INFO", "message": "Application message: no count", "arg1": "argument1", "uuid": "23RS2F"}

Usage

Log Plus requires a simple configuration command to set the log file location, optional logging configurational file and any context fields to be added to all log entries:

logplus.setup(log_file, [config_file], [static_args]...)
  • log_file: Path of log file to write to
  • config_file: Optional logging configuration file in YAML format
  • static_args: Key value arguments to be included in all log entries

Note that if a configuration file is not supplied, it will use the following configuration:

---
    version: 1
    disable_existing_loggers: False
    formatters:
    simple:
            format: "%(asctime)s - %(levelname)s - %(message)s"
    json:
            format: "%(asctime)s %(filename)s %(lineno)s %(levelname)s %(message)s"
            class: pythonjsonlogger.jsonlogger.JsonFormatter

    handlers:
    console:
            class: logging.StreamHandler
            level: DEBUG
            formatter: simple
            stream: ext://sys.stdout

    file_handler:
            class: logging.handlers.RotatingFileHandler
            formatter: json
            maxBytes: 10485760 # 10MB
            backupCount: 20
            encoding: utf8

    root:
    level: INFO
    handlers: [console, file_handler]
    ...

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

History

0.1.0 (2018-08-20)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for logplus, version 0.1.15
Filename, size File type Python version Upload date Hashes
Filename, size logplus-0.1.15-py2.py3-none-any.whl (5.2 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page