Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if any errors were logged during a python program's execute?

Tags:

python

logging

I have a python script which calls log.error() and log.exception() in several places. These exceptions are caught so that the script can continue to run, however, I would like to be able to determine if log.error() and/or log.exception() were ever called so I can exit the script with an error code by calling sys.exit(1). A naive implementation using an "error" variable is included below. It seems to me there must be a better way.

error = False

try:
  ...
except:
   log.exception("Something bad occurred.")
   error = True

if error:
   sys.exit(1)
like image 883
Tim Graham Avatar asked Feb 07 '11 20:02

Tim Graham


People also ask

How do I check Python error logs?

To log an exception in Python we can use logging module and through that we can log the error. Logging an exception in python with an error can be done in the logging. exception() method. This function logs a message with level ERROR on this logger.

Which of the following is performed by logging in Python?

Python has a built-in module logging which allows writing status messages to a file or any other output streams. The file can contain the information on which part of the code is executed and what problems have been arisen.

How do you handle errors in Python?

The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.

What are logging levels in Python?

Python Logging Levels There are six log levels in Python; each level is associated with an integer that indicates the log severity: NOTSET=0, DEBUG=10, INFO=20, WARN=30, ERROR=40, and CRITICAL=50.


2 Answers

I had the same issue as the original poster: I wanted to exit my Python script with an error code if any messages of error or greater severity were logged. For my application, it's desirable for execution to continue as long as no unhandled exceptions are raised. However, continuous integrations builds should fail if any errors are logged.

I found the errorhandler python package, which does just what we need. See the GitHub, PyPI page, and docs.

Below is the code I used:

import logging
import sys

import errorhandler

# Track if message gets logged with severity of error or greater
error_handler = errorhandler.ErrorHandler()

# Also log to stderr
stream_handler = logging.StreamHandler(stream=sys.stderr)
logger = logging.getLogger()
logger.setLevel(logging.INFO)  # Set whatever logging level for stderr
logger.addHandler(stream_handler)

# Do your program here

if error_handler.fired:
    logger.critical('Failure: exiting with code 1 due to logged errors')
    raise SystemExit(1)
like image 187
Daniel Himmelstein Avatar answered Oct 27 '22 10:10

Daniel Himmelstein


You can check logger._cache. It returns a dictionary with keys corresponding to the numeric value of the error level logged. So for checking if an error was logged you could do:

if 40 in logger._cache and logger._cache[40]

like image 31
A. Pine Avatar answered Oct 27 '22 11:10

A. Pine