Skip to main content

Treelogger

This package adds structure to your application logs by creating a tree structure for functions that execute within functions.

These nested functions can be very troublesome to troubleshoot using a regular logger, but by using the tree logger, you can easily see the chain of events and drill down to where the error occurs.

Below is an example of what the tree would look like. The example shows how output of a function can be nested to the appropriate level.

Function Stand_Up 
-"Begin Standing Up" 
--Function Move_Legs
---"Moving Legs" 
----Function Check_Balance
-----"Balance is Good"
----Finish Check_Balance
---"Leg Movement Sucessful"
-- Finish Move_Legs 
-"Stand Up took 1 Second(s)" 
Finish Stand_up

To begin tree logging, import the tree object and treewrap decorator into your modules.

#test.py 

from treelogger import tree, treewrap 


@treewrap()
def showthis(value):
    tree.log(value)

    return

@treewrap()
def nest(value):

    tree.log('Nesting a value of %i' % value)

    showthis(value)

    tree.log('Nesting a value of %i + 1' % value)

    showthis(value + 1)

You can then import things from your modules into one or more main modules. In the main module, use the tree = TreeLogger() form to create a logging instance.

The TreeLogger class instance is a global instance (kind of like sys.stdout) and all of your modules that imported tree will send their logs to the global tree. When defining the global tree, it has 2 default outputs: stdout and text file.

#main.py
from test import showthis, nest
from treelogger import tree, TreeLogger

tree = TreeLogger()
nest(123)

tree.close_log()

Running main.py would give you the output below.


<?xml version="1.0" ?>
<log>
	<nest>
		<msg>  Nesting a value of 123  </msg>
		<showthis>
			<msg>  123  </msg>
		</showthis>
		<msg>  Nesting a value of 123 + 1  </msg>
		<showthis>
			<msg>  124  </msg>
        </showthis>
	</nest>
</log>


As you can see, functions become nested and so do the logging messages for the functions. The resulting XML file can be searched using Xpath or visualized using an XML visualizer.

A common mistake is to use the treewrap without the (). Unlike other decorators, the brackets are necessary for properly returning values from the decorated function.

Release files for treelogger 0.15

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for treelogger 0.15
File Size Uploaded
treelogger-0.15.tar.gz 3.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for treelogger 0.15
File Interpreter ABI Platform
treelogger-0.15-py3-none-any.whl Python 3 none any Details

Total release size: 10.4 kB

Release files / treelogger-0.15.tar.gz

Download URL treelogger-0.15.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fbfbeb495adf44d591f018e86094121fd2cd84ee9115b729fd50dd6c01f213dd
BLAKE2b-256 checksum
How to use checksums
7450e6423854475aed245e5794cafed84679496cd84adf6a51e11fd2098b7705
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4

Release files / treelogger-0.15-py3-none-any.whl

Download URL treelogger-0.15-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
68e124176db64f17bdde7a0c0e62b7ed46d095dc66ea413d868d2377552ccb14
BLAKE2b-256 checksum
How to use checksums
9c746841c46d33f90f431aabad65a533260614c594a5d7b0cc3bbf0e3861b7d1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4

Release history Release notifications | RSS feed

This release

0.15 This release

2 release files

0.14

2 release files

0.13

2 release files

0.12

2 release files

0.11

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page