python 管道 异步读取 stdout select_管道python记录stdout流输出到grep

I knew the reason for this a while back but I have since forgotten and 5 mins of googling hasn't revealed the answer.

I have written a python script which has two handlers. One for files and one for streams.

Everything works as I want.

Now, I wanted to quickly grep for something in the output that was being printed to the terminal but piping the script's output through grep doesn't appear to be working in that all of the output still get's printed to the terminal.

I am using unix and python 2.7

This is probably a duplicate question but I can't find the answer.

Here's my setup of the logging module:

def setup_logger(verbosity):

#logger = logging.getLogger(regress.app_name)

logger = logging.getLogger('components')

logger.setLevel(logging.DEBUG)

# create file handler which logs even debug messages

fh = logging.FileHandler(Config.logging_file_name, mode='w')

fh.setLevel(logging.DEBUG)

# create console handler with a higher log level

ch = logging.StreamHandler()

ch.setLevel({True:logging.DEBUG, False:logging.INFO}[verbosity])

# create formatter and add it to the handlers

file_formatter = logging.Formatter('%(asctime)s - %(pathname)s - %(funcName)s - %(name)s - %(levelname)s - %(lineno)s - %(message)s')

console_formatter = logging.Formatter('%(filename)s - %(lineno)s - %(levelname)s - %(message)s')

fh.setFormatter(file_formatter)

ch.setFormatter(console_formatter)

# add the handlers to the logger

logger.addHandler(fh)

logger.addHandler(ch)

#logging.abort = abort

#logging.abort("testing...")

#logger.info("does this happen?")

#logging.func = func

#logging.func()

#logger.func()

My invocation of the script looks like this:

解决方案

As @Blender mentions in the comments below the original question, you just need to redirect stderr. You can do this by adding a 2>&1 redirect before the pipe. So, for example,

python main.py 2>&1 | grep INFO

will filter for any INFO lines logged by main.py.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值