logcraft
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
pickleand 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.
Release files for logcraft 0.0.1.post1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| logcraft-0.0.1.post1.tar.gz | 5.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| logcraft-0.0.1.post1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.3 kB
Release files / logcraft-0.0.1.post1.tar.gz
| Download URL | logcraft-0.0.1.post1.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ae5d6fa5fcf421a45f74d80e0075e3943767c4e7d0af7313976b2162db928a2d
|
|
BLAKE2b-256 checksum How to use checksums |
c6262c49dbdf98e9443ff477840b59c11a1e237f06385e47800763757cd808ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / logcraft-0.0.1.post1-py3-none-any.whl
| Download URL | logcraft-0.0.1.post1-py3-none-any.whl |
|---|---|
| Size | 6.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
26f7a3c5e51ef7545f195be2ec6f45f862302a6d2c7e52e3baffc40f681fc30f
|
|
BLAKE2b-256 checksum How to use checksums |
aadc5f657e75530516aacc2e59b8cdbedd36dec1bfe2a17b562fe6b742a85188
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|