Skip to main content

Add minimal tracing to a function using a decorator

Project description

trace-to-log

A simple library to add some logging of selected functions via a trace decorator.

By default, tracing is only enabled if TRACE_ME environment variable is set

Example:

from trace_to_log import trace

@trace
def adder(x:int, y:int) -> int:
    return x + y

Now, calling adder will result in the following log entries at DEBUG level:

Going to call adder with args:
x :: 1
y :: 2
adder took 9.5367431640625e-07 seconds
Finished adder, returned:3

being picky

If you don't want to log every argument, maybe because the string representation of one of the objects is verbose, you can explicitly select which to arguments to log:

from trace_to_log import trace

@trace('x')
def adder(x:int, y:int) -> int:
    return x + y

This will result in:

Going to call adder with args:
x :: 1
adder took 9.5367431640625e-07 seconds
Finished adder, returned:3

custom argument conversions

Make log interpretation easier with a custom representation:

from trace_to_log import trace

to_hex = lambda x: format(x, '#010x')

@trace('value', address=to_hex)
def write_32(address:int, value:int):
    pass

This will result in:

Going to call write_32 with args:
address :: 0x20000000
value :: 12"
write_32 took 1.6689300537109375e-06 seconds
Finished write_32, returned:None

Unconditionally enabling tracing

from trace_to_log import make_trace

trace = make_trace(
    trace_enable=lambda: True,
)

@trace
def adder(x:int, y:int) -> int:
    return x + y

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

trace_to_log-0.1.0.tar.gz (2.7 kB view details)

Uploaded Source

Built Distribution

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

trace_to_log-0.1.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file trace_to_log-0.1.0.tar.gz.

File metadata

  • Download URL: trace_to_log-0.1.0.tar.gz
  • Upload date:
  • Size: 2.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for trace_to_log-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5bf31b22cb89c8ff21c4cc1108aafca2a941164ebc24353412178170c22d2e26
MD5 f0fe742676ff3ad19c4c583b5ff8d99d
BLAKE2b-256 71b5d5f15dcf4db4292dac6277d13ff17ad34ec2a727deb7a5a8cd19a5e53d06

See more details on using hashes here.

File details

Details for the file trace_to_log-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for trace_to_log-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1d4d1172ba8089a1ddf9101bb38584b2911b04b1cea2cb27b0cd33d6b8493a86
MD5 e304c44c6564f512b56c9c2363d4113e
BLAKE2b-256 12352bca9253f0832d75557d364fb28a0457c5c0936553eb3d0344c334405f77

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