Logging handler: No output

  • Thread starter Florian Lindner
  • Start date
F

Florian Lindner

Hello,

I have a class method that executes a subprocess. There are two loggers in the
class, self.logger for general logging and proclog for process output (stdout
& stderr) logging which should go to stdout and a file:


def start_process(self, command, no_shlex=False, raise_excpt=True,
print_output = True, **kwargs):

cmd = command if no_shlex else shlex.split(command)

# Use an additional logger without formatting for process output.
proclog = logging.getLogger(self.config.tag)
proclog.propagate = False # Process output should not propage to the main
logger
logfile = self._logfilename()

if logfile:
proclog.addHandler(logging.FileHandler(logfile))

if print_output:
proclog.addHandler(logging.StreamHandler(sys.stdout))

self.popen = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, bufsize=0, **kwargs)
while True:
output = self.popen.stdout.readline().decode()
if output == "" and self.popen.poll() != None:
break
proclog.info(output.rstrip("\n"))

ret_code = self.popen.returncode

self.logger.debug("%s returned with %i", command, ret_code)


But neither the FileHandler nor the StreamHandler produce any actual output.
The file is being created but stays empty. If I use a print output in the
while loop it works, so output is catched and the applications stdout in
working. But why the logger proclog catching nothing?

Thanks,

Florian
 
P

Paul Rubin

Florian Lindner said:
The file is being created but stays empty. If I use a print output in the
while loop it works, so output is catched and the applications stdout in
working. But why the logger proclog catching nothing?

I don't see you setting the log level anyplace in that sample, and
you are logging at INFO and DEBUG levels. By default, only WARNING
and above only actually produce output. If you want INFO and DEBUG
to log, you have to request it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top