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.2.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.2-py2-none-any.whl (4.7 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for tee-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d819969fe413fe3d65e1d4166ad5df85204d515bde87686fcc6bf713de3e1ab4
MD5 2d573c965bf2b5c73d7ef30b085577d4
BLAKE2b-256 78d2c29c47ab10ffd81ad475b6c41433b2b0f88d545ad0150f16c1ce34ae5237

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for tee-0.0.2-py2-none-any.whl
Algorithm Hash digest
SHA256 b1751ae674ec56c401ea57d8f3802a2391b3e5e1c436d82398248bae094b40c2
MD5 c3c6fd0156136333208905e4bacf95f1
BLAKE2b-256 85884e0bb3efab74eac20bd6d66148439b0eb9951cd09bc8ffa6edebf9bb3b33

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