RotatingFileHandler and logging config file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Cranfill

    RotatingFileHandler and logging config file

    Hello,

    I've successfully coded Python to do what I want with a
    RotatingFileHan dler, 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.getLogg er('PDGUI')
    # We will rotate the files 'manually', so use zero rotate size.
    handler = logging.handler s.RotatingFileH andler( \
    "..\\PDGUI.log" , "a", 0, 10)
    handler.doRollo ver() # force the next file to be used

    formatter = logging.Formatt er("%(asctime) s " + \
    "%(levelname)s\ t%(message)s")
    handler.setForm atter(formatter )
    self._logger.ad dHandler(handle r)
    self._logger.se tLevel(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. RotatingFileHan dler
    level=NOTSET
    formatter=form0 1
    args=("..\\PDGU I.log", "a", 0, 10,)


    But I still have one problem - how do I force the "handler.doRoll over()"
    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 RotatingFileHan dler 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.doRollo ver() in code, either, if
    I've started off with a config file. Something like


    logging.config. fileConfig(opti ons.logconfig)

    # BUGBUG If the config file specifies a RotatingFileHan dler,
    # we need to force a doRollover now, but there's no way!
    #
    handler = logging.getLogg er().getHandler () # THIS DOESN'T EXIST!
    handler.doRollo ver() # 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





  • Kent Johnson

    #2
    Re: RotatingFileHan dler and logging config file

    Rob Cranfill wrote:[color=blue]
    > [BTW, has anyone else noticed that RotatingFileHan dler isn't documented
    > in the docs? All the other file handlers have at least a paragraph on
    > their options, but nothing for RFH!][/color]

    It is in the latest docs.

    Kent

    Comment

    • Rob Cranfill

      #3
      Re: RotatingFileHan dler and logging config file

      Kent Johnson wrote:
      [color=blue]
      >
      > It is in the latest docs.
      >
      > Kent[/color]

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



      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

      Comment

      • Peter Hansen

        #4
        Re: RotatingFileHan dler and logging config file

        Rob Cranfill wrote:[color=blue]
        > Kent Johnson wrote:[color=green]
        >> It is in the latest docs.[/color]
        >
        > No, it isn't. (But thanks for replying anyway!)[/color]

        Can you prove it isn't? ;-)
        [color=blue]
        > 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"?[/color]

        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

        Comment

        • news.sydney.pipenetworks.com

          #5
          Re: RotatingFileHan dler and logging config file

          Rob Cranfill wrote:[color=blue]
          > Kent Johnson wrote:
          >[color=green]
          >>
          >> It is in the latest docs.
          >>
          >> Kent[/color]
          >
          >
          > No, it isn't. (But thanks for replying anyway!)
          >
          > http://docs.python.org/lib/logging-c...ileformat.html
          >
          >[/color]

          You're looking in the wrong place. Try





          Huy

          Comment

          • Rob Cranfill

            #6
            Re: RotatingFileHan dler and logging config file

            news.sydney.pip enetworks.com wrote:
            [color=blue]
            >
            > You're looking in the wrong place. Try
            >
            > http://docs.python.org/lib/node333.html
            >[/color]

            which isn't quite the page in question, but leads to the closest
            pertinent page,

            which *still* has nothing on RotatingFileHan dler.

            That's OK, I've given up on the idea of being able to configure a
            RotatingFileHan dler 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

            Comment

            • Rob Cranfill

              #7
              Re: RotatingFileHan dler and logging config file

              Peter Hansen wrote:
              [color=blue]
              > 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....
              >[/color]

              Yes, I can puzzle out the constructor args ("constructo r", 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 RotatingFileHan dler any better than my original) but
              that's OK, see my following message to <nytimes@swiftd sl.com.au> (what a
              funny name!)

              - rob

              Comment

              • Bengt Richter

                #8
                Re: RotatingFileHan dler and logging config file

                On Wed, 16 Mar 2005 20:48:40 -0800, Rob Cranfill <robcranfill@co mcast.dot.net> wrote:
                [color=blue]
                >news.sydney.pi penetworks.com wrote:
                >[color=green]
                >>
                >> You're looking in the wrong place. Try
                >>
                >> http://docs.python.org/lib/node333.html
                >>[/color]
                >
                >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 RotatingFileHan dler.
                >
                >That's OK, I've given up on the idea of being able to configure a
                >RotatingFileHa ndler 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*.
                >[/color]
                To fish for yourself, go to google and put

                RotatingFileHan dler site:docs.pytho n.org

                in the search slot and click search.

                The first hit is


                HTH

                Regards,
                Bengt Richter

                Comment

                • news.sydney.pipenetworks.com

                  #9
                  Re: RotatingFileHan dler and logging config file

                  Rob Cranfill wrote:[color=blue]
                  > news.sydney.pip enetworks.com wrote:
                  >[color=green]
                  >>
                  >> You're looking in the wrong place. Try
                  >>
                  >> http://docs.python.org/lib/node333.html
                  >>[/color]
                  >
                  > 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 RotatingFileHan dler.[/color]

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

                  Huy

                  Comment

                  • Rob Cranfill

                    #10
                    Re: RotatingFileHan dler and logging config file

                    Bengt Richter wrote:
                    [color=blue]
                    > The first hit is
                    > http://docs.python.org/lib/node332.html
                    > HTH[/color]

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

                    - rob

                    Comment

                    • Rob Cranfill

                      #11
                      Re: RotatingFileHan dler and logging config file

                      news.sydney.pip enetworks.com wrote:[color=blue]
                      >
                      > Yeah...sorry about that. I misunderstood what node333.html was used for.
                      > Looks like your best bet may be to extend the RotatingFileHan dler
                      > directly in the handlers module and call doRollover() in __init__.[/color]

                      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

                      Comment

                      • Peter Hansen

                        #12
                        Re: RotatingFileHan dler and logging config file

                        Rob Cranfill wrote:[color=blue]
                        > 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....[/color]

                        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.handler s module, as "logging.handle rs.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 .RotatingFileHa ndler").

                        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

                        Comment

                        • Vinay Sajip

                          #13
                          Re: RotatingFileHan dler and logging config file

                          Rob Cranfill wrote:[color=blue]
                          > NID (No, It Doesn't) ;-) but thanks anyway. To reiterate, the question
                          > is how to make RotatingFileHan dler do a doRotate() on startup from a
                          > *config file*. No mention of that in what you point to.[/color]

                          I don't think that RotatingFileHan dler *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

                          Comment

                          Working...