我很早以前就知道了这个原因,但此后我已经忘记了,并且在5分钟的谷歌搜索中没有找到答案。
我写了一个有两个处理程序的python脚本。一种用于文件,另一种用于流。
一切都如我所愿。
现在,我想快速grep查找正在输出到终端的输出中的某些内容,但是通过grep传递脚本输出的管道似乎无法正常工作,因为所有输出仍会被打印到终端。
我正在使用unix和python 2.7
这可能是一个重复的问题,但我找不到答案。
这是我的日志记录模块的设置:
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()
我对脚本的调用如下所示:
<script_name> <script args> | grep -i <search_string>
正如@Blender在原始问题下方的注释中提到的那样,您只需要重定向stderr。您可以通过2>&1
在管道之前添加重定向来实现。因此,例如
python main.py 2>&1 | grep INFO
将过滤INFO
由记录的任何行main.py
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句