2

I'm building a GUI program that has several a GUI (main) module and 4 different QThread modules. I want to be able to properly log unhandled exceptions and other various information that I specify. So I decided to use the python's built-in logging module.

This is what I configured it as:

logging.basicConfig(filename="ubc.log", 
                    format='%(asctime)-6s: %(name)s - %(levelname)s - %(module)s - %(funcName)s - %(lineno)d - %(message)s', 
                    level=logging.DEBUG)

Per this config, I wanted the logging module to tell me exactly what line an error/information occurred on, in which module, what function did it take place in, etc. However, this is what I get in the log (when I do logging.info("text")):

2011-12-22 16:06:02,072: root - INFO - logging - info - 1592 - Calling load blog names function, tabWidget index is 0

Needless to say, most of this information is of no use to me. Line number is definitely not 1592, function name is not info, and module is not logging.

To reiterate, what I want is this: when I say logging.info("log this"), I want this to appear in the log:

2011-12-22 16:06:02,072: root - INFO - WorkerThread1.py - upload_function(self, email, param) - line number 131 - log this

Is something like this possible, and if so, how?

EDIT: Per request, I'm adding more code:

class UI(QMainWindow, ui_ui.Ui_MainWindow):

    def __init__(self, parent=None):
        super(BlogCreator, self).__init__(parent)
        self.setupUi(self)
        self.tabWidget.setCurrentIndex(0)

        logging.info("Prda!")

The logging.info("Prda!") is on line 414 of the GUI.py file. As you can see, this message should be written every time the software starts (this is the setupUi). And it is written, but here is what is being written:

2011-12-22 16:53:04,209: root - INFO - logging - info - 1592 - Prda!

EDIT #2 Upon further examination, it seems that it is PyInstaller that somehow screws things up. Running the software directly through the interpreter produces desired results; after the program has been compiled into EXE file, the aforementioned occurs.

2
  • 1
    Source filename, line number and function name are worked out in the logging module in logging/__init__.py:1197 findCaller(). Have you tried stepping thru this function in pdb and see where it goes wrong? What version of python are you using? Commented Dec 23, 2011 at 9:52
  • @lbolla, python 2.7.2 on Windows 7 32bit. Commented Dec 23, 2011 at 10:25

3 Answers 3

3

Can you post a full snippet? If I do what you just said, I get a proper log. I'll post my script here (named stackoverflow_8613924.py), for comparison:

import logging

logging.basicConfig(filename=None, format='%(asctime)-6s: %(name)s - %(levelname)s - %(module)s - %(funcName)s - %(lineno)d - %(message)s', level=logging.DEBUG)

def fname():
    logging.info('hello world')

fname()

Executing it gives me:

$ python stackoverflow_8613924.py 
2011-12-23 09:05:35,678: root - INFO - stackoverflow_8613924 - fname - 6 - hello world
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply, I have added more code to clear things up a bit.
1

I was not able to reproduce your problem. This post contains a very simple example. Please give it a try and tell us if you succeed to get a correct result or not:

myModule.py:

import logging
logging.basicConfig(filename="myLogFile.log", \
    format='%(asctime)-6s: %(name)s - %(levelname)s - %(module)s - %(funcName)s - %(lineno)d - %(message)s', \
    level=logging.DEBUG)

def myFct():
    logging.info("info at module.function level")

logging.info("info at module level")
myFct()

After execution, my log file contains:

2011-12-23 10:35:09,743: root - INFO - myModule - <module> - 11 - info at module level
2011-12-23 10:35:09,743: root - INFO - myModule - myFct - 9 - info at module.class.function level

1 Comment

Hi, I typed this directly into the interpreter and got what you got. Meaning that there's an error somewhere else in my original code, god knows where. Thanks.
0

Maybe the traceback module is what you are looking for :

import sys, traceback

try:
    my_function()
except Exception:
    exc_type, exc_value, exc_traceback = sys.exc_info()
    msg = u"\n".join(traceback.format_tb(exc_traceback))
    logging.error(msg)

1 Comment

Isn't this just for logging exceptions? Also, I don't want to put all of my functions (50+ of them) in try .. except clause.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.