Skip to main content

Python library to tee stderr/stdout temporarily

Project description

Build Status PyPI version

Python library to tee stderr / stdout to a file

Installation

pip install tee

Quick Start

tee_test.py

import sys
from tee import StdoutTee, StderrTee

with StdoutTee("mystdout.txt"), StderrTee("mystderr.txt"):
    sys.stdout.write("[stdout] hello\n")
    sys.stderr.write("[stderr] hello\n")
    sys.stdout.write("[stdout] world\n")
    sys.stderr.write("[stderr] world\n")

sys.stdout.write("[stdout] not going to be written to file\n")
sys.stderr.write("[stderr] not going to be written to file\n")
$ python tee_test.py

[stdout] hello
[stderr] hello
[stdout] world
[stderr] world
[stdout] not going to be written to file
[stderr] not going to be written to file

$ cat mystdout.txt
[stdout] hello
[stdout] world

$ cat mystderr.txt
[stderr] hello
[stderr] world

Filters

StdoutTee and StderrTee take filters as parameters which run before writing to a file or the stream. These filters are callables that take the message to be written as input and return either None or a new message.

I find them particularly useful when you want to write colorized output to the stream, but strip out the control characters when writing to a file, especially when using fabric.

import re
import tee
from fabric.api import run

def _remove_control_chars(message):
    return re.sub(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]', "", message)

def echo_color():
    with tee.StdoutTee("fabout.txt", mode="a", file_filters=[_remove_control_chars]):
        run("""echo -e "\E[1;32mHello World \E[4;31mLets add some\E[0m\E[1;34m color" && tput sgr0""")
fab -H localhost echo_color

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

tee-0.0.3.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

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

tee-0.0.3-py2-none-any.whl (3.1 kB view details)

Uploaded Python 2

File details

Details for the file tee-0.0.3.tar.gz.

File metadata

  • Download URL: tee-0.0.3.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for tee-0.0.3.tar.gz
Algorithm Hash digest
SHA256 801dea18fe5046b57c26e87aa5c7ca0d1b8590cdc0e4e84158d07cdfa6628cdb
MD5 a38cdfd0840510ee82c561bdceff0dfa
BLAKE2b-256 44134daa2418b6c72de8255da00f873d709317accd57f38c6d8bf0233aeb9bf2

See more details on using hashes here.

File details

Details for the file tee-0.0.3-py2-none-any.whl.

File metadata

  • Download URL: tee-0.0.3-py2-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for tee-0.0.3-py2-none-any.whl
Algorithm Hash digest
SHA256 16fdbb34b1e89a308094d86d2d497f5fa5ac47df9202f3dde913070d31699fad
MD5 49846bfb5df141129ab3b4a817320885
BLAKE2b-256 ea8d3505b05e91bc2099fac84365bcd4581b7099c4c456051fed6b6377a751ba

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