{"id":6529,"library":"autologging","title":"Autologging","description":"Autologging (version 1.3.2) is a Python library that simplifies logging and tracing for classes and functions by eliminating boilerplate code. It provides decorators (`@logged` and `@traced`) to automatically inject a logger or trace method calls, respectively. It works with Python's standard `logging` module, allowing for independent configuration and control of application logging and program flow tracing. The library supports Python 2.7 and Python 3.3+.","status":"active","version":"1.3.2","language":"en","source_language":"en","source_url":"https://github.com/mzipay/Autologging","tags":["logging","tracing","decorator","automation","stdlib"],"install":[{"cmd":"pip install Autologging","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"logged","correct":"from autologging import logged"},{"symbol":"traced","correct":"from autologging import traced"},{"note":"A custom log level (value 1) for tracing, lower in severity than `logging.DEBUG`.","symbol":"TRACE","correct":"from autologging import TRACE"}],"quickstart":{"code":"import logging\nimport sys\nfrom autologging import logged, traced, TRACE\n\n# Configure basic logging to see the output\nlogging.basicConfig(\n    level=logging.DEBUG,\n    stream=sys.stdout,\n    format=\"%(levelname)s:%(name)s:%(funcName)s:%(message)s\"\n)\n# Set root logger level to TRACE to see tracing output\nlogging.getLogger().setLevel(TRACE)\n\n@traced # Apply @traced outermost for consistent behavior\n@logged\nclass MyClass:\n    def __init__(self, name):\n        self.name = name\n        self.__log.info(\"MyClass instance created with name: %s\", self.name)\n\n    def do_work(self, value):\n        self.__log.debug(\"Doing work with value: %s\", value)\n        result = value * 2\n        return result\n\n    def _internal_method(self):\n        self.__log.trace(\"Executing internal method.\")\n        return \"internal_result\"\n\n# Example usage\ninstance = MyClass(\"Example\")\noutput = instance.do_work(10)\nprint(f\"Result of do_work: {output}\")\ninstance._internal_method()\n","lang":"python","description":"This example demonstrates how to use the `@logged` and `@traced` decorators on a class. It sets up basic logging to console, including the custom `TRACE` level. When `MyClass` methods are called, `__log` is available for manual logging, and `@traced` automatically logs method entry and exit. Ensure `logging.getLogger().setLevel(TRACE)` is called to make `TRACE` level messages visible."},"warnings":[{"fix":"Always apply `@traced` before `@logged` on the same callable.","message":"When combining `@logged` and `@traced` decorators on the same class or function, it is recommended to apply `@traced` as the outermost decorator and `@logged` as the innermost (i.e., `@traced @logged def ...`). While the library is designed to work regardless of order, this order is considered 'safe' because `@logged` simply sets an attribute and returns the original object.","severity":"gotcha","affected_versions":"1.0.0+"},{"fix":"Decorate subclasses with `@logged` if they need their own `__log` instance and proper logger naming.","message":"If a subclass of a `@logged`-decorated parent class wishes to use a `self.__log` object that correctly reflects its own class name in log messages, it must itself be decorated with `@logged`. Otherwise, logs made through the parent's `__log` will show the parent's name.","severity":"gotcha","affected_versions":"1.0.0+"},{"fix":"Be aware of these limitations when debugging on IronPython; this is a platform-specific issue.","message":"When using `autologging` with IronPython, you might encounter misreported line numbers and '<unknown file>' in log records due to IronPython's limited support for frames.","severity":"gotcha","affected_versions":"All versions on IronPython"},{"fix":"Avoid using Python logging's internal keyword arguments as keys in your extra logging data.","message":"Key names for extra data passed to Python's standard logging module (which autologging uses) cannot clash with some internal names (e.g., 'message', 'args'). If a clash occurs, autologging may issue a warning and change the key name (e.g., from 'message' to 'protected_message').","severity":"gotcha","affected_versions":"1.0.0+"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[{"fix":"Install the module using pip: 'pip install autologging'.","cause":"The 'autologging' module is not installed in the Python environment.","error":"ModuleNotFoundError: No module named 'autologging'"},{"fix":"Ensure you have the latest version of autologging installed: 'pip install --upgrade autologging'.","cause":"The 'autologging' module is installed, but the 'logged' decorator cannot be imported, possibly due to an outdated version.","error":"ImportError: cannot import name 'logged' from 'autologging'"},{"fix":"Verify the installation and version of autologging: 'pip show autologging'.","cause":"The 'traced' decorator is not found in the 'autologging' module, likely due to an incorrect installation or version.","error":"AttributeError: module 'autologging' has no attribute 'traced'"},{"fix":"Ensure that the 'traced' decorator is correctly imported and applied to the function or class.","cause":"This error can occur if the 'traced' decorator is not properly applied, possibly due to incorrect usage or import issues.","error":"TypeError: 'NoneType' object is not callable"},{"fix":"Check the logging configuration and ensure that log messages are correctly formatted as JSON.","cause":"This error may occur if the logging configuration is set to output in JSON format, but the log messages are not properly formatted.","error":"ValueError: No JSON object could be decoded"}]}