472,805 Members | 1,316 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 TimedRotatingFileHandler 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.getLogger('log.regular') <orlogger =
logging.getLogger('log.daemonic')
... 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,regular,daemonic

[handlers]
keys=fileHandler,consoleHandler,timedRotatingFileH andler

[formatters]
keys=simpleFormatter

[logger_root]
level=DEBUG
handlers=consoleHandler

[logger_regular]
level=INFO
handlers=fileHandler,consoleHandler
propagate=0
qualname=log.regular

[logger_daemonic]
level=INFO
handlers=timedRotatingFileHandler,consoleHandler
propagate=0
qualname=log.daemonic

[handler_timedRotatingFileHandler]
class=handlers.TimedRotatingFileHandler
level=INFO
formatter=simpleFormatter
args=('mylog.log', 'midnight')

[handler_fileHandler]
class=FileHandler
level=INFO
formatter=simpleFormatter
; sorry about the grossness
args=('mylog.' + str(time.localtime().tm_year) + '_' +
str(time.localtime().tm_mon).zfill(2) + '_' +
str(time.localtime().tm_mday).zfill(2) + '.log', 'a')

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleFormatter
args=(sys.stdout,)

[formatter_simpleFormatter]
format=%(asctime)s - %(filename)s - %(name)s - %(levelname)s - %
(message)s
Jun 27 '08 #1
3 2160
Hello,
So far so good. In the relevant applications, the code looks something
like this:
logging.config.fileConfig('log.ini')
logger = logging.getLogger('log.regular') <orlogger =
logging.getLogger('log.daemonic')
.. 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(type) # type is either "deamon" or "regular"
atexit.register(lambda: remove(log_ini))
logging.config.fileConfig(log_ini)
HTH,
--
Miki <mi*********@gmail.com>
http://pythonwise.blogspot.com
Jun 27 '08 #2
On Apr 22, 12:57 pm, Miki <miki.teb...@gmail.comwrote:
Hello,
So far so good. In the relevant applications, the code looks something
like this:
logging.config.fileConfig('log.ini')
logger = logging.getLogger('log.regular') <orlogger =
logging.getLogger('log.daemonic')
.. 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(type) # type is either "deamon" or "regular"
atexit.register(lambda: remove(log_ini))
logging.config.fileConfig(log_ini)

HTH,
--
Miki <miki.teb...@gmail.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...@gmail.comwrote:
Hello,
So far so good. In the relevant applications, the code looks something
like this:
>logging.config.fileConfig('log.ini')
logger =logging.getLogger('log.regular') <orlogger =
>logging.getLogger('log.daemonic')
.. 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 theloggingconfiguration 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(type) # type is either "deamon" or "regular"
>>atexit.register(lambda: remove(log_ini))
>>>logging.config.fileConfig(log_ini)
HTH,
--
Miki <miki.teb...@gmail.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 theloggingmodule 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
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...
6
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...
0
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...
3
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...
1
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"...
0
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...
4
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...
1
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. ...
1
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...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.