473,378 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Logging with callers filename and line number


I'm using Logging in my library module for producing trace log of
certain events.

For being more useful these log entries should be associated with the
filename and line number of *users* code that was issuing the library
call. This is similar to what Logging itself does since it doesn't
print "logging/__init__.py" as value of %(filename) but instead it
prints users file where logging method (info, error...) was called.

What would be the best way to achieve this also for other modules?
I took a quick look into the code in Logging module to see if it
provides means for controlling what goes into %(filename) and %(lineno)
but I didn't find any. I then ended up in subclassing Logger and
redefining Logger.findCaller() to skip a list of files instead of just
one file. Then in my library module I can add current file name into
the list of file names which logger will skip.

Does this seem like sensible solution for you:
import logging
import sys
import os

srcfilelist = [ logging._srcfile, os.path.normcase(__file__) ]

class MyLogger(logging.Logger):

def findCaller(self):
f = sys._getframe(1)
while 1:
co = f.f_code
filename = os.path.normcase(co.co_filename)
if filename in srcfilelist:
f = f.f_back
continue
return filename, f.f_lineno

logging.setLoggerClass(MyLogger)

---
Tero

Jul 18 '05 #1
0 2177

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: j vickroy | last post by:
Hello, I'm trying to understand the behavior of the Python 2.3 logging module (MS Windows 2k) with regard to RotatingFileHandler. The following script illustrates a puzzling problem. What is...
0
by: Marek Augustyn | last post by:
Hello, I'm getting strange exception raised using module logging. It's strange because it happens only sometimes, and it happens in different places (but always in a logging method). i.e. this...
2
by: Tor Erik Sønvisen | last post by:
Hi Have the following code: import logging logging.basicConfig(level = logging.DEBUG, format = ' %(message)s', filename = 'rfs.log', filemode = 'w')
10
by: Thomas Heller | last post by:
I'm about to add some logging calls to a library I have. How can I prevent that the script that uses the library prints 'No handlers could be found for logger "comtypes.client"' when the script...
7
by: Leo Breebaart | last post by:
I have another question where I am not so much looking for a solution but rather hoping to get some feedback on *which* solutions people here consider good Pythonic ways to approach a issue. ...
7
by: flupke | last post by:
Hi, i'm getting errors with the log module concerning RotatingFileHandler. I'm using Python 2.4.3 on Windows XP SP2. This used to work in previous python versions but since i upgraded to 2.4.3...
12
by: Tekkaman | last post by:
I'm getting a strange behaviour from the "pathname" and "lineno" formatter mapping keys. Instead of my file and my line number I get: /usr/lib/python2.4/logging/__init__.py as the file, and...
3
by: seb | last post by:
Hi, I am writing to a file some basic information using the logging module. It is working but in the log file some line are printed several time. I had put some print debugging messages in the...
2
by: scriptlearner | last post by:
OS: Solaris 9 Python Version: 2.4.4 I need to log certain data in a worker thread; however, I am getting an error now when I use two worker threads. I think the problem comes from the line...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.