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

Logging to zero or more destinations

In the Python 2.5 Library Reference, section 14.5.3 (Logging to
multiple destinations), an example is given of logging to both a file
and the console. This is done by using logging.basicConfig() to
configure a log file, and then calling
logging.getLogger('').addHandler(console) to add the console.

However, in section 14.5.4 (Sending and receiving logging events
across a network), a call is made to
rootLogger.addHandler(socketHandler), and later it is stated that "On
the client side, nothing is printed on the console".

Finally, back in section 14.5.2 (Basic example), it's stated that "by
default, the root logger is configured to only handle messages with a
severity of WARNING or above. The message format is also a
configuration default, as is the output destination of the messages -
sys.stderr."

The only way that I can see for all three statements to be consistent
is that the root logger starts with an empty list of handlers, and
doesn't instantiate a default handler until either
logging.basicConfig() is called, or the first time that a message is
logged. This would also seem to imply that there's no way to use an
empty handler list (say, if you want to suppress all logging), because
the root handler will instantiate a handler for you. Is this correct?

P.S. I tried researching this further by myself, but the logging
module doesn't come with source (apparently it's written in C?) and I
don't have the time to find and download the source to my laptop.

Thanks!
Jul 8 '08 #1
4 1594
samwyse <sa*****@gmail.comwrites:
In the Python 2.5 Library Reference, section 14.5.3 (Logging to
multiple destinations), an example is given of logging to both a file
and the console. This is done by using logging.basicConfig() to
configure a log file, and then calling
logging.getLogger('').addHandler(console) to add the console.

However, in section 14.5.4 (Sending and receiving logging events
across a network), a call is made to
rootLogger.addHandler(socketHandler), and later it is stated that "On
the client side, nothing is printed on the console".

Finally, back in section 14.5.2 (Basic example), it's stated that "by
default, the root logger is configured to only handle messages with a
severity of WARNING or above. The message format is also a
configuration default, as is the output destination of the messages -
sys.stderr."

The only way that I can see for all three statements to be consistent
is that the root logger starts with an empty list of handlers, and
doesn't instantiate a default handler until either
logging.basicConfig() is called,
That is correct.
or the first time that a message is
logged.
That is not correct. The list of handlers is empty until `basicConfig`
or explicit `addHandler` is called.
This would also seem to imply that there's no way to use an
empty handler list (say, if you want to suppress all logging), because
the root handler will instantiate a handler for you. Is this correct?
No. Consider this:
>>import logging
logging.root.warning('error message')
No handlers could be found for logger "root"
>>logging.root.warning('error message')
Note only one warning message.

P.S. I tried researching this further by myself, but the logging
module doesn't come with source (apparently it's written in C?) and I
don't have the time to find and download the source to my laptop.
Hmmm... that's strange. It is a pure Python package.

$ ls /usr/lib/python2.5/logging/
config.py config.pyc handlers.py handlers.pyc __init__.py __init__.pyc

HTH,
Rob
Jul 8 '08 #2
samwyse wrote:
In the Python 2.5 Library Reference, section 14.5.3 (Logging to
multiple destinations), an example is given of logging to both a file
and the console. This is done by using logging.basicConfig() to
configure a log file, and then calling
logging.getLogger('').addHandler(console) to add the console.

However, in section 14.5.4 (Sending and receiving logging events
across a network), a call is made to
rootLogger.addHandler(socketHandler), and later it is stated that "On
the client side, nothing is printed on the console".

Finally, back in section 14.5.2 (Basic example), it's stated that "by
default, the root logger is configured to only handle messages with a
severity of WARNING or above. The message format is also a
configuration default, as is the output destination of the messages -
sys.stderr."

The only way that I can see for all three statements to be consistent
is that the root logger starts with an empty list of handlers, and
doesn't instantiate a default handler until either
logging.basicConfig() is called, or the first time that a message is
logged. This would also seem to imply that there's no way to use an
empty handler list (say, if you want to suppress all logging), because
the root handler will instantiate a handler for you. Is this correct?
Sort of. Your analysis of what happens is entirely correct (see below). However,
a way to suppress all logging is to have a single Handler where the emit()
method does nothing. Or you can set the level above CRITICAL.

$ python
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>import logging
logging.warn('foo')
WARNING:root:foo
>>logger = logging.getLogger()
logger.handlers
[<logging.StreamHandler instance at 0xb43788>]
>>>
$ python
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04)
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>import logging
logger = logging.getLogger()
logger.handlers
[]
>>logging.basicConfig()
logger.handlers
[<logging.StreamHandler instance at 0xb43788>]
>>logger.handlers = []
class NullHandler(logging.Handler):
.... def emit(self, record):
.... pass
....
>>logger.addHandler(NullHandler())
logging.warn('foo')
P.S. I tried researching this further by myself, but the logging
module doesn't come with source (apparently it's written in C?) and I
don't have the time to find and download the source to my laptop.
No it's all Python. Look in c:\Python25\Lib\logging\ .

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jul 8 '08 #3
Rob Wolfe wrote:
samwyse <sa*****@gmail.comwrites:
>The only way that I can see for all three statements to be consistent
is that the root logger starts with an empty list of handlers, and
doesn't instantiate a default handler until either
logging.basicConfig() is called,

That is correct.
>or the first time that a message is
logged.

That is not correct. The list of handlers is empty until `basicConfig`
or explicit `addHandler` is called.
>This would also seem to imply that there's no way to use an
empty handler list (say, if you want to suppress all logging), because
the root handler will instantiate a handler for you. Is this correct?

No. Consider this:
>>>import logging
logging.root.warning('error message')
No handlers could be found for logger "root"
>>>logging.root.warning('error message')

Note only one warning message.
Ah, right. It is the module-level functions logging.warn(), etc. that invoke
basicConfig() if no handler is present.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jul 8 '08 #4
On Jul 8, 3:01*pm, Rob Wolfe <r...@smsnet.plwrote:
samwyse <samw...@gmail.comwrites:
P.S. *I tried researching this further by myself, but the logging
module doesn't come with source (apparently it's written in C?) and I
don't have the time to find and download the source to my laptop.

Hmmm... that's strange. It is a pure Python package.

$ ls /usr/lib/python2.5/logging/
config.py *config.pyc *handlers.py *handlers.pyc *__init__.py *__init__.pyc

HTH,
Rob
Oops, my bad. I was using IDLE and tried the "Open Module..." command
on logging, not logging.something.
Jul 9 '08 #5

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

Similar topics

12
by: Rob Cranfill | last post by:
Hello, I've successfully coded Python to do what I want with a RotatingFileHandler, but am having trouble getting the same behavior via a config file. I wanted to create one log file each...
0
by: Michael CATANZARITI | last post by:
It's a flexible and highly configurable logging framework Main features : - Configurable logging destinations (appenders) - Configurable logging format (layouts) - Categorized logging statements...
0
by: Karuppasamy | last post by:
H I am trying to use the Logging Module provided by Microsoft Application Blocks for .Net I installed everything as per the Instructions given in the 'Development Using the Logging Block' ...
2
by: Chumma Dede | last post by:
Hi, I need to code a DLL in .NET which logs the response times for our asp.net multi-tier application. The problem is we need to log the timestamps at multiple stages in a process lifecycle...
2
by: chuck | last post by:
I want to create two different loggers so that users see one output (e.g. no exception/stack traces) and others (e.g support staff) that does see stack traces via email. The code below is close...
0
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...
2
by: nyhetsgrupper | last post by:
Hi, Is it possible to configure the enterprise library logging block to log to different destinations based on priority. I can see it is possible to do this based on category, but would it not be...
17
by: Cramer | last post by:
I plan to implement an exception logging feature in an ASP.NET Web application that writes encountered exceptions to disk. The exception data will be stored as XML. I am planning on having each...
6
by: McA | last post by:
Hi all, I need a recommendation. I would to like to use the logging module to create log messages the following way: a) Every log message does go to a admin sink. b) The logging of special...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.