Skip to main content

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

Project description

Loggify: capture prints and tracebacks to a 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 the 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("main.log"):  # specify output filename
    main()

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

Source Distribution

loggify-0.2.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

loggify-0.2.1-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file loggify-0.2.1.tar.gz.

File metadata

  • Download URL: loggify-0.2.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.0.1 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

File hashes

Hashes for loggify-0.2.1.tar.gz
Algorithm Hash digest
SHA256 ebb8fc0fc017b2d7253589aceaa9232874187d444cad826cc1ce4f6fb187bf79
MD5 3dc1c28556628ce73a15e14874b93ca6
BLAKE2b-256 88b6d385fe88015db363c9f51b1462f39f3b350dab763988ef9964e4972e7da2

See more details on using hashes here.

File details

Details for the file loggify-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: loggify-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.0.1 pkginfo/1.8.2 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.10

File hashes

Hashes for loggify-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aa2a4dfc0f3c77ce47a0050d2c7fe81d0c5bddec8229a81c1dee0b5e67f1783a
MD5 253a2d00946861aebac2c9516eaa1f5f
BLAKE2b-256 027e843aa04ddd8966b22447fc2c465298b642170eb0e140023f568140432125

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page