Skip to main content

A library to wrap around objects and modules in Python and log property accesses and calls

Project description

Wrap Logger

A library to wrap around objects and modules in Python and log property accesses and calls.

pip install wrap-logger

Rationale

In some cases when programming, errors can break things to the point where you have no idea where things went wrong, for example, crashing the runtime environment before a backtrace can be printed. In these cases, it can be extremely useful to log changes to data that are potentially related to the issue, but doing so can be tedious and slow. I needed a simple solution to easily inject logging into objects, and so I created wrap-logger.

Usage

Using wrap-logger is simple. Simply call the wrap function, which will return a wrapped version.

With a module

import foo
from wrap_logger import wrap

# Now all function calls and global reads/writes will be logged
foo = wrap(foo)

# Getting a property causes things to be logged
foo.bar
# [WRAP LOG] > Get  foo.bar
# [WRAP LOG] < Get  foo.bar: gave 42

# Same for setting properties
obj.bar = 43
# [WRAP LOG] > Set  foo.bar: 42 -> 43
# [WRAP LOG] < Set  foo.bar

# And calling functions
obj.echo('hello world')
# [WRAP LOG] > Call foo.echo('hello world')
# [WRAP LOG] < Call foo.echo('hello world'): returned 'hello world'

With a class

from wrap_logger import wrap

class Simple:
    def __init__(self) -> None:
        self.value = 42


# Wrap an instance of the object
obj = wrap(Simple())

Without Pip

wrap-logger requires no dependencies, and can even function with some parts of the standard library missing. Simply head over to the releases tab where the wrap-logger.zip file is attached, then extract it into a new folder within your project, where you can import it easily.

Here is an example file structure:

+ program.py
+ wrap_logger/  (you create this folder and place the files into it)
    + __init__.py
    + __wrap_logger.py
    + etc

You should then be able to use it normally:

# program.py
from wrap_logger import wrap

...

Logging to a file

wrap-logger will write to any file you give it.

import sys
from wrap_logger import wrap

some_object = object()
wrapped = wrap(some_object, output=sys.stderr)
# All logs will go to stderr

Implementation details

wrap-logger wraps objects in a WrapLogger class. Although the class does override the __class__ property so as to fool isinstance checks, fooling the type function does not appear to work, so those checks may fail leading to potentially erroneous behaviour.

TODOs

wrap-logger was thrown together so I could quickly debug another project, so it is incomplete. If there is demand (hint: create an issue), I will be happy to try my hand at implementing the following features:

  • Recursive wrapping, so that attributes of attributes are also logged
  • Configuration on what is/isn't logged (currently it just prints everything)
  • Figure out what happens if you use wrap-logger on itself. Does it crash? Does it delete System 32? Does the space-time continuum collapse? Who knows!

MIT License

Copyright (c) 2023 Maddy Guthridge

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

wrap_logger-0.2.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

wrap_logger-0.2.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file wrap_logger-0.2.1.tar.gz.

File metadata

  • Download URL: wrap_logger-0.2.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1021-azure

File hashes

Hashes for wrap_logger-0.2.1.tar.gz
Algorithm Hash digest
SHA256 3d6f375154a8a3bf5841b2fc60d22cdb481c7eedb995d3de72d3d17bc3eb655f
MD5 ffa34629a827ab30068a063ce96a82f1
BLAKE2b-256 84bfef6e2ede4cc48974e91611afea4bfbc52e57d706e63a3d381138b203378e

See more details on using hashes here.

File details

Details for the file wrap_logger-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: wrap_logger-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1021-azure

File hashes

Hashes for wrap_logger-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2575797b733b90fd3548382bfe081c2574940c6836bfb2ad0ea9a28ecc4d9ccf
MD5 f2a2bbdf8321303961fdf489921d9d9e
BLAKE2b-256 6aa31869e0cb8bfa01330afb023b5938064859951f779a92a183c1e180d0967e

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