Eric
Eric

Reputation: 1261

Python not logging error output

I'm using Jupyter Notebook to develop some Python. This is my first stab at logging errors and I'm having an issue where no errors are logged to my error file.

I'm using:

import logging
logger = logging.getLogger('error')
logger.propagate = False
hdlr = logging.FileHandler("error.log")
formatter = logging.Formatter('%(asctime)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.DEBUG)

to create the logger.

I'm then using a try/Except block which is being called on purpose (for testing) by using a column that doesn't exist in my database:

try:
    some_bad_call_to_the_database('start',group_id)
except Exception as e:
    logger.exception("an error I would like to log")
    print traceback.format_exc(limit=1)

and the exception is called as can be seen from my output in my notebook:

Traceback (most recent call last):
File "<ipython-input-10-ef8532b8e6e0>", line 19, in <module>
some_bad_call_to_the_database('start',group_id)
InternalError: (1054, u"Unknown column 'start_times' in 'field list'")

However, error.log is not being written to. Any thoughts would be appreciated.

Upvotes: 1

Views: 83

Answers (2)

Marom
Marom

Reputation: 56

try:

except Exception as e:
    excep = logger.info("an error I would like to log")

for more information please look at the docs https://docs.python.org/2/library/logging.html

have a great day!

Upvotes: 1

Dan Gittik
Dan Gittik

Reputation: 3850

You're using logging.exception, which delegates to the root logger, instead of using logger.exception, which would use yours (and write to error.log).

Upvotes: 1

Related Questions