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

Logging to different addressees

McA
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 messages should go to the admin sink AND to
a sink specifically for
a certain addressee.
c) I don't want to write the log message to two different loggers for
that purpose. I would like to do it this way:
common_logger.log('bla') -message to admin sink
certain_logger.log('something' -message to admin sink and addressee-
sink
d) Filtering and debug level should work as expected.

I could I achieve this in a elegant way?

Best regards
Andreas Mock
Jul 15 '08 #1
6 1020
On Jul 15, 1:27 pm, McA <andreas.m...@web.dewrote:
Hi all,

I need a recommendation. I would to like to use theloggingmodule to
create log messages the following way:
a) Every log message does go to a admin sink.
b) Theloggingof special messages should go to the admin sink AND to
a sink specifically for
a certain addressee.
c) I don't want to write the log message to two different loggers for
that purpose. I would like to do it this way:
common_logger.log('bla') -message to admin sink
certain_logger.log('something' -message to admin sink and addressee-
sink
d) Filtering and debug level should work as expected.

I could I achieve this in a elegant way?

Best regards
Andreas Mock
Add a handler to the root logger (or common_logger) to send to the
admin sink.
Add a handler to certain_logger to send to the addressee sink.
If you added the admin sink handler to the root logger, you're done.
Otherwise, you need to ensure that certain_logger is a child of
common_logger.

Regards,

Vinay Sajip
Jul 15 '08 #2
McA
Hi Vinary,

thank you for answering. I start be proud that the author of
the logging package himself is answering. :-)

On 15 Jul., 15:51, Vinay Sajip <vinay_sa...@yahoo.co.ukwrote:
On Jul 15, 1:27 pm, McA <andreas.m...@web.dewrote:


Add a handler to the root logger (or common_logger) to send to the
admin sink.
That's clear.
Add a handler to certain_logger to send to the addressee sink.
That's also clear.
If you added the admin sink handler to the root logger, you're done.
Isn't that the first thing above? What do you mean?
Otherwise, you need to ensure that certain_logger is a child of
common_logger.
What I want to code is something like that.
a) I know thet this is a message for the admin only:
admin_logger.log('blabla') (admin_logger = root_logger =
logging.get_logger(""))

b) certain_logger.log('something' =log to the root/admin/-sink as
well as to the
certain-sink.

Do I have to create a logger subclass where I do the multiplexing of
the logging messages?
I'm pretty sure I miss something. ;-)
>
Regards,

Vinay Sajip
Regards
Andreas Mock

Jul 15 '08 #3
On Jul 15, 5:17 pm, McA <andreas.m...@web.dewrote:
If you added the admin sink handler to the root logger, you're done.

Isn't that the first thing above? What do you mean?
I gave you a choice - to add the handler to the admin_logger OR the
root logger. So I am saying here that if you added to the root logger
(and not common_logger, assuming they're different), then there's
nothing more to do...
>
Otherwise, you need to ensure that certain_logger is a child of
common_logger.

What I want to code is something like that.
a) I know thet this is a message for the admin only:
admin_logger.log('blabla') (admin_logger = root_logger =logging.get_logger(""))

b) certain_logger.log('something' =log to the root/admin/-sink as
well as to the
certain-sink.

Do I have to create a logger subclass where I do the multiplexing of
theloggingmessages?
I'm pretty sure I miss something. ;-)
No, the logging package is designed to allow flexibility of different
events being logged to different destinations. There's no need to
subclass Logger to achieve what you're asking for. The following
script:

# simple.py
import logging

admin_logger = logging.getLogger("") # The root logger
addressee_logger = logging.getLogger("addressee")

admin_sink = logging.FileHandler("admin.log", "w")
addressee_sink = logging.FileHandler("addressee.log", "w")

admin_logger.addHandler(admin_sink)
addressee_logger.addHandler(addressee_sink)

admin_logger.setLevel(logging.DEBUG)

admin_logger.debug("This message appears in admin sink only.")
addressee_logger.debug("This message appears in both admin sink and
addressee sink."

# - end of simple.py

Generates the following results:

----------------------------------------------------------
admin.log
----------------------------------------------------------
This message appears in admin sink only.
This message appears in both admin sink and addressee sink.

----------------------------------------------------------
addressee.log
----------------------------------------------------------
This message appears in both admin sink and addressee sink.

Regards,

Vinay Sajip
Jul 15 '08 #4
McA
Hi Vinay,

thank you for being so patient.

On 16 Jul., 01:21, Vinay Sajip <vinay_sa...@yahoo.co.ukwrote:
On Jul 15, 5:17 pm, McA <andreas.m...@web.dewrote:
If you added the admin sink handler to the root logger, you're done.
Isn't that the first thing above? What do you mean?

I gave you a choice - to add the handler to the admin_logger OR the
root logger. So I am saying here that if you added to the root logger
(and not common_logger, assuming they're different), then there's
nothing more to do...
Reading the rest of your mail let me understand what you meant.
>
# simple.py
import logging

admin_logger = logging.getLogger("") # The root logger
addressee_logger = logging.getLogger("addressee")

admin_sink = logging.FileHandler("admin.log", "w")
addressee_sink = logging.FileHandler("addressee.log", "w")

admin_logger.addHandler(admin_sink)
addressee_logger.addHandler(addressee_sink)

admin_logger.setLevel(logging.DEBUG)

admin_logger.debug("This message appears in admin sink only.")
addressee_logger.debug("This message appears in both admin sink and
addressee sink."
Thank you for that snippet. That means, that the root-logger does
inherit
EVERY message (if it fits to the level and isn't filtered) and the
inheritage chain is build by the chosen logger names, e.g.
messages to logging.getLogger('tree.leave') would also show up in
logging.getLogger('tree') automatically?

If this is true, how can I avoid this "bubbling up" if I would like
to?
(You see, that's a new question, but I want to take the chance to get
the answers from you personally ;-)

Hope not to bother.

Best regards
Andreas Mock

Jul 16 '08 #5
On Jul 16, 8:55 am, McA <andreas.m...@web.dewrote:
Thank you for that snippet. That means, that the root-logger does
inherit
EVERY message (if it fits to the level and isn't filtered) and the
inheritage chain is build by the chosen logger names, e.g.
messages tologging.getLogger('tree.leave') would also show up inlogging.getLogger('tree') automatically?
Yes.
If this is true, how can I avoid this "bubbling up" if I would like
to?
(You see, that's a new question, but I want to take the chance to get
the answers from you personally ;-)

Hope not to bother.
Use the propagate flag, which is mentioned in the documentation. In
fact, please make sure you've reviewed all the documentation before
posting, as the documentation is intended to answer the more
straightforward and common questions which come up.

Best regards,

Vinay Sajip
Jul 16 '08 #6
McA
On 16 Jul., 15:38, Vinay Sajip <vinay_sa...@yahoo.co.ukwrote:
On Jul 16, 8:55 am, McA <andreas.m...@web.dewrote:
messages tologging.getLogger('tree.leave') would also show up inlogging.getLogger('tree') automatically?

Yes.
Ok.
>
Hope not to bother.

Use the propagate flag, which is mentioned in the documentation.
That's the right hint.

In fact, please make sure you've reviewed all the documentation before
posting, as the documentation is intended to answer the more
straightforward and common questions which come up.
I did it and I'll do it again. :-)
You know, sometimes a piece of text/documentation starts to
become information for the reader when he exactly hits the
problem.

Anyway, thank you for your help and for that module.
Best regards,

Vinay Sajip
Best regards
Andreas Mock

Jul 16 '08 #7

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

Similar topics

0
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...
2
by: Tor Erik Sønvisen | last post by:
Hi Have the following code: import logging logging.basicConfig(level = logging.DEBUG, format = ' %(message)s', filename = 'rfs.log', filemode = 'w')
6
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...
3
by: james.p.news | last post by:
I'm new to Python, but I've been thrown into coding a pretty complicated regression testing script. I need to email the log of the daily test to the code owners. I thought I could use SMTPHandler...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.