logging exceptions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexandru  Mosoi

    logging exceptions

    why doesn't logging throw any exception when it should? how do I
    configure logging to throw exceptions?
    >>try:
    .... logging.fatal(' asdf %d', '123')
    .... except:
    .... print 'this line is never printed'
    ....
    Traceback (most recent call last):
    File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
    python2.5/logging/__init__.py", line 744, in emit
    msg = self.format(rec ord)
    File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
    python2.5/logging/__init__.py", line 630, in format
    return fmt.format(reco rd)
    File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
    python2.5/logging/__init__.py", line 418, in format
    record.message = record.getMessa ge()
    File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
    python2.5/logging/__init__.py", line 288, in getMessage
    msg = msg % self.args
    TypeError: int argument required
  • Rob Wolfe

    #2
    Re: logging exceptions



    Alexandru Mosoi napisa³(a):
    why doesn't logging throw any exception when it should? how do I
    configure logging to throw exceptions?
    >
    >try:
    ... logging.fatal(' asdf %d', '123')
    ... except:
    ... print 'this line is never printed'
    ...
    [...]

    You need to subclass your handler and redefine `handleError` method.
    See: http://docs.python.org/lib/node409.html

    HTH,
    Rob

    Comment

    • Vinay Sajip

      #3
      Re: logging exceptions

      On Aug 26, 10:36 am, Alexandru Mosoi <brtz...@gmail. comwrote:
      why doesn'tloggingt hrow any exception when it should? how do I
      configureloggin gto throw exceptions?
      >
      >try:
      >
      ... logging.fatal(' asdf %d', '123')
      ... except:
      ... print 'this line is never printed'
      ...
      Traceback (most recent call last):
      File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
      python2.5/logging/__init__.py", line 744, in emit
      msg = self.format(rec ord)
      File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
      python2.5/logging/__init__.py", line 630, in format
      return fmt.format(reco rd)
      File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
      python2.5/logging/__init__.py", line 418, in format
      record.message = record.getMessa ge()
      File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
      python2.5/logging/__init__.py", line 288, in getMessage
      msg = msg % self.args
      TypeError: int argument required
      Was your traceback from the snippet you posted? If it was, then the
      exception (a TypeError) *is* being raised from logging. So I don't
      understand your question "why doesn't logging throw any exception when
      it should?", because logging is raising an exception here.

      To cause logging to *not* raise exceptions, set
      logging.raiseEx ceptions to 0 (default is 1). The raiseExceptions
      variable would normally be set to 0 in a production environment, where
      you don't want logging-related exceptions to bring an application
      down.

      Regards,

      Vinay Sajip

      Comment

      • Rob Wolfe

        #4
        Re: logging exceptions



        Vinay Sajip napisa³(a):
        On Aug 26, 10:36 am, Alexandru Mosoi <brtz...@gmail. comwrote:
        why doesn'tloggingt hrow any exception when it should? how do I
        configureloggin gto throw exceptions?
        >>try:
        ... logging.fatal(' asdf %d', '123')
        ... except:
        ... print 'this line is never printed'
        ...
        Traceback (most recent call last):
        File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
        python2.5/logging/__init__.py", line 744, in emit
        msg = self.format(rec ord)
        File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
        python2.5/logging/__init__.py", line 630, in format
        return fmt.format(reco rd)
        File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
        python2.5/logging/__init__.py", line 418, in format
        record.message = record.getMessa ge()
        File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
        python2.5/logging/__init__.py", line 288, in getMessage
        msg = msg % self.args
        TypeError: int argument required
        >
        Was your traceback from the snippet you posted? If it was, then the
        exception (a TypeError) *is* being raised from logging. So I don't
        understand your question "why doesn't logging throw any exception when
        it should?", because logging is raising an exception here.
        No, it isn't.
        This traceback is *printed* in `Handler.handle Error` method:

        <code from logging>
        __version__ = "0.5.0.2"

        [...]

        def handleError(sel f, record):
        """
        Handle errors which occur during an emit() call.

        This method should be called from handlers when an exception
        is
        encountered during an emit() call. If raiseExceptions is
        false,
        exceptions get silently ignored. This is what is mostly wanted
        for a logging system - most users will not care about errors
        in
        the logging system, they are more interested in application
        errors.
        You could, however, replace this with a custom handler if you
        wish.
        The record which was being processed is passed in to this
        method.
        """
        if raiseExceptions :
        ei = sys.exc_info()
        traceback.print _exception(ei[0], ei[1], ei[2], None,
        sys.stderr)
        del ei

        </code from logging>
        >
        To cause logging to *not* raise exceptions, set
        logging.raiseEx ceptions to 0 (default is 1). The raiseExceptions
        variable would normally be set to 0 in a production environment, where
        you don't want logging-related exceptions to bring an application
        down.
        Well, I think that it will not help. The exception will be printed
        not raised. The only solution is to redefine `handleError` method
        or maybe I've missed something?

        Br,
        Rob

        Comment

        • Vinay Sajip

          #5
          Re: logging exceptions

          On Aug 27, 10:30 am, Rob Wolfe <r...@smsnet.pl wrote:
          Vinay Sajip napisa³(a):
          >
          >
          >
          On Aug 26, 10:36 am, Alexandru Mosoi <brtz...@gmail. comwrote:
          why doesn'tloggingt hrow any exception when it should? how do I
          configureloggin gto throw exceptions?
          >
          >try:
          >
          ... logging.fatal(' asdf %d', '123')
          ... except:
          ... print 'this line is never printed'
          ...
          Traceback (most recent call last):
          File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
          python2.5/logging/__init__.py", line 744, in emit
          msg = self.format(rec ord)
          File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
          python2.5/logging/__init__.py", line 630, in format
          return fmt.format(reco rd)
          File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
          python2.5/logging/__init__.py", line 418, in format
          record.message = record.getMessa ge()
          File "/Library/Frameworks/Python.framewor k/Versions/2.5/lib/
          python2.5/logging/__init__.py", line 288, in getMessage
          msg = msg % self.args
          TypeError: int argument required
          >
          Was your traceback from the snippet you posted? If it was, then the
          exception (a TypeError) *is* being raised fromlogging. So I don't
          understand your question "why doesn'tloggingt hrow any exception when
          it should?", becauseloggingi s raising an exception here.
          >
          No, it isn't.
          This traceback is *printed* in `Handler.handle Error` method:
          >
          You're right - silly me. I wasn't paying attention :-(
          variable would normally be set to 0 in a production environment, where
          you don't wantlogging-related exceptions to bring an application
          down.
          >
          Well, I think that it will not help. The exception will be printed
          not raised. The only solution is to redefine `handleError` method
          or maybe I've missed something?
          >

          No, you're right. To do custom handling of exceptions, handleError
          needs to be overridden.

          Regards,

          Vinay

          Comment

          Working...