Skip to main content

Advanced python logging decorators.

Project description

Have you ever wanted to have logging decorators for logging function calls and for automatic creation of logger object in class, based on class hierarchy.

If your answer is true, here is my implementation https://github.com/offlinehacker/PyLogDecorate.

There is very nice option i’ve implemented called subdecorate, enabling for functions in derived classes being logged, even if decorator is only applied on base class functions.

I’ve also added option to recursively trace function calls betwene objects. I’ve implemented this using a set of trace series. Well you actually only speciffy on class functions, that you want to trace, name of trace(tracename) series and class attribute that you want to be written to log when you enter or leave this this function. If you do this, LogCall will write a logging message to logger when you enter and leave function, with additional parameters of log series(tracename) and specificed class attribute(traceattr). Processing these messages with python log Formatter you can reconstruct execution for one or more trace series. This is much better than stack trace, because you can trace what you want to trace, not the whole stack and helps you a lot with complicated recursive programs.

  • Install:

    python setup.py install or easy_install PyLogDecorate.

  • Example code:

from log import LogCall, LogClass

@LogCall()
def test():
    print "x"

@LogClass({"subdecorate": True})
class test1(object):
    name="kekec"
    name2="kekec2"
    @LogCall({"subdecorate": True, "tracename": "tracetest", "traceattr": "name"})
    def test(self):
        pass

    @LogCall({"subdecorate": True, "tracename": "tracetest", "traceattr": "name2"})
    def test2(self):
        self.logger.debug("Inside base class!")

@LogClass()
class test2(test1):
    def __init__(self):
        pass

    def test(self):
        self.logger.debug("Inside funtion.")
        self.test2()

@LogClass()
class test3(test2):
    def __init__(self):
        pass

    def test(self):
        self.logger.debug("Inside funtion2.")
        self.test2()


test()
b=test3()
c=test2()
b.test()
c.test()

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

PyLogDecorate-0.33.4.tar.gz (4.5 kB view details)

Uploaded Source

File details

Details for the file PyLogDecorate-0.33.4.tar.gz.

File metadata

File hashes

Hashes for PyLogDecorate-0.33.4.tar.gz
Algorithm Hash digest
SHA256 6ac7f48a414d59b0b5df6bb19fe775512b40940f667eee1709dad6bd45a035cd
MD5 d88410e71280bf37ec4d6c0feb535f8e
BLAKE2b-256 a93cef2f63dc6965855dd8794d6eff872a42b9ad170918aca9980ff32039a29b

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