Skip to main content

Capture prints and tracebacks to the timestamped log file with 2 lines of code

Project description

Loggify: capture prints and tracebacks to log file with 2 lines of code

Python PyPI version license

Loggify is a convenient way to redirect stdout and stderr to the timestamped log file. It works by temporarily overriding the default behaviour of sys.stdout and sys.stderr in the context manager. You still see the output in the console - it is just additionally streamed to the log file (see example below).

Installation

$ pip install loggify

Usage

script.py:

from loggify import Loggify

def main():
    print("START")
    for i in range(10):
        print(" " * i + "x")
    print("END")
    5 / 0   # traceback will be captured

with Loggify("output.log"):  # specify output filename
    main()

output.log:

2020-04-07 20:45:18.391:STDOUT:START
2020-04-07 20:45:18.391:STDOUT:x
2020-04-07 20:45:18.391:STDOUT: x
2020-04-07 20:45:18.391:STDOUT:  x
2020-04-07 20:45:18.391:STDOUT:   x
2020-04-07 20:45:18.391:STDOUT:    x
2020-04-07 20:45:18.391:STDOUT:     x
2020-04-07 20:45:18.391:STDOUT:      x
2020-04-07 20:45:18.391:STDOUT:       x
2020-04-07 20:45:18.391:STDOUT:        x
2020-04-07 20:45:18.391:STDOUT:         x
2020-04-07 20:45:18.392:STDOUT:END
2020-04-07 20:45:18.392:STDERR:Traceback (most recent call last):
2020-04-07 20:45:18.392:STDERR:  File "script.py", line 11, in <module>
2020-04-07 20:45:18.392:STDERR:    main()
2020-04-07 20:45:18.392:STDERR:  File "script.py", line 8, in main
2020-04-07 20:45:18.392:STDERR:    5 / 0   # traceback will be captured
2020-04-07 20:45:18.392:STDERR:ZeroDivisionError: division by zero

License

MIT License (see LICENSE).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for loggify, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size loggify-0.1.2-py3-none-any.whl (4.2 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size loggify-0.1.2.tar.gz (3.3 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page