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

Note, the '*' argument means all arguments should be logged. Otherwise, no arguments are logged.

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 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.2.0.tar.gz (2.8 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.2.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for trace_to_log-0.2.0.tar.gz
Algorithm Hash digest
SHA256 04755caf056c430a04dd62a4283a372077548524b487153a7d2c357984e23a48
MD5 6a398b0ce22b3d88ca5c3f5bcee57942
BLAKE2b-256 e10fab009431d3fa4f882eb310a2f7a03c4ee1b4a4ad942e72e5c86dcb9cc32d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for trace_to_log-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4595a21022df768cc0743be97885accb1dd3978cee07f050a621976e8c3a111d
MD5 a7ce54e625626b8b80fd48aeb9dc08aa
BLAKE2b-256 fdcdf62c830dd07cb244497b60e4aafde65437a60ce05adfe37e87b469be3bef

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