Gre*_*uhn 7 python linux stdout pipe
不久前我知道这样做的原因,但后来我忘记了,谷歌搜索了 5 分钟还没有找到答案。
我写了一个 python 脚本,它有两个处理程序。一种用于文件,一种用于流。
一切都如我所愿。
现在,我想快速搜索输出中正在打印到终端的内容,但是通过 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()
Run Code Online (Sandbox Code Playgroud)
我对脚本的调用如下所示:
<script_name> <script args> | grep -i <search_string>
正如@Blender 在原始问题下方的评论中提到的,您只需要重定向 stderr。您可以通过2>&1在管道前添加重定向来做到这一点。所以,例如,
python main.py 2>&1 | grep INFO
Run Code Online (Sandbox Code Playgroud)
将过滤INFO由 记录的任何行main.py。
| 归档时间: |
|
| 查看次数: |
3268 次 |
| 最近记录: |