RotatingFileHandler and logging config file

Discussion in 'Python' started by Rob Cranfill, Mar 15, 2005.

  1. Rob Cranfill

    Rob Cranfill Guest

    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
    Rob Cranfill, Mar 15, 2005
    #1
    1. Advertising

  2. Rob Cranfill

    Kent Johnson Guest

    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
    Kent Johnson, Mar 15, 2005
    #2
    1. Advertising

  3. Rob Cranfill

    Rob Cranfill Guest

    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-config-fileformat.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
    Rob Cranfill, Mar 16, 2005
    #3
  4. Rob Cranfill

    Peter Hansen Guest

    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-config-fileformat.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
    Peter Hansen, Mar 16, 2005
    #4
  5. news.sydney.pipenetworks.com, Mar 16, 2005
    #5
  6. Rob Cranfill

    Rob Cranfill Guest

    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-config-fileformat.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
    Rob Cranfill, Mar 17, 2005
    #6
  7. Rob Cranfill

    Rob Cranfill Guest

    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 <> (what a
    funny name!)

    - rob
    Rob Cranfill, Mar 17, 2005
    #7
  8. On Wed, 16 Mar 2005 20:48:40 -0800, 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-config-fileformat.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
    Bengt Richter, Mar 17, 2005
    #8
  9. 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-config-fileformat.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
    news.sydney.pipenetworks.com, Mar 18, 2005
    #9
  10. Rob Cranfill

    Rob Cranfill Guest

    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
    Rob Cranfill, Mar 19, 2005
    #10
  11. Rob Cranfill

    Rob Cranfill Guest

    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
    Rob Cranfill, Mar 19, 2005
    #11
  12. Rob Cranfill

    Peter Hansen Guest

    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
    Peter Hansen, Mar 19, 2005
    #12
  13. Rob Cranfill

    Vinay Sajip Guest

    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
    Vinay Sajip, Mar 20, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kamus of Kadizhar

    RotatingFileHandler

    Kamus of Kadizhar, Dec 2, 2004, in forum: Python
    Replies:
    0
    Views:
    387
    Kamus of Kadizhar
    Dec 2, 2004
  2. flupke
    Replies:
    7
    Views:
    941
    Vinay Sajip
    Jul 2, 2006
  3. Replies:
    4
    Views:
    472
    Vinay Sajip
    May 10, 2007
  4. jordilin
    Replies:
    2
    Views:
    1,156
    Vinay Sajip
    Dec 22, 2009
  5. Herman
    Replies:
    0
    Views:
    172
    Herman
    Feb 19, 2012
Loading...

Share This Page