pythonlinuxstdout

Is there a "2> dev null" style way to redirect *some* messages but not specific others?


Running Linux. I have a script I wrote that calls a local TTS engine called "Piper". The script runs well and outputs two types of messages to the console - the info passed by Piper about the TTS process, and my text prompt. I do not need to display the TTS info, but I need the text prompt to display. In the output example below, the text prompt is "****>"

Playing raw data 'stdin' : Signed 16 bit Little Endian, Rate 22050 Hz, Mono
[2024-05-09 21:05:21.214] [piper] [info] Loaded voice in 0.438713789 second(s)
[2024-05-09 21:05:21.215] [piper] [info] Initialized piper
[2024-05-09 21:05:21.867] [piper] [info] Waiting for audio to finish playing...
[2024-05-09 21:05:21.867] [piper] [info] Real-time factor: 0.17096870659688937 
(infer=0.6470913549999999 sec, audio=3.7848526077097504 sec)
[2024-05-09 21:05:21.867] [piper] [info] Terminated piper
****>

I would like all the timestamped data to go 2> dev null, but allow my scripted prompt ***> to display. Running "python script.py 2> dev null" nulls the ****> prompt as well as the other data. Is there a simple way to differentiate the output and redirect accordingly?


Solution

  • In this case, the messages are coming from Python’s standard-library logging system, which can be configured in a more reliable way than shell-level grepping:

    python -c '
        import logging
        from piper.__main__ import main
        logging.getLogger("piper").setLevel(logging.WARNING)
        main()
    '