Skip to main content

Autologging - easier logging and tracing for Python classes

http://ninthtest.info/python-autologging/

PyPI version Python version Python implementation License Wheel availability

Introduction

Autologging eliminates boilerplate logging setup code and tracing code, and provides a means to separate application logging from program flow and data tracing.

Python modules that make use of Autologging are cleaner, leaner, and more resilient to changes that would otherwise require updating tracing statements.

Autologging allows for tracing to be configured (and controlled) independently from application logging. Toggle tracing on/off, write trace log records to a separate log, and use different formatting for trace log entries - all via standard Python logging facilities, and without affecting your application logging.

What's in the autologging namespace?

Autologging exposes two decorators and a custom log level:

logged Decorate a class to create a __log member. The logger is named by default to match the dotted-name of the containing class. A function may also be decorated, creating a _log attribute on the function object whose default name matches the containing module. A specifically-named logger may also be passed to the decorator (i.e. logged(my_logger)).

traced Decorate a class to provide automatic method call/return tracing. By default, all class, static, and instance methods are traced (excluding "special" methods, with the exception of __init__ and __call__). As with the logged decorator, the default name of the tracing logger matches the dotted-name of the containing class and may be overridden by passing a specifically-named logger to the decorator. Additionally, this decorator accepts multiple string arguments that explicitly name the methods to be traced (and may even name "special" methods).

Module-level functions may also be traced using this decorator.

New in version 1.2.0: automatic yield/stop tracing of Python generator iterators (if the generator function is traced).

TRACE The autologging.TRACE (level 1) log level is registered with the Python logging module when Autologging is imported so that tracing can be configured and controlled independently of application logging.

Tracing may be disabled entirely by setting the AUTOLOGGING_TRACED_NOOP environment variable or by calling the autologging.install_traced_noop() function.

A brief example

A simple logged and traced class:

 1 import logging
 2 import sys
 3
 4 from autologging import logged, TRACE, traced
 5
 6 @traced
 7 @logged
 8 class Example:
 9
10     def __init__(self):
11         self.__log.info("initialized")
12
13     def backwards(self, *words):
14         for word in words:
15             yield "".join(reversed(word))
16
17
18 if __name__ == "__main__":
19     logging.basicConfig(
20             level=TRACE, stream=sys.stderr,
21             format="%(levelname)s:%(filename)s,%(lineno)d:%(name)s.%(funcName)s:%(message)s")
22     example = Example()
23     for result in example.backwards("spam", "eggs"):
24         print(result)

Logging and tracing output:

$ python example.py
TRACE:example.py,10:__main__.Example.__init__:CALL *() **{}
INFO:example.py,11:__main__.Example.__init__:initialized
TRACE:example.py,11:__main__.Example.__init__:RETURN None
TRACE:example.py,13:__main__.Example.backwards:CALL *('spam', 'eggs') **{}
TRACE:example.py,15:__main__.Example.backwards:RETURN <generator object backwards at 0x7fa534d61eb0>
TRACE:example.py,15:__main__.Example.backwards:YIELD 'maps'
maps
TRACE:example.py,15:__main__.Example.backwards:YIELD 'sgge'
sgge
TRACE:example.py,15:__main__.Example.backwards:STOP

Installation

The easiest way to install Autologging is to use pip:

$ pip install Autologging

Source installation

Clone or fork the repository:

$ git clone https://github.com/mzipay/Autologging.git

Alternatively, download and extract a source .zip or .tar.gz archive from https://github.com/mzipay/Autologging/releases, https://pypi.python.org/pypi/Autologging or https://sourceforge.net/projects/autologging/files/.

Run the test suite and install the autologging module: (make sure you have setuptools installed!)

$ cd Autologging
$ python setup.py test
$ python setup.py install

Binary installation

Download the Python wheel (.whl) or a Windows installer from https://pypi.python.org/pypi/Autologging or https://sourceforge.net/projects/autologging/files/.

(Use pip or wheel to install the .whl.)

Release files for Autologging 1.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Autologging 1.3.2
File Size Uploaded
Autologging-1.3.2.zip 331.7 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for Autologging 1.3.2
File Interpreter ABI Platform
Autologging-1.3.2.win-amd64.exe Details
Autologging-1.3.2-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 953.7 kB

Release files / Autologging-1.3.2.zip

Download URL Autologging-1.3.2.zip
Size 331.7 kB
Tags Source
SHA-256 checksum
How to use checksums
117659584d8aab8cf62046f682f8e57b54d958b8571c737fa8bf15c32937fbb6
BLAKE2b-256 checksum
How to use checksums
06dd2062256827437ba3584b94a595b024bdfab17523b36e4749f220f7b60ec5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.2

Release files / Autologging-1.3.2.win-amd64.exe

Download URL Autologging-1.3.2.win-amd64.exe
Size 608.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e1f91cf65898f9c3063f7ca37aad6f9a7ce1205b83ec89f0ef66381f07bcab4b
BLAKE2b-256 checksum
How to use checksums
969ec7621d21cbc631dc391ba51c17dc05b24c5be1ea682725f540ee06e77493
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.2

Release files / Autologging-1.3.2-py2.py3-none-any.whl

Download URL Autologging-1.3.2-py2.py3-none-any.whl
Size 13.8 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
a14c6950a003e674a8fac03e7de03fb8cd18f7a42caa87ac5152556e8d389ae5
BLAKE2b-256 checksum
How to use checksums
77a873ca572f00e610bc0c36b429396aa431ede8dd05ecfa6fb58a89d72cfc7c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.2

Release history Release notifications | RSS feed

This release

1.3.2 This release

3 release files

1.3.1

3 release files

1.3.0

3 release files

1.2.1

3 release files

1.2.0

3 release files

1.1.0

5 release files

1.0.1

5 release files

1.0.0

4 release files

0.4.0

7 release files

0.3.0

6 release files

0.2.1

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page