Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"None" in python log-file

I'm using the logging module in python. When I call the script with the wrong arguments on the command-line, the log-file contains at some point the single word "None" and I don't know where it comes from.

Here's my code cutting, where I do the logging.exception:

# Show script where to find blank- and viva-data / set argv

vz = glob.glob("/home/jw/cmp_blank_viva/*csv")
skript, s1, m1 = argv

# Define script-functions and logging-config

logging.basicConfig(level=logging.ERROR, filename='cmp_blank_viva.log', format='%(asctime)s:%(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')

def open_data(vz, s1, m1):

    try:

        checker = []

        for i in vz:
            if "drhot_viva_check_%s" % m1 in i:
                blank_data = csv.reader(open(i, 'r'))
                checker.append(1)
            else:
                pass

        if 1 not in checker:
            logging.exception("Could not open viva file with %s %s.\n" % (s1, m1))

        checker = []

        for i in vz:
            if "hoteldaten_%s_%s" % (m1, s1) in i:
                viva_data = csv.DictReader(open(i, 'r'), delimiter = ';')
                checker.append(1)
            else:
                pass

        if 1 not in checker:
            logging.exception("Could not open blank file with %s %s.\n" % (s1, m1))

        return blank_data, viva_data

    except IOError:
        logging.exception("Could not open file:\n " + traceback.format_exc())
    except IndexError:
        logging.exception("Could not open file:\n " + traceback.format_exc())

After I run the script (with the wrong arguments), the log-file looks like:

Could not open viva file with arg1 arg2.

None
Could not open blank file with arg1 arg2.

None

Any ideas?

like image 290
jwi Avatar asked Feb 03 '15 15:02

jwi


People also ask

How do I make a log file not exist in Python?

There is a built-in module called logging for creating a log file in Python. import logging logging. basicConfig(filename="log. txt", level=logging.

Why logger info is not printing in Python?

This is because the root logger, by default, only prints the log messages of a severity level of WARNING or above. However, using the root logger this way is not much different from using the print() function. The call to logging. basicConfig() is to alter the root logger.

How do you clear a log file in python?

You'd have to signal to the daemon that the log file's been removed so it can close/reopen the log file handle and start using the new log file. Otherwise it'll just keep writing to the old one, which will still exist somewhere until all filehandles on it are closed. Give mode="w" to logging. FileHandler .


1 Answers

You are using logging.exception() when there was no exception:

logging.exception("Could not open viva file with %s %s.\n" % (s1, m1))

Since there is no exception to log, None is added instead. Use logging.error() here to just log a message:

logging.error("Could not open viva file with %s %s", s1, m1)

You don't need to interpolate the string elements, logging does this for you when needed. I also removed the \n newline, that too is added for you.

Elsewhere you are including the exception manually when you are in a exception handler:

logging.exception("Could not open file:\n " + traceback.format_exc())

You don't need to append the traceback manually, logging.exception() handles this for you and includes the traceback, properly formatted.

Just log the message, nothing more:

logging.exception("Could not open file")

From the logging.exception() documentation:

Exception info is always added to the logging message. This method should only be called from an exception handler.

Bold emphasis mine; you used it outside an exception handler so no exception was available to be logged.

It does so by setting the exc_info keyword argument for you:

  • exc_info* which, if it does not evaluate as false, causes exception information to be added to the logging message. If an exception tuple (in the format returned by sys.exc_info()) is provided, it is used; otherwise, sys.exc_info() is called to get the exception information.
like image 67
Martijn Pieters Avatar answered Oct 30 '22 12:10

Martijn Pieters