473,696 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Choosing log file destination in logging configuration file

Hey,

I have a sort of petty-neurotic question, I'm kinda pedantic and like
to keep my log directories clean, and this thing is bothering me to
the point of actually posting a question (couldn't find any post about
this..).

My application has two types of components: daemons and regular ones
that are run every on crontab. Because the log structure is very
similar, I want both to use the same logging configuration file.

For clarity, I want all of the files to have the date as part of the
filename.
The TimedRotatingFi leHandler has it built in (once it rotates), and I
found a dirty hack to make it work for the FileHandler (see shameful
code below). Basically I'm all set - have the daemons use the rotating
handler, since they're, um, daemonic and on all the time, and have the
regular components use the regular handler, using the dirty hack to
insert the date into the filename.

So far so good. In the relevant applications, the code looks something
like this:
logging.config. fileConfig('log .ini')
logger = logging.getLogg er('log.regular ') <orlogger =
logging.getLogg er('log.daemoni c')
... and start logging.

The thorn in my side is that after the fileConfig call, BOTH handlers
are instantiated, meaning both types of files are created for every
component, even if I don't need it: component.log (for the rotating
handler) and compenent.date. log (for the regular file handler).

...So finally, here's my question:
Apart from splitting the logging configuration into two separate
files, is there any way to NOT create the file until you actually use
it?

Here's the logging configuration file for reference.. Thanks !

[loggers]
keys=root,regul ar,daemonic

[handlers]
keys=fileHandle r,consoleHandle r,timedRotating FileHandler

[formatters]
keys=simpleForm atter

[logger_root]
level=DEBUG
handlers=consol eHandler

[logger_regular]
level=INFO
handlers=fileHa ndler,consoleHa ndler
propagate=0
qualname=log.re gular

[logger_daemonic]
level=INFO
handlers=timedR otatingFileHand ler,consoleHand ler
propagate=0
qualname=log.da emonic

[handler_timedRo tatingFileHandl er]
class=handlers. TimedRotatingFi leHandler
level=INFO
formatter=simpl eFormatter
args=('mylog.lo g', 'midnight')

[handler_fileHan dler]
class=FileHandl er
level=INFO
formatter=simpl eFormatter
; sorry about the grossness
args=('mylog.' + str(time.localt ime().tm_year) + '_' +
str(time.localt ime().tm_mon).z fill(2) + '_' +
str(time.localt ime().tm_mday). zfill(2) + '.log', 'a')

[handler_console Handler]
class=StreamHan dler
level=DEBUG
formatter=simpl eFormatter
args=(sys.stdou t,)

[formatter_simpl eFormatter]
format=%(asctim e)s - %(filename)s - %(name)s - %(levelname)s - %
(message)s
Jun 27 '08 #1
3 2222
Hello,
So far so good. In the relevant applications, the code looks something
like this:
logging.config. fileConfig('log .ini')
logger = logging.getLogg er('log.regular ') <orlogger =
logging.getLogg er('log.daemoni c')
.. and start logging.

The thorn in my side is that after the fileConfig call, BOTH handlers
are instantiated, meaning both types of files are created for every
component, even if I don't need it: component.log (for the rotating
handler) and compenent.date. log (for the regular file handler).

..So finally, here's my question:
Apart from splitting the logging configuration into two separate
files, is there any way to NOT create the file until you actually use
it?
You can generate the .ini file on the fly and then load it:
>>log_ini = gen_log_ini(typ e) # type is either "deamon" or "regular"
atexit.regist er(lambda: remove(log_ini) )
logging.confi g.fileConfig(lo g_ini)
HTH,
--
Miki <mi*********@gm ail.com>
http://pythonwise.blogspot.com
Jun 27 '08 #2
On Apr 22, 12:57 pm, Miki <miki.teb...@gm ail.comwrote:
Hello,
So far so good. In the relevant applications, the code looks something
like this:
logging.config. fileConfig('log .ini')
logger = logging.getLogg er('log.regular ') <orlogger =
logging.getLogg er('log.daemoni c')
.. and start logging.
The thorn in my side is that after the fileConfig call, BOTH handlers
are instantiated, meaning both types of files are created for every
component, even if I don't need it: component.log (for the rotating
handler) and compenent.date. log (for the regular file handler).
..So finally, here's my question:
Apart from splitting the logging configuration into two separate
files, is there any way to NOT create the file until you actually use
it?

You can generate the .ini file on the fly and then load it:
>log_ini = gen_log_ini(typ e) # type is either "deamon" or "regular"
atexit.registe r(lambda: remove(log_ini) )
logging.config .fileConfig(log _ini)

HTH,
--
Miki <miki.teb...@gm ail.com>http://pythonwise.blogspot.com

I think it misses the point of having a file to config..
It looks as if there's no solution. A peek at the logging module shows
this:
klass = cp.get(sectname , "class")
...
klass = eval(klass, vars(logging))
args = cp.get(sectname , "args")
args = eval(args, vars(logging))
h = apply(klass, args)

Bah. I'll have to split them into two configuration files (or subclass
and have the respective file and rotating handlers lazy evaluate until
the actual log call is made).
Thanks though.
Jun 27 '08 #3
On Apr 22, 9:48 pm, guyben...@gmail .com wrote:
On Apr 22, 12:57 pm, Miki <miki.teb...@gm ail.comwrote:
Hello,
So far so good. In the relevant applications, the code looks something
like this:
>logging.config .fileConfig('lo g.ini')
logger =logging.getLog ger('log.regula r') <orlogger =
>logging.getLog ger('log.daemon ic')
.. and startlogging.
The thorn in my side is that after the fileConfig call, BOTH handlers
are instantiated, meaning both types of files are created for every
component, even if I don't need it: component.log (for the rotating
handler) and compenent.date. log (for the regular file handler).
..So finally, here's my question:
Apart from splitting theloggingconfi guration into two separate
files, is there any way to NOT create the file until you actually use
it?
You can generate the .ini file on the fly and then load it:
>>log_ini = gen_log_ini(typ e) # type is either "deamon" or "regular"
>>atexit.regist er(lambda: remove(log_ini) )
>>>logging.conf ig.fileConfig(l og_ini)
HTH,
--
Miki <miki.teb...@gm ail.com>http://pythonwise.blogspot.com

I think it misses the point of having a file to config..
It looks as if there's no solution. A peek at theloggingmodul e shows
this:
klass = cp.get(sectname , "class")
...
klass = eval(klass, vars(logging))
args = cp.get(sectname , "args")
args = eval(args, vars(logging))
h = apply(klass, args)

Bah. I'll have to split them into two configuration files (or subclass
and have the respective file and rotating handlers lazy evaluate until
the actual log call is made).
Thanks though.
Not sure if it's any help - the SVN version contains a new optional
"delay" parameter for FileHandler and subclasses (including the
rotating handlers) which delays opening the file until there's a need
to, i.e. when an emit() call occurs.

Regards,

Vinay Sajip
Jun 27 '08 #4

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

Similar topics

23
2214
by: Rotem | last post by:
Hi, while working on something in my current project I have made several improvements to the logging package in Python, two of them are worth mentioning: 1. addition of a logging record field %(function)s, which results in the name of the entity which logged the record. My version even deduces the class name in the case which the logger is a bound method, and assuming the name of the "self" variable is indeed "self".
6
1932
by: Robert | last post by:
Hi, I'm creating simple logging function in my server application but i've come to a little problem. I use fopen and fprintf to open and print to that file and after the application quits fclose ofcourse but the data get's only written when i use fclose, i need something so it gets written to the file directly because otherwise the logging won't have much effect because i want to run the program for long times. Is there any function to...
0
1630
by: mrkbrndck | last post by:
I would like to open a log file in a windows UI that corresponds to the file maintained in the loggingDistributorConfig.config file. Has any one done this? Below is the code I tried to use and the loggingDistributorConfig.config xml. Thanks in Advance, Mark //start by capturing the configuration of the Logging with a logging configuration view object
3
4433
by: Eric | last post by:
Help! I created a XML schema with a Visual Studio tools. I'm filling a dataset with a DataAdapter. Before I use the "WriteXml" method to write the data to a xml file, I want to map the XSD file I created to the dataset so that when I do use the "WriteXml" method, the generated xml file will be properly formatted to the schema I created. I did try using the "ReadXMLSchema" but it didn't work. Please help me!
1
6493
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
0
2112
by: rajesh.hanchate | last post by:
Please help me in resolving this issue. I am using EnterpriseLibrary 2.0 Exception and logging block for logging exceptions to event log. It works fine for sometime. After some time it stops logging to event log. To make it active again I need to open the web.config and save without making any changes. It starts to work again. Here is the configuration I am using. <exceptionHandling>
4
3327
by: Frank Aune | last post by:
Hello, I've been playing with the python logging module lately, and I manage to log to both stderr and MySQL database. What I'm wondering, is if its possible to specify the database handler in a config file like: class=DBHandler
1
8328
by: Steve Wofford | last post by:
I am recieving the following. This happens when I moved it from my development system to our production SBS 2003 w/ latest .net frameworks and service packs . I developed under XPSP2 and VS2008. The error is pointing to the Web.Config of my application. System.Data.Entity was missing from the server, but was on the dev system. I tried to add, but didnt help: C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5
1
47460
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
8667
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8597
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9148
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
6515
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
5857
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
4357
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3034
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
2319
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1992
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.