473,662 Members | 2,464 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

logging - how to use in a library?

I'm using the logging module in my comtypes library to log
'interesting' things that happen. In other words, the idea
is if the user of the library is interested in the details that
happen in the package internally, he (she?) would configure
a logging level and handlers that write the log messages where it
is convenient.

This works great, with one exception:
If the script using the library does NOT configure logging, and somewhere the library calls
logger.error(.. .) or logger.critical (...) then he gets a message on stderr saying:

No handlers could be found for logger "foo"

Of course this can be avoided by configuring a NULL-Handler, or raising the
loglevel to a very high value - but why is this necessary?
I would assume that if no handlers are configured than simply the logging
package should not output anything...

Why does logging insist on a default level of ERROR even if unconfigured, and
why does it insist to output something even if no handler is defined?

I assume it would not be a good idea to configure logging in the library itself,
possibly overwriting explicit configuration that the user has done... I don't get it.

Thomas
Aug 26 '08 #1
6 1719
Thomas Heller <th*****@python .netwrites:
If the script using the library does NOT configure logging, and
somewhere the library calls logger.error(.. .) or
logger.critical (...) then he gets a message on stderr saying:

No handlers could be found for logger "foo"
Right. So, part of the requirements for using your library now is that
the caller is responsible for configuring logging before using the
library.
I assume it would not be a good idea to configure logging in the
library itself, possibly overwriting explicit configuration that the
user has done... I don't get it.
The logging system needs to be configured in a way that makes sense
for the application, so it's the application (somewhere near the top
level) that's responsible for configuring it. You're right that this
can't be done without that context; there's no sane default.

You need to document that assumption of your library for those who
will use it.

--
\ “The cost of education is trivial compared to the cost of |
`\ ignorance.” —Thomas Jefferson |
_o__) |
Ben Finney
Aug 26 '08 #2
On Aug 26, 8:05 pm, Thomas Heller <thel...@python .netwrote:
I'm using theloggingmodul e in my comtypes library to log
'interesting' things that happen. In other words, the idea
is if the user of the library is interested in the details that
happen in the package internally, he (she?) would configure
alogginglevel and handlers that write the log messages where it
is convenient.

This works great, with one exception:
If the script using the library does NOT configureloggin g, and somewhere the library calls
logger.error(.. .) or logger.critical (...) then he gets a message on stderr saying:

No handlers could be found for logger "foo"

Of course this can be avoided by configuring a NULL-Handler, or raising the
loglevel to a very high value - but why is this necessary?
I would assume that if no handlers are configured than simply thelogging
package should not output anything...

Why doeslogginginsi st on a default level of ERROR even if unconfigured, and
why does it insist to output something even if no handler is defined?

I assume it would not be a good idea to configureloggin gin the library itself,
possibly overwriting explicit configuration that the user has done... I don't get it.

Thomas
Suppose a user of logging configures it wrongly by mistake, so that
there are no handlers configured. In this case, if the logging system
were not to output anything at all, then you would have no information
at all about why - leading to a longer time to diagnose the problem.
That's the only reason why the message is there, and it's output when
there are no handlers found for an event, and logging.raiseEx ceptions
is 1/True (=a non-production environment), and
Logger.manager. emittedNoHandle rWarning is 0/False (to avoid printing
the message multiple times). As Ben Finney has said, an application's
logging requirements are the determining factor; and I agree that it's
not a good idea to do logging configuration in the library, which
could conflict with what an application developer expects. So
documenting your logging assumpstions is a good approach.

The default level for logging is WARNING, not ERROR - this was judged
to be a good default level, since under most circumstances we're
particularly interested in warnings and errors. (A level of WARNING
catches ERROR and CRITICAL events, too, of course.)

Best Regards,
Vinay Sajip
Aug 27 '08 #3
Vinay Sajip schrieb:
Suppose a user of logging configures it wrongly by mistake, so that
there are no handlers configured. In this case, if the logging system
were not to output anything at all, then you would have no information
at all about why - leading to a longer time to diagnose the problem.
That's the only reason why the message is there, and it's output when
there are no handlers found for an event, and logging.raiseEx ceptions
is 1/True (=a non-production environment), and
Logger.manager. emittedNoHandle rWarning is 0/False (to avoid printing
the message multiple times). As Ben Finney has said, an application's
logging requirements are the determining factor; and I agree that it's
not a good idea to do logging configuration in the library, which
could conflict with what an application developer expects. So
documenting your logging assumpstions is a good approach.
Well, in my library logging is not used to notify the application user
of problems in the library or so, it is used to give the application
developer who is using my library a chance to find out what the library
is doing without the need to step through the code.

So, in my case a call to logger.error or logger.warning does not mean that the
application is using the library in a wrong way, instead it means that
something isn't working in a certain way. These 'errors' or 'warnings'
are handled by the library itself.

Unexpected exceptions are not catched by the library, of course.
I came up with a workaround that seems to do what I want. I add a NULL handler
to my top-level logger which is not the root logger but a logger named 'comtypes',
other loggers are named 'comtypes.somet hing...'. So the application developer
using the library will not see any comtypes logger messages at all, unless
he calls 'logging.basicC onfig()', for example.
The default level for logging is WARNING, not ERROR - this was judged
to be a good default level, since under most circumstances we're
particularly interested in warnings and errors. (A level of WARNING
catches ERROR and CRITICAL events, too, of course.)
Sure - mistake on my side.

Thanks,
Thomas
Aug 27 '08 #4
On Aug 27, 11:28 am, Thomas Heller <thel...@python .netwrote:
Vinay Sajip schrieb:

I came up with a workaround that seems to do what I want. I add a NULL handler
to my top-level logger which is not the root logger but a logger named 'comtypes',
other loggers are named 'comtypes.somet hing...'. So the application developer
using the library will not see any comtypes logger messages at all, unless
he calls 'logging.basicC onfig()', for example.
This is a good solution - I'll add this as a suggestion in the
documentation.

Regards,

Vinay Sajip
Aug 27 '08 #5
Vinay Sajip schrieb:
On Aug 27, 11:28 am, Thomas Heller <thel...@python .netwrote:
>>
I came up with a workaround that seems to do what I want. I add a NULL handler
to my top-level logger which is not the root logger but a logger named 'comtypes',
other loggers are named 'comtypes.somet hing...'. So the application developer
using the library will not see any comtypes logger messages at all, unless
he calls 'logging.basicC onfig()', for example.

This is a good solution - I'll add this as a suggestion in the
documentation.
Cool.

BTW: Let me say that the more I use logging the more I like it.
logging is a fantastic package!

Thanks,
Thomas
Aug 29 '08 #6
On Aug 29, 11:02 pm, Thomas Heller <thel...@python .netwrote:
>
BTW: Let me say that the more I useloggingthe more I like it.loggingis a fantastic package!
Thank you, I can say the same for ctypes and py2exe :-)

Regards,

Vinay Sajip
Sep 1 '08 #7

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

Similar topics

0
2254
by: Tero Saarni | last post by:
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...
6
2206
by: Eric DeWall | last post by:
In trying to clean up the inevitable debug printing littered through some code, I started reading up on the 'logging' module. Browsing groups indicates that the design of the module is controversial but personally I'm very happy to see such a full-featured logger as part of the standard distribution. Here's my problem though (using 2.3.3) - I'm trying to write some reusable classes that _optionally_ use the logging module. So a class...
10
3296
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 runs? I would like to setup the logging so that there is no logging when nothing is configured, and no warning messages are printed. Thomas
7
9329
by: amitos | last post by:
Hi! I consider using the Logging Application Block, Enterprise Library, Jan 2006 in a major project. To date, I've found that some of its features are not very well documented. One important thing is logging to a database. I tried configuring a database trace listener in the "Enterprise Library Configuration". There I'm stuck. Stored procedures? Which ones exactly? What parameters do they take? I'm pretty confused. It also seems it...
3
2586
by: Dick | last post by:
How do I integrating ASP.NET Tracing with the Enterprise Library Logging Application Block? I guess this should be possible and in a similar way to the way ASP.NET Tracing is integrated with System.Diagnostics Tracing as described in http://msdn2.microsoft.com/en-us/library/b0ectfxd(VS.80).aspx. But I would have thought this is something most ASP developers using the Logging Application Block would want to do and therefore I expected to...
0
1856
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 want to put this comment and a alternative most simple default framework for discussion. Maybe there are more Python users which like to see that imported (managed) logging issue more down-to-earth and flexible ? ... Vinay Sajip wrote: >...
2
1852
by: Tod Olson | last post by:
Anyone have advice for importing the logging module using MacPython 2.4.3? MacPython installs the logging module in: /Library/Frameworks/Python.framework/Versions/2.4/lib/logging/ There's an __init__.py there and everything, but this directory is not in sys.path. I add it to sys.path as follows: /Library/Frameworks/Python.framework/Versions/2.4/lib/logging
9
1950
by: Rasika WIJAYARATNE | last post by:
Hi guys, Please check this out: http://rkwcoding.blogspot.com/2007/07/error-logging.html
4
3394
by: Alexandru Mosoi | last post by:
why doesn't logging throw any exception when it should? how do I configure logging to throw exceptions? .... logging.fatal('asdf %d', '123') .... except: .... print 'this line is never printed' .... Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.5/lib/ python2.5/logging/__init__.py", line 744, in emit
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8856
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8545
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7365
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
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 presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
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 we have to send another system
2
1992
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.