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

RotatingFileHandler and logging config file

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 time I run my app, with up to 10
files kept from the last invocations. This was accomplished with

self._logger = logging.getLogger('PDGUI')
# We will rotate the files 'manually', so use zero rotate size.
handler = logging.handlers.RotatingFileHandler( \
"..\\PDGUI.log", "a", 0, 10)
handler.doRollover() # force the next file to be used

formatter = logging.Formatter("%(asctime)s " + \
"%(levelname)s\t%(message)s")
handler.setFormatter(formatter)
self._logger.addHandler(handler)
self._logger.setLevel(logging.INFO)

I'd like to do the same thing with a config file (so that, if this
*isn't* the behavior I want, I can change the config file, of course).
I started with a rather plain config file, with the only interesting bit
being:

[handler_hand01]
class=handlers.RotatingFileHandler
level=NOTSET
formatter=form01
args=("..\\PDGUI.log", "a", 0, 10,)
But I still have one problem - how do I force the "handler.doRollover()"
to happen with the config file? There doesn't seem to be any way to do
this in the config file itself, which is OK and perhaps appropriate
[BTW, has anyone else noticed that RotatingFileHandler isn't documented
in the docs? All the other file handlers have at least a paragraph on
their options, but nothing for RFH!]

I can't find any way to do the handler.doRollover() in code, either, if
I've started off with a config file. Something like
logging.config.fileConfig(options.logconfig)

# BUGBUG If the config file specifies a RotatingFileHandler,
# we need to force a doRollover now, but there's no way!
#
handler = logging.getLogger().getHandler() # THIS DOESN'T EXIST!
handler.doRollover() # force the next file to be used
Ideas, suggestions, etc? It's too bad - with the code method, I can do
exactly what I want, but not with the config file.

- rob

Jul 18 '05 #1
12 12650
Rob Cranfill wrote:
[BTW, has anyone else noticed that RotatingFileHandler isn't documented
in the docs? All the other file handlers have at least a paragraph on
their options, but nothing for RFH!]


It is in the latest docs.

Kent
Jul 18 '05 #2
Kent Johnson wrote:

It is in the latest docs.

Kent


No, it isn't. (But thanks for replying anyway!)

http://docs.python.org/lib/logging-c...ileformat.html

has all the others (OK, maybe not all, I haven't thoroughly checked, but
it's got nine of 'em) but nothing for RFH.

Or is that not "the latest docs"?

- rob
Jul 18 '05 #3
Rob Cranfill wrote:
Kent Johnson wrote:
It is in the latest docs.
No, it isn't. (But thanks for replying anyway!)


Can you prove it isn't? ;-)
http://docs.python.org/lib/logging-c...ileformat.html

has all the others (OK, maybe not all, I haven't thoroughly checked, but
it's got nine of 'em) but nothing for RFH.

Or is that not "the latest docs"?


Sure, but it's not the only place to look in the current docs.
Try here instead: http://docs.python.org/lib/node332.html

The missing piece of the puzzle might be the connection
between the 'args' in the config file and the arguments
passed to the __init__ method of the class....

-Peter
Jul 18 '05 #4
Rob Cranfill wrote:
Kent Johnson wrote:

It is in the latest docs.

Kent

No, it isn't. (But thanks for replying anyway!)

http://docs.python.org/lib/logging-c...ileformat.html


You're looking in the wrong place. Try

http://docs.python.org/lib/node333.html

Huy
Jul 18 '05 #5
news.sydney.pipenetworks.com wrote:

You're looking in the wrong place. Try

http://docs.python.org/lib/node333.html


which isn't quite the page in question, but leads to the closest
pertinent page,
http://docs.python.org/lib/logging-c...ileformat.html
which *still* has nothing on RotatingFileHandler.

That's OK, I've given up on the idea of being able to configure a
RotatingFileHandler from a config file; I'll do it in my code as a
default, and if users want any *other* behavior, they can config *that*.

- rob

Jul 18 '05 #6
Peter Hansen wrote:
The missing piece of the puzzle might be the connection
between the 'args' in the config file and the arguments
passed to the __init__ method of the class....


Yes, I can puzzle out the constructor args ("constructor", heh heh, must
be a Java Man) but it's how to get it to do a doRollover() that's in
question. No sign of support for it in the docs (which your link to
doesn't elucidate RotatingFileHandler any better than my original) but
that's OK, see my following message to <ny*****@swiftdsl.com.au> (what a
funny name!)

- rob
Jul 18 '05 #7
On Wed, 16 Mar 2005 20:48:40 -0800, Rob Cranfill <ro*********@comcast.dot.net> wrote:
news.sydney.pipenetworks.com wrote:

You're looking in the wrong place. Try

http://docs.python.org/lib/node333.html


which isn't quite the page in question, but leads to the closest
pertinent page,
http://docs.python.org/lib/logging-c...ileformat.html
which *still* has nothing on RotatingFileHandler.

That's OK, I've given up on the idea of being able to configure a
RotatingFileHandler from a config file; I'll do it in my code as a
default, and if users want any *other* behavior, they can config *that*.

To fish for yourself, go to google and put

RotatingFileHandler site:docs.python.org

in the search slot and click search.

The first hit is
http://docs.python.org/lib/node332.html

HTH

Regards,
Bengt Richter
Jul 18 '05 #8
Rob Cranfill wrote:
news.sydney.pipenetworks.com wrote:

You're looking in the wrong place. Try

http://docs.python.org/lib/node333.html


which isn't quite the page in question, but leads to the closest
pertinent page,
http://docs.python.org/lib/logging-c...ileformat.html
which *still* has nothing on RotatingFileHandler.


Yeah...sorry about that. I misunderstood what node333.html was used for.
Looks like your best bet may be to extend the RotatingFileHandler
directly in the handlers module and call doRollover() in __init__.

Huy
Jul 18 '05 #9
Bengt Richter wrote:
The first hit is
http://docs.python.org/lib/node332.html
HTH


NID (No, It Doesn't) ;-) but thanks anyway. To reiterate, the question
is how to make RotatingFileHandler do a doRotate() on startup from a
*config file*. No mention of that in what you point to.

- rob
Jul 18 '05 #10
news.sydney.pipenetworks.com wrote:

Yeah...sorry about that. I misunderstood what node333.html was used for.
Looks like your best bet may be to extend the RotatingFileHandler
directly in the handlers module and call doRollover() in __init__.


Ah, now *there's* an intriguing approach! I am too much a Python noob to
know - can I subclass RFH and then invoke that new class from a config
file (the crux of my original question) ? I may play around with it
today....

Thanks,
- rob
Jul 18 '05 #11
Rob Cranfill wrote:
Ah, now *there's* an intriguing approach! I am too much a Python noob to
know - can I subclass RFH and then invoke that new class from a config
file (the crux of my original question) ? I may play around with it
today....


There's at least one way that will work, though it may not be
the cleanest approach.

Wherever you create your subclass, stick a reference to it in
the logging.handlers module, as "logging.handlers.MyClass = MyClass".
Then the config file can load it just as you now load the
existing class (which I assume looks like the usual examples,
with "class=handlers.RotatingFileHandler").

The logging.cfg file is executed in the context of the logging
module's namespace, so you could also stick your names directly
in there and skip the "handlers." part, but that might be
even less clear...

-Peter
Jul 18 '05 #12
Rob Cranfill wrote:
NID (No, It Doesn't) ;-) but thanks anyway. To reiterate, the question
is how to make RotatingFileHandler do a doRotate() on startup from a
*config file*. No mention of that in what you point to.


I don't think that RotatingFileHandler *should* be configurable to do a
doRollover() on startup. I would follow up the suggestion of using your
own derived class. The config mechanism will allow you to instantiate
custom handlers - you only have to take into account (as an earlier
poster has indicated) how the evaluation of the handler class (and its
constructor arguments) is performed.

BTW - constructor is not just for Java, but C++ too (not to mention C#).
Vinay Sajip

Jul 18 '05 #13

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

Similar topics

5
by: who be dat? | last post by:
Hello all. I'm writing an application that is writing trace information that can be viewed in trace.axd. I would like to rename this and use a different name specific to my application. I know...
1
by: Almad | last post by:
Hi, our applications can have plugins as subpackages and I'd like to allow them to use their own logger as well as it's configuration. I thought that best way will be their own configuration...
1
by: Kenneth Love | last post by:
I have a Python logging config file that contains a RotatingFileHandler handler. In the args key, I have hard-coded the log filename. Everything works great. However, I find that I now need to...
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: Matthew Wilson | last post by:
The python logging module is a beautiful masterpiece. I'm studying filters and the config-file approach. Is it possible to define a filter somehow and then refer to it in my config file? TIA ...
3
by: guybenron | last post by:
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...
3
by: Lowell Alleman | last post by:
Here is the situation: I wrote my own log handler class (derived from logging.Handler) and I want to be able to use it from a logging config file, that is, a config file loaded with the...
4
by: Matthew Wilson | last post by:
I'm working on a package that uses the standard library logging module along with a .cfg file. In my code, I use logging.config.fileConfig('/home/matt/mypackage/matt.cfg') to load in the...
1
by: GR M | last post by:
Hi.. I want to write the connection string of my application in app.config file. I dont know how to write it or how to dynamically change it from my application source code either. My App.config...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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.