https://github.com/arnowaczynski/loggify
Capture prints and tracebacks to a log file with 2 lines of code
https://github.com/arnowaczynski/loggify
logging python
Last synced: 4 months ago
JSON representation
Capture prints and tracebacks to a log file with 2 lines of code
- Host: GitHub
- URL: https://github.com/arnowaczynski/loggify
- Owner: arnowaczynski
- License: mit
- Created: 2020-04-07T08:54:37.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-12-21T17:21:30.000Z (over 4 years ago)
- Last Synced: 2025-05-07T17:18:44.658Z (about 1 year ago)
- Topics: logging, python
- Language: Python
- Homepage:
- Size: 16.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Loggify: capture prints and tracebacks to a log file with 2 lines of code
[](https://www.python.org/downloads/)
[](https://pypi.org/project/loggify/)
[](https://github.com/ar-nowaczynski/loggify)
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
```bash
$ pip install loggify
```
## Usage
script.py:
```python
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
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](https://github.com/ar-nowaczynski/loggify/blob/master/LICENSE)).