Skip to main content

Autologging makes logging and tracing Python classes easy.

Project description

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.)

Download files

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

Source Distribution

Autologging-1.3.1.zip (329.5 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

Autologging-1.3.1.win-amd64.exe (607.7 kB view details)

Uploaded Source

Autologging-1.3.1-py2.py3-none-any.whl (13.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file Autologging-1.3.1.zip.

File metadata

  • Download URL: Autologging-1.3.1.zip
  • Upload date:
  • Size: 329.5 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for Autologging-1.3.1.zip
Algorithm Hash digest
SHA256 170222e2d6db674f37a71d3314dbfb29975baa9d376f8074691fa3b0412399d0
MD5 73e8c1323c917c37b8321b05450765e1
BLAKE2b-256 403d8060c131a6e17d34ae4ac9efa5d8f65a5b04ea2bb16c681eb5c8e4a149cf

See more details on using hashes here.

File details

Details for the file Autologging-1.3.1.win-amd64.exe.

File metadata

  • Download URL: Autologging-1.3.1.win-amd64.exe
  • Upload date:
  • Size: 607.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for Autologging-1.3.1.win-amd64.exe
Algorithm Hash digest
SHA256 6bb9b90ba71b442e99bb8c0856e914c55fa5279026f4fdefb7d84f00f9be9060
MD5 a9fe47f1043c7b97b8c90fdc133e49e6
BLAKE2b-256 3ef7ab0c7ac51f6a22a56121cc414defdad4511c393cc5eafe8e3b60b6d362b8

See more details on using hashes here.

File details

Details for the file Autologging-1.3.1-py2.py3-none-any.whl.

File metadata

  • Download URL: Autologging-1.3.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using 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

File hashes

Hashes for Autologging-1.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8fd6ff71e36dd99b61937c81e0ccaa859f4051d2887f82f72d18a6b2b0a43bd9
MD5 5b8b6dcab4616d3a5d5c6b3750688b50
BLAKE2b-256 bd34fb033bdc30ab3f9646483a1923daf015b90546fa22a3562e6c34cf2d27dc

See more details on using hashes here.

Supported by

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