Python Forum
Logging module stopped working!
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Logging module stopped working!
#1
I'm building an application that contains some long-running operations in a separate thread from the user interface.  I've been using the logging module writing logging.info() statements to a .log file to keep track of the data interactions while it runs.

In the midst of a recent run, the logging simply stopped working.  The rest of the application continued on as if nothing had happened, but without the log I'm pretty much blind.

I set up the logging code at the very beginning of the app, before any other work is done.  Here's the relevant code:

#!/usr/bin/env python3

import sys
import os
#import functions_classes
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import sqlite3
import logging
import inspect
import threading
import datetime

#import local modules
import qt_EnvTabs
import globalnames
import evocontrol
import inputoutput

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        # set up logging
        logging.basicConfig(format='%(levelname)s:%(message)s', filename="sample.log", level=logging.DEBUG)
        logging.info("Starting system, MainWindow.__init__,  %s", str(datetime.datetime.today()))
        self.createUI()
I also have an earlier draft of the application that I saved into another directory.  Its initial code is identical to what I posted here.  I opened it, saw the first window activate, and then closed it.  I checked for the sample.log file, it existed, and contained the proper message:

"INFO:Starting system, MainWindow.__init__,  2017-10-16 20:58:40.988035"

I did the same to the current file, and no log file was created at all!

Between the time that the logging was working and the time it quit, the only changes I made were to add a couple of logging.info() statements into a downstream module.  But that seems irrelevant here, as those modules aren't included in the above test.

Is there any behind-the-scenes behavior that I'm missing?  I'm stumped.
Reply
#2
For anyone who read the above post and also didn't know a solution, I now understand it better.  The truth is that "downstream modules" really aren't so downstream after all.  The modules I imported prior to MainWindow() being called all get parsed to at least some extent, and in one of those local modules I had a call to logging.info() that was placed prior to the __init__() function of a class.  The parser apparently tried to process it, but because the configuration command for logging does't get called until I'm in MainWindow.__init__(), it couldn't do anything with it.  I wish this kind of thing wasn't a silent failure, but it is.

The solution was to move the import logging command to higher up in the list, and follow it immediately with the logging.basicConfig() command before any of the local modules get imported.

That gives you a lot more flexibility on where to place your other calls to the logging module.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  working with TLV module Object Jennifer_Jone 3 1,075 Mar-14-2023, 07:54 PM
Last Post: Jennifer_Jone
  Unsure why module is not working garynewport 0 735 Feb-15-2023, 03:21 PM
Last Post: garynewport
  Pandas module not working Hass 2 1,669 Apr-14-2022, 03:13 PM
Last Post: snippsat
  Spyder stopped working in Windows 10 gammaray 3 3,030 Apr-19-2021, 05:33 PM
Last Post: jefsummers
  importing module - not working jdhamblett 3 2,944 Jun-22-2020, 07:33 PM
Last Post: jdhamblett
  Python logging RotatingFileHandler and TimedRotatingFileHandler is not working with Q waytosatyam7 2 4,726 Dec-24-2019, 08:44 AM
Last Post: buran
  setup() from turtle module not working bobfat 7 5,987 Oct-28-2019, 11:05 AM
Last Post: newbieAuggie2019
  pyinstaller not working with acoustics module pynz 2 2,712 Oct-08-2019, 07:56 AM
Last Post: pynz
  module logging posting randomly DJ_Qu 2 2,186 May-14-2019, 06:41 AM
Last Post: DJ_Qu
  logging: child module unable to get parent config jerryxiao 3 3,304 Apr-09-2019, 04:17 AM
Last Post: jerryxiao

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020