admin管理员组文章数量:1221020
I have a code (Fortran), called from a Python wrapper using subprocess
, that can write a lot of information to standard output. Sometimes, if the output is too large, it causes the code to crash.
Usually, we don't need all the output, just the last few lines that tell us whether the code ran successfully or ran into a problem.
So, I want to redirect the standard output to a log file of finite size, say a few lines.
I found that the logging
package allows this with RotatingFileHandler
.
However, I cannot manage to tell subprocess
to write to the log file handled by the RotatingFileHandler
.
Currently, my code is as follow
import subprocess as subp
import logging
from logging.handlers import RotatingFileHandler
logger = logging.getLogger( 'My rolling log' )
handler = RotatingFileHandler( 'test.log', maxBytes=1000,
backupCount=1, mode='w+', encoding='latin-1' )
logger.addHandler( handler )
# define a fileno method because subprocess needs one:
logger.fileno = logger.handlers[0].stream.fileno
stdout = logger
s = subp.Popen( 'mycode.x', stdin=subp.PIPE, stdout=stdout, stderr=subp.PIPE )
sortie, err = smunicate( input=cmd.encode() )
This does save the output to a the log file, but it is not rolling as I want. Could you point out what I am doing wrong ?
本文标签: pythonRedirect standard output of program run using subprocess to rolling log fileStack Overflow
版权声明:本文标题:python - Redirect standard output of program run using subprocess to rolling log file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739337197a2158761.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论