473,492 Members | 4,301 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

logging from severl classes

Hi,

i'm trying to log to the same file (using logging module) from different
classes but i can't seem to get it to work.
File 1 is the main file which sets up the logger and then i want to also
use the same logger in another class
This is what i have

file1
=============================
import logging

class A:
def __init__(self):
self.logger = logging.getLogger("app_logfile")
### stdout handler ###
self.logger.setLevel(logging.DEBUG)

stream_hld = logging.StreamHandler()
stream_hld.setLevel(logging.DEBUG)
stream_formatter =
logging.Formatter('%(asctime)s %(levelname)s %(module)s %(lineno)d
%(message)s ')
stream_hld.setFormatter(stream_formatter)
self.logger.addHandler(stream_hld)")
=============================

file2
=============================
import logging

class B:
def __init__(self):
self.logger = logging.getLogger("app_logfile")
self.logger.debug("creating class B")

def otherfunction(self):
self.logger.debug("in otherfunction")
=============================

But i get an error:
AttributeError: B instance has no attribute 'logger'

I was hoping that by requesting a logger with the same name
(app_logfile) i would get the setup also and could use the logger
straight away. Funny thing is that the use of the logger in the __init__
function of class B works but the call to the logger in otherfunction
doesn't work and results in the error above.
Class B is created after class A so the logger object exists already.

Any ideas of what i'm doing wrong and how i can achieve setting up the
logger object only once?

Thanks,
Benedict
Jul 18 '05 #1
1 1573
Vinay Sajip wrote:
It works for me:

#file3.py
import file1
import file2

a = file1.A()
b = file2.B()
b.otherfunction()

gives

2004-12-28 00:18:34,805 DEBUG file2 6 creating class B
2004-12-28 00:18:34,805 DEBUG file2 9 in otherfunction


yeah, the classes where a simplification of the classes i'm using and
used these to just show what i wanted to do without testing them.
they indeed work. strange.
Thanks for your answer

benedict
Jul 18 '05 #2

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

Similar topics

4
3251
by: Jan van Mansum | last post by:
Hello group, I have just been trying out the log4j library. As far as I understand the idea is to keep the logging statements in your, even in the release build. However, obviously you wouldn't...
5
7985
by: Ravi Shankar | last post by:
Hi all, I have an enterprise application. I am using Apache Log4J for the logging purposes. WHen a request is received by the application, it goes throug servlets and many classes, and the...
0
4637
by: Joe Bloggs | last post by:
Hi all, I take the pleasure to inform that Dotnet Commons Logging has been released for use. Dotnet Commons Logging, a subproject of the Donet Commons project currently located under the...
6
7297
by: pmatos | last post by:
Hi all, I am trying to create a simple but efficient C++ logging class. I know there are lots of them out there but I want something simple and efficient. The number one requirement is the...
7
1482
by: John Bailo | last post by:
I have a .exe that calls a .dll I want to write log messages at various parts of the process in the .exe and .dll to a common trace/log file. I set up a method in the .exe to write to the...
0
1831
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...
7
8527
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...
19
6049
by: =?Utf-8?B?Sko=?= | last post by:
I have a logging component that I will access in other assemblies. So it was brought up to me that I should pass an instance around to these components instead of just making the logging class...
3
6379
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
7118
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
7157
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
7192
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...
0
5452
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,...
0
4579
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3087
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
1397
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
637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.