[Tutor] Logging exceptions, but getting stderr output instead

Alex Hall ahall at autodist.com
Wed May 25 15:13:23 EDT 2016


Well, I found the major problem: I had
logging.exception()
not
logger.exception()
All I can say is, with the screen reader I'm using, they sound similar.
Things are now working as expected. I'm still wondering about stopping
execution, though: call exit(), raise, or some other way?


On Wed, May 25, 2016 at 2:11 PM, Alex Hall <ahall at autodist.com> wrote:

> Hello again list,
> I didn't expect to be back so soon. :) I'm trying to log my new script,
> and logger.info() works fine. However, logger.exception() doesn't; I see
> the exception print to stderr, and it never appears in the log. Oddly, info
> messages after that appear in the shell and in my log, whereas normally
> they are only in the log. Here's my logger setup:
>
> logger = logging.getLogger(appName)
> logger.setLevel(logging.DEBUG)
> infoLogFormatter = logging.Formatter("%(asctime)s\n%(name)s,
> %(levelname)s: %(message)s", datefmt = "%I:%M:%S %p, %B %d, %Y")
> infoLogFileName = appName+".log"
> infoFileHandler = logging.FileHandler(infoLogFileName, mode="w")
> infoFileHandler.level = logging.INFO
> infoFileHandler.setFormatter(infoLogFormatter)
> logger.addHandler(infoFileHandler)
>
> Then, I deliberately called a non-existant function:
>
> for rep in reps:
>  try:
>   workbook = xlsxwriter.Workbook(workbookName)
>   worksheet = workbook.addWorksheet(rep.name) #should be add_worksheet,
> so this errors out
>  except:
>   logging.exception("Error generating spreadsheet for {name}".format(name=
> rep.name))
>
> The string I pass to logging.exception, along with the stack trace, print
> to the command line and not to my log file. Other logging.info() calls
> also print to the command line, but they also appear in the log. I haven't
> done much with logging before, and what I have done has been debug only,
> never exceptions. The resolution to this will likely be obvious, but when I
> looked it up, I found only information about how to log exceptions. No one
> seems to have the problem of exceptions not being logged correctly.
>
> As a quick aside, is there an easy way to halt script execution for some
> exceptions? Right now, catching them means that execution continues, but I
> sometimes want to log the problem and then abort the script, as the error
> means it shouldn't continue. Thanks.
>
>
> --
> Alex Hall
> Automatic Distributors, IT department
> ahall at autodist.com
>



-- 
Alex Hall
Automatic Distributors, IT department
ahall at autodist.com


More information about the Tutor mailing list