Skip to main content

Python Macro-Generated Logging from Comments

Project description

logcraft

Continuous Integration

Python Macro-Generated Logging from Comments

Introduction

What is logcraft?

logcraft gives the functionality of automatically generating logging calls from annotated comments. This treats logging as documentation, separates application logging and program flow to the highest degree, and provides an overall better development experience.

Source code files are not modified. Instead the @log macro is only applied at the decoration of your object. This gives greater transparency and control to the developer.

Write This: Instead Of:
@log
def do_something(x):
    #i: started doing something with {x}
    x = do_something_interesting(x)
    #i: finished doing something
    return x
def do_something(x):
    logging.info(f"started doing something with {x}")
    x = do_something_interesting(x)
    logging.info("finished doing something")
    return x

Moreover, logcraft has the following benefits:

  • No external dependencies
  • Macro expansion is limited to decorated objects
  • No source files are modified
  • Works with pickle and other serialisation libraries
  • Inject your own logger (loguru etc.) or a custom callable to replace print
  • Allows continuation over multiple comments
  • Allows string formatting with evaluated variables

Inspired by: node-comment-macros and conversations with @kale and @jiaxi.

Why logcraft?

A lot of logging in Python these days are simply descriptive in nature (e.g. "procedure x started" or "x iteration has y variable"). These logging calls usually adds no functionality to the program logic and clouds the programmer's interpretation of the source code.

This library applies the opinion that logging which are documentative in nature should be treated as documentation.

Hence, logging should be generated from annotated comments and logging configuration should be easily injected without modifying the program logic. Having logging as comments results in a clear delineation

How Do I Install?

Simply run:

pip install logcraft

Quickstart

Import the @log decorator:

from logcraft.macro import log

Decorate your object with @log and annotate comments which are intended to be logging messages. Comments with the same annotation and are right after another are treated as continuations of the same logging call. Furthermore, annotated comments with "{}" will be expanded into f-strings that references variables in the local or global namespaces.

Note that @log has to be the top decorator

The following:

@log
def add_one(sequence):
    """Adds 1 to sequence"""
    #: add print call with line
    #: continuation
    # this is some ignored comment
    #: add another line

    #i: this is an info message
    #d: this is a debug message

    new_sequence = []
    for x in sequence:
        #d: adding 1 to x={x}
        new_sequence.append(x + 1) 
        #d: finished adding 1 to x={x}
    #i: constructed new sequence {new_sequence}

    return new_sequence

Will be expanded to:

def add_one(sequence):
    """Adds 1 to sequence"""
    print("add print call with line continuation")
    # this is some ignored comment
    print("add another line")

    logging.info("this is an info message")
    logging.debug("this is a debug message")

    new_sequence = []
    for x in sequence:
        logging.debug(f"adding 1 to x={x}")
        new_sequence.append(x + 1) 
        logging.debug(f"finished adding 1 to x={x}")
    logging.info(f"constructed new sequence {new_sequence}")

    return new_sequence

List of Annotations

The following are the available list of annotations:

#: callable (default: print)

#c: logging.critical 
#d: logging.debug
#e: logging.error
#f: logging.fatal
#i: logging.info
#w: logging.warn

Other Installation Methods

Source

Clone the repository:

git clone https://github.com/ryanlyn/logcraft

Run installation:

cd logcraft
python setup.py install

License

logcraft is made available under the MIT License. For more details, see the LICENSE.

Project details


Download files

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

Source Distribution

logcraft-0.0.1.post1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

logcraft-0.0.1.post1-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file logcraft-0.0.1.post1.tar.gz.

File metadata

  • Download URL: logcraft-0.0.1.post1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for logcraft-0.0.1.post1.tar.gz
Algorithm Hash digest
SHA256 ae5d6fa5fcf421a45f74d80e0075e3943767c4e7d0af7313976b2162db928a2d
MD5 d2db200814830e16cbfc574938f2ed2b
BLAKE2b-256 c6262c49dbdf98e9443ff477840b59c11a1e237f06385e47800763757cd808ae

See more details on using hashes here.

File details

Details for the file logcraft-0.0.1.post1-py3-none-any.whl.

File metadata

  • Download URL: logcraft-0.0.1.post1-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.2

File hashes

Hashes for logcraft-0.0.1.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 26f7a3c5e51ef7545f195be2ec6f45f862302a6d2c7e52e3baffc40f681fc30f
MD5 6d3f738f8ad8969b63121f1bcef371b2
BLAKE2b-256 aadc5f657e75530516aacc2e59b8cdbedd36dec1bfe2a17b562fe6b742a85188

See more details on using hashes here.

Supported by

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