Package: wrap-logger
Wrap LoggerA library to wrap around objects and modules in Python and log property
accesses and calls.pip install wrap-loggerRationaleIn 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 createdwrap-logger.UsageUsingwrap-loggeris simple. Simply call thewrapfunction, which will
return a wrapped version.With a moduleimportfoofromwrap_loggerimportwrap# Now all function calls and global reads/writes will be loggedfoo=wrap(foo)# Getting a property causes things to be loggedfoo.bar# [WRAP LOG] > Get  foo.bar# [WRAP LOG] < Get  foo.bar: gave 42# Same for setting propertiesobj.bar=43# [WRAP LOG] > Set  foo.bar: 42 -> 43# [WRAP LOG] < Set  foo.bar# And calling functionsobj.echo('hello world')# [WRAP LOG] > Call foo.echo('hello world')# [WRAP LOG] < Call foo.echo('hello world'): returned 'hello world'With a classfromwrap_loggerimportwrapclassSimple:def__init__(self)->None:self.value=42# Wrap an instance of the objectobj=wrap(Simple())Without Pipwrap-loggerrequires no dependencies, and can even function with some parts
of the standard library missing. Simply head over to the releases tab where thewrap-logger.zipfile 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
    + etcYou should then be able to use it normally:# program.pyfromwrap_loggerimportwrap...Logging to a filewrap-loggerwill write to any file you give it.importsysfromwrap_loggerimportwrapsome_object=object()wrapped=wrap(some_object,output=sys.stderr)# All logs will go to stderrImplementation detailswrap-loggerwraps objects in aWrapLoggerclass. Although the class does
override the__class__property so as to foolisinstancechecks, fooling
thetypefunction does not appear to work, so those checks may fail leading
to potentially erroneous behaviour.TODOswrap-loggerwas 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 loggedConfiguration on what is/isn't logged (currently it just prints
everything)Figure out what happens if you usewrap-loggeron itself. Does it
crash? Does it delete System 32? Does the space-time continuum collapse?
Who knows!MIT LicenseCopyright (c) 2023 Miguel GuthridgePermission 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.