473,387 Members | 1,348 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,387 software developers and data experts.

Logging ancestors ignored if config changes?


Hi,

I am seeing some odd behaviour with logging which would be explained
if loggers that are not defined explicitly (but which are controlled
via their ancestors) must be created after the logging system is
configured via fileConfig().

That's a bit abstract, so here's the problem itself: I define my log
within a module by doing

import logging
log = logging.getLogger(__name__)

Now typically __name__ will be something like "acooke.utils.foo".

That happens before the application configures logging, which it does
by calling logging.config.fileConfig() to load a configuration.

If I do that, then I don't see any logging output from
"acooke.utils.foo" (when using "log" from above after "fileConfig" has
been called) unless I explicitly define a logger with that name.
Neither root nor an "acooke" logger, defined in the config file, are
called.

Is this a bug? Am I doing something stupid? To me the above seems
like a natural way of using the system...

Thanks,
Andrew
Jun 27 '08 #1
2 1068
One way to handle this is to make creation of module-level Loggers
lazy, and make sure that logging initialisation occurs before any
other logging is actually used (which is not so hard - just init log
at the start of the application).

Of course, there's a performance hit...

For example:

class Log(object):
def __init__(self, name):
super(Log, self).__init__()
self._name = name
self._lazy = None
def __getattr__(self, key):
if not self._lazy:
self._lazy = logging.getLogger(self._name)
return getattr(self._lazy, key)

and then, in some module:

from acooke.util.log import Log
log = Log(__name__)
[...]
class Foo(object):
def my_method(self):
log.debug("this works as expected")

Andrew
Jun 27 '08 #2
On 26 Apr, 04:02, andrew cooke <and...@acooke.orgwrote:
Hi,

I am seeing some odd behaviour withloggingwhich would be explained
if loggers that are not defined explicitly (but which are controlled
via their ancestors) must be created after theloggingsystem is
configured via fileConfig().

That's a bit abstract, so here's the problem itself: I define my log
within a module by doing

importlogging
log =logging.getLogger(__name__)

Now typically __name__ will be something like "acooke.utils.foo".

That happens before the application configureslogging, which it does
by callinglogging.config.fileConfig() to load a configuration.

If I do that, then I don't see anyloggingoutput from
"acooke.utils.foo" (when using "log" from above after "fileConfig" has
been called) unless I explicitly define a logger with that name.
Neither root nor an "acooke" logger, defined in the config file, are
called.

Is this a bug? Am I doing something stupid? To me the above seems
like a natural way of using the system...

Thanks,
Andrew
It's not a bug, it's by design (for better or worse). A call to
fileConfig disables all existing loggers which are not explicitly
named in the new configuration. Configuration is intended for one-off
use rather than in an incremental mode.

Better approaches would be to defer creation of the loggers
programmatically until after the configuration call, or else to name
them explicitly in the configuration.

Regards,

Vinay Sajip
Jun 27 '08 #3

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

Similar topics

3
by: Andreas Jung | last post by:
I am trying to write a custom logger with a custom handler: class MyHandler(StreamHandler): pass class Logger: def __init__(self): self._l = logging.getLogger('someident')...
1
by: Rene Pijlman | last post by:
I'd expect the program below to put only one line with "Eat me" in the log file. However, I get this with Python 2.4.2 in WingIDE on Windows XP: Eat me Ignore me My understanding is: 1....
10
by: Baurzhan Ismagulov | last post by:
Hello all, I want that each module has its own logger. I've defined the following config file: keys=f01 keys=console
1
by: cowznofsky | last post by:
I'm using version 3.1.0.0. I've created a web service project and am logging info to the Event Log. Originally I experimented with the Quick Start Logging App. Then I copied some of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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
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
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...

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.