Skip to main content

Create tree structures for your logs

Project description

# 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.



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

treelogger-0.13.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

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

treelogger-0.13-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file treelogger-0.13.tar.gz.

File metadata

  • Download URL: treelogger-0.13.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4

File hashes

Hashes for treelogger-0.13.tar.gz
Algorithm Hash digest
SHA256 837ddd4a091b8bec2437e8958f931a5f9120638c824d38c4dca3bd1787b64f7c
MD5 3c353c95dd39e5e19e34c85ef0cf480a
BLAKE2b-256 002e33ad5aefc9877770e772086c60dc258a573a024877f74bf5721bb4ae9548

See more details on using hashes here.

File details

Details for the file treelogger-0.13-py3-none-any.whl.

File metadata

  • Download URL: treelogger-0.13-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/28.8.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.4

File hashes

Hashes for treelogger-0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 b7ad84e5ef2b235702a231ab0ddce609f85ef3a452740c06527aab4486967887
MD5 fd612a05fc0eccccc83e087a69c03d85
BLAKE2b-256 afd39b787bcb24d5592971048b7b66e5b934f70b391d2518e62e00f6565fcc74

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