473,509 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

logging module: add client_addr to all log records

Hi!

I'm writing a server and I want to use the logging module for logging
stuff. I want to log all transactions in detail, and if everything goes
haywire I want to know which client did it. Therefore I want to affix
the client address to every single log item.

I would like to do the following:
Formatter("[%(client_addr)s]: %(message)s")
....
logger.critical("Offal Barrage Detected: Running Away", client_addr =
'french.knights.org')

I've written something that accomplishes this (example below), but I
have this nagging feeling that this could have been done in a better
way. Any ideas?

When test.py executes it prints this:
[WARNING|2006-05-08 23:44:57,421|internal]: Unable to Pronounce 'ni!'
[WARNING|2006-05-08 23:44:57,421|french.knights.nu]: Incoming Bovine
Detected

I'm grateful for any advice.

Cheers!
/Joel Hedlund

test.py
---------------------------------------------------------------------
import logging
import cowlogger
logger = logging.getLogger('cowlogger')
logger.warning("Unable to Pronounce 'ni!'")
logger.warning("Incoming Bovine Detected",
client_addr = 'french.knights.nu')
---------------------------------------------------------------------

cowlogger.py
---------------------------------------------------------------------
import logging

BaseLogger = logging.getLoggerClass()

class LogRecord(logging.LogRecord):
def __init__(self, name, level, pathname, lineno, msg, args,
exc_info, client_addr):
"""
Adding client_addr as instantiation parameter and
attribute.
"""
logging.LogRecord.__init__(self, name, level, pathname,
lineno, msg, args, exc_info)
self.client_addr = client_addr

class CowLogger(BaseLogger):
def makeRecord(self, name, level, fn, lno, msg, args, exc_info,
client_addr):
"""
Code nicked from logging.Logger. I just added client_addr
to header and LogRecord instantiation.
"""
return LogRecord(name, level, fn, lno, msg, args, exc_info,
client_addr)

def _log(self, level, msg, args, exc_info=None,
client_addr = 'internal'):
"""
Code nicked from logging.Logger. I just added client_addr
to header and makeRecord call.
"""
if logging._srcfile:
fn, lno, func = self.findCaller()
else:
fn, lno, func = "(unknown file)", 0, "(unknown function)"
if exc_info:
if type(exc_info) != types.TupleType:
exc_info = sys.exc_info()
record = self.makeRecord(self.name, level, fn, lno, msg,
args, exc_info, client_addr)
self.handle(record)

logging.setLoggerClass(CowLogger)

# Setting up log item formatting:
format = "[%(levelname)s|%(asctime)s|%(client_addr)s]: %(message)s"
formatter = logging.Formatter(format)
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger = logging.getLogger('cowlogger')
logger.addHandler(handler)
---------------------------------------------------------------------

May 8 '06 #1
1 2252
> See a very similar example which uses the new 'extra' keyword argument:

Now that's brilliant! Exactly what I need.

But unfortunately, it's also unavailable until 2.5 comes out. Until then I'm
afraid I'm stuck with my shoddy hack... but it's always nice to know the time
will come when I can finally throw it out!

Thanks for taking the time!

Cheers!
/Joel
May 11 '06 #2

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

Similar topics

5
1260
by: Kamus of Kadizhar | last post by:
Does anyone have a working example or a recipe of a simple logging system using the logging module? Any FM I can refer to? I can't seem to get it right - I can't even make sense of the standard...
0
1646
by: Robert.Schmitt | last post by:
I found that the configuration system of the new logging package of Python 2.3 has some unintuitive idiosyncracies that are worth mentioning because they can cost you quite some development time...
6
10863
by: Ville Vainio | last post by:
Just posting this for the sake of google: Like everyone else, I figured it's time to start using the 'logging' module. I typically want to dump "info" level (and up) log information to...
2
1730
by: rh0dium | last post by:
Hi all, So I have a slice of code which calls other python code. I have started to take a real liking to the logging module, but I want to extend this into the called python code. I have no...
23
2187
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field...
7
2413
by: Jed Parsons | last post by:
Hi, I'm using the logging module for the first time. I'm using it from within Zope Extensions. My problem is that, for every event logged, the logger is producing multiple identical entries...
0
1837
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
4
3306
by: Frank Aune | last post by:
Hello, I've been playing with the python logging module lately, and I manage to log to both stderr and MySQL database. What I'm wondering, is if its possible to specify the database handler in...
3
6386
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.