Simple Log

A simple Python module to allow for simple and straightforward logging from your application.

Installation

Install simple log using pip

$ pip install simple_log

Usage

We try and stay true to the name and make using this module as simple as possible. For the simplest case just use

>>> from simple_log import get_log
>>> my_log = get_log()
>>> my_log.info("This is an information message")
2016.11.10 22:21:51 default INFO:: This is an information message
>>> my_log.error("This is an error message")
2016.11.10 22:22:12 default ERROR:: This is an error message
>>> my_log.warning("This is a warning message")
2016.11.10 23:29:14 default WARNING:: This is a warning message

If you want to have multiple logs just pass a name to get_log

>>> first_log = get_log("first")
>>> first_log.info("Information message to first log")
2016.11.10 22:27:30 first INFO:: Information message to first log
>>> second_log = get_log('two')
>>> second_log.debug("This is a debug message")
2016.11.10 22:28:02 two DEBUG:: This is a debug message

By default the logging level is set to INFO (the standard module defaults to WARNING). See the logging tutorial for information on logging levels. If you would like to change the logging level, for instance to display DEBUG messages use the setLevel method on your log object

>>> from simple_log import get_log
>>> my_log = get_log('test_log')
>>> my_log.debug('This is the first debug message')
...
>>> my_log.setLevel('test_log', 'DEBUG')
>>> my_log.debug('This is the second debug message')
2016.11.10 22:34:55 test_log DEBUG:: This is the second debug message

Writing to a file

If you would like your log messages written to a file as well as the screen use the add_file function

>>> from simple_log import get_log, add_file
>>> my_log = get_log('test_log')
>>> my_log.info('This is an information message')
2016.11.12 22:48:10 test_log INFO:: This is an information message
>>> add_file('test_log', 'test_log.log')
>>> my_log.info('This is another information message')
2016.11.12 22:49:30 test_log INFO:: This is another information message
>>> cat test_log.log
2016.11.12 22:49:30 test_log INFO:: This is another information message

the add_file function tries to be clever and should work if you pass it either the name of a log that was created using get_log or if you pass in the log object itself.

>>> from simple_log import get_log, add_file
>>> my_log = get_log('test_log')
>>> add_file(my_log, 'test_log.log')
>>> my_log.info('Information message to file')
2016.11.23 19:54:10 test_log INFO:: Information message to file
>>> cat test_log.log
2016.11.23 19:54:10 test_log INFO:: Information message to file

Structured logging

This module now includes support for structured logging using the approach described in the logging cookbook

We have included a class which can be used to wrap messages sent to any of the logging methods that will present the output as a Json object. In keeping with the rest of the module this is designed to be simple to use

>>> from simple_log import get_log, StructuredMessage as sm
>>> my_log = get_log('test_log')
>>> my_log.info(sm('Test message with additions', key=1, value=one))
2023.03.08 18:00:48 test_log INFO:: Test message with additions >>> {"key": 1, "value": "one"}

If you already have some key information in a dictionary or other mapping object you can just pass the whole thing

>>> from simple_log import get_log, StructuredMessage as sm
>>> my_log = get_log('test_log')
>>> my_dict = {'id': 1, 'length': 200, 'name': 'block of wood', 'owner': 'carpenter'}
>>> my_log.info(sm('Test with dict', **my_dict))
2023.03.08 18:04:59 test_log INFO:: Test with dict >>> {"id": 1, "length": 200, "name": "block of wood", "owner": "carpenter"}

Code Structure

The only dependency for simple_log is the Python standard library logging module

The structured message class has increased this to also include the json module as well.

This package is made up of one Python module called log_utilities.py within the simple_log module. It has these members

simple_log.get_log(logname=None)

Return a log object called <logname>

If you don’t specify a <logname> your returned object will have the name default

Log objects returned by this function have a logging level of ‘INFO’ and a sensible message output format including the current date and time and then the log level followed by the log message.

simple_log.add_file(log_name, file_name)

Add a file handler to log messages directed to <log_name> to <file_name>

We can accept either a log object or the name of a log object in the <log_name> parameter

simple_log.StructuredMessage(message, /, **kwargs)

Present the logging message and arguments in a structured format

Any keyword arguments will be returned as a json object.