Reference#

pylogformats#

Easy to Use Formatter Classes for the Python logging module.

Availiable JSON Formatters:

Example Advanced Json Log#
 1{
 2    "logger": "root",
 3    "timestamp": "2021-02-04T23:02:52.522958",
 4    "rtimestamp": "2021-02-04T23:02:37.518800",
 5    "message": "TEST",
 6    "level": "ERROR",
 7    "levelno": 40,
 8    "location": {
 9        "pathname": "<FULL_PATH>\\test_logger.py",
10        "module": "test_logger",
11        "filename": "test_logger.py",
12        "function": "<module>",
13        "line": 16
14    },
15    "process": {
16        "number": 2300,
17        "name": "MainProcess"
18    },
19    "thread": {
20        "number": 12516,
21        "name": "MainThread"
22    },
23    "v": 1
24}

Bunyan Specification

Example Bunyan Json Log#
1{
2    "time": "2021-02-04T23:01:00.781Z",
3    "name": "root",
4    "pid": 15504,
5    "level": 40,
6    "msg": "TEST",
7    "hostname": "HerculesPC",
8    "v": 0
9}
Example Json Log#
 1{
 2    "logger": "root",
 3    "timestamp": "2021-02-04T23:01:46.435011",
 4    "message": "TEST",
 5    "level": "ERROR",
 6    "levelno": 40,
 7    "function": "<module>",
 8    "process": {
 9        "number": 13316,
10        "name": "MainProcess"
11    },
12    "thread": {
13        "number": 10704,
14        "name": "MainThread"
15    },
16    "v": 1
17}

Availiable Text Formatters:

Example Text Log#
1[DEBUG] [2021-02-04 23:01:46] A Test Debug Log
Example Text Log#
1[D 2021-02-04 23:01:46 l:root f:<module> ln:5] A Test Log [includesExtras:Yes]
pylogformats.AdvJSONFormat#

alias of AdvJsonFormat

class pylogformats.AdvJsonFormat(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)#

A formatter for an opinionated Advanced Json format.

Extends the logging.Formatter class to correctly format a log record using this format.

Example Usage:

>>> import logging
>>> import sys
>>> import pylogformats
>>>
>>> # Setup the Stream Handler Using AdvJsonFormat
>>> stream_handler = logging.StreamHandler(sys.stdout)
>>> stream_handler.setFormatter(pylogformats.AdvJsonFormat())
>>>
>>> # Setup the logging config using the stream hander and the DEBUG logging level
>>> logging.basicConfig(handlers=[stream_handler], level=logging.DEBUG)
>>>
>>> # The test log
>>> logging.debug("Test Log")
{"logger": "root", "timestamp": ..., "rtimestamp": ..., "message": "Test Log", "level": "DEBUG", "levelno": 10, "location": {"pathname": ..., "module": ..., "filename": ..., "function": ..., "line": ...}, "process": {"number": ..., "name": "MainProcess"}, "thread": {"number": ..., "name": "MainThread"}, "v": 1}
>>>
>>> logging.debug(
...     "Test Log With Extra",
...     extra={"whatami": "An Extra"}
... )
{"logger": "root", "timestamp": ..., "rtimestamp": ..., "message": "Test Log With Extra", "level": "DEBUG", "levelno": 10, "location": {"pathname": ..., "module": ..., "filename": ..., "function": ..., "line": ...}, "process": {"number": ..., "name": "MainProcess"}, "thread": {"number": ..., "name": "MainThread"}, "v": 1, "whatami": "An Extra"}
>>>
>>> # Clean up Logging
>>> logging.getLogger().removeHandler(stream_handler)

In some lines of the example code, there is an output of a sample log. This log shows some values as an ellipsis (…). This is because it is a Doctest codeblock which helps ensure code examples are up-to-date when code changes.

Please be assured that these ellipsis are not present when formatting the log and the keys that use them will have real, accurate values in them.

format(record)#

Format LogRecords into an advanced JSON log format.

Parameters

record (logging.LogRecord) – An instance of logging.LogRecord which contains all relevant information about the event being logged.

Returns

A string representation of the Bunyan formatted logging event.

Return type

str

class pylogformats.BunyanFormat(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)#

A Simple Bunyan JSON Formatter.

Extends the logging.Formatter class to correctly format a log record as bunyan.

Example Usage:

>>> import logging
>>> import sys
>>> import pylogformats
>>>
>>> # Setup the Stream Handler Using Bunyan
>>> stream_handler = logging.StreamHandler(sys.stdout)
>>> stream_handler.setFormatter(pylogformats.BunyanFormat())
>>>
>>> # Setup the logging config using the stream hander and the DEBUG logging level
>>> logging.basicConfig(handlers=[stream_handler], level=logging.DEBUG)
>>>
>>> # The test log
>>> logging.debug("Test Log")
{"time": ..., "name": "root", "pid": ..., "level": 10, "msg": "Test Log", "hostname": ..., "v": 0}
>>>
>>> logging.debug(
...     "Test Log With Extra",
...     extra={
...         "whatami": "An Extra"
...     }
... )
{"time": ..., "name": "root", "pid": ..., "level": 10, "msg": "Test Log With Extra", "hostname": ..., "v": 0, "whatami": "An Extra"}
>>> # Clean up Logging
>>> logging.getLogger().removeHandler(stream_handler)

In some lines of the example code, there is an output of a sample log. This log shows some values as an ellipsis (…). This is because it is a Doctest codeblock which helps ensure code examples are up-to-date when code changes.

Please be assured that these ellipsis are not present when formatting the log and the keys that use them will have real, accurate values in them.

format(record)#

Format LogRecords into a Bunyan JSON String.

Parameters

record (logging.LogRecord) – An instance of logging.LogRecord which contains all relevant information about the event being logged.

Returns

A string representation of the Bunyan formatted logging event.

Return type

str

formatTime(record, datefmt=None)#

Format LogRecord Times into the correct formatted string String.

Parameters
  • record (logging.LogRecord) – An instance of logging.LogRecord which contains all relevant information about the event being logged.

  • datefmt (str | None) – A valid datetime.strftime() format string.

Returns

A string representation of the Bunyan formatted logging event.

Return type

str

pylogformats.CompactFormat#

alias of CompactTextFormat

class pylogformats.CompactTextFormat(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)#

A formatter for an opinionated Compact Text format.

Extends the logging.Formatter class to correctly format a log record using this format.

Example Usage:

>>> import logging
>>> import sys
>>> import pylogformats
>>>
>>> # Setup the Stream Handler Using CompactTextFormat
>>> stream_handler = logging.StreamHandler(sys.stdout)
>>> stream_handler.setFormatter(pylogformats.CompactTextFormat())
>>>
>>> # Setup the logging config using the stream hander and the DEBUG logging level
>>> logging.basicConfig(handlers=[stream_handler], level=logging.DEBUG)
>>>
>>> # The test log
>>> logging.debug("Test Log")
[D ... ... l:root f:... ln:...] Test Log
>>>
>>> logging.debug(
...     "Test Log With Extra",
...     extra={"whatami": "An Extra"}
... )
[D ... ... l:root f:... ln:...] Test Log With Extra [whatami:An Extra]
>>> # Clean up Logging
>>> logging.getLogger().removeHandler(stream_handler)

In some lines of the example code, there is an output of a sample log. This log shows some values as an ellipsis (…). This is because it is a Doctest codeblock which helps ensure code examples are up-to-date when code changes.

Please be assured that these ellipsis are not present when formatting the log and the keys that use them will have real, accurate values in them.

format(record)#

Format LogRecords into an Compact Text log format.

Parameters

record (logging.LogRecord) – An instance of logging.LogRecord which contains all relevant information about the event being logged.

Returns

A string representation of the Bunyan formatted logging event.

Return type

str

pylogformats.JSONFormat#

alias of JsonFormat

class pylogformats.JsonFormat(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)#

A formatter for an opinionated Json format.

Extends the logging.Formatter class to correctly format a log record using this format.

Example Usage:

>>> import logging
>>> import sys
>>> import pylogformats
>>>
>>> # Setup the Stream Handler Using JsonFormat
>>> stream_handler = logging.StreamHandler(sys.stdout)
>>> stream_handler.setFormatter(pylogformats.JsonFormat())
>>>
>>> # Setup the logging config using the stream hander and the DEBUG logging level
>>> logging.basicConfig(handlers=[stream_handler], level=logging.DEBUG)
>>>
>>> # The test log
>>> logging.debug("Test Log")
{"logger": "root", "timestamp": ..., "message": "Test Log", "level": "DEBUG", "levelno": 10, "function": "<module>", "process": {"number": ..., "name": "MainProcess"}, "thread": {"number": ..., "name": "MainThread"}, "v": 1}
>>>
>>> logging.debug(
...     "Test Log With Extra",
...     extra={"whatami": "An Extra"}
... )
{"logger": "root", "timestamp": ..., "message": "Test Log With Extra", "level": "DEBUG", "levelno": 10, "function": "<module>", "process": {"number": ..., "name": "MainProcess"}, "thread": {"number": ..., "name": "MainThread"}, "v": 1, "whatami": "An Extra"}
>>> # Clean up Logging
>>> logging.getLogger().removeHandler(stream_handler)

In some lines of the example code, there is an output of a sample log. This log shows some values as an ellipsis (…). This is because it is a Doctest codeblock which helps ensure code examples are up-to-date when code changes.

Please be assured that these ellipsis are not present when formatting the log and the keys that use them will have real, accurate values in them.

format(record)#

Format LogRecords into an simplified JSON log format.

Parameters

record (logging.LogRecord) – An instance of logging.LogRecord which contains all relevant information about the event being logged.

Returns

A string representation of the Bunyan formatted logging event.

Return type

str

pylogformats.SimpleFormat#

alias of SimpleTextFormat

class pylogformats.SimpleTextFormat(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)#

A formatter for an opinionated Compact Text format.

Extends the logging.Formatter class to correctly format a log record using this format.

Example Usage:

>>> import logging
>>> import sys
>>> import pylogformats
>>>
>>> # Setup the Stream Handler Using SimpleTextFormat
>>> stream_handler = logging.StreamHandler(sys.stdout)
>>> stream_handler.setFormatter(pylogformats.SimpleTextFormat())
>>>
>>> # Setup the logging config using the stream hander and the DEBUG logging level
>>> logging.basicConfig(handlers=[stream_handler], level=logging.DEBUG)
>>>
>>> # The test log
>>> logging.debug("Test Log")
[DEBUG ... ...] Test Log
>>>
>>> logging.debug(
...     "Test Log With Extra",
...     extra={"whatami": "An Extra"}
... )
[DEBUG ... ...] Test Log With Extra
>>> # Clean up Logging
>>> logging.getLogger().removeHandler(stream_handler)

In some lines of the example code, there is an output of a sample log. This log shows some values as an ellipsis (…). This is because it is a Doctest codeblock which helps ensure code examples are up-to-date when code changes.

Please be assured that these ellipsis are not present when formatting the log and the keys that use them will have real, accurate values in them.

format(record)#

Format LogRecords into an simplified Text log format.

Parameters

record (logging.LogRecord) – An instance of logging.LogRecord which contains all relevant information about the event being logged.

Returns

A string representation of the Bunyan formatted logging event.

Return type

str