Automatic Logging

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • daftspaniel@gmail.com

    #1

    Automatic Logging

    Sorry if this is a FAQ but Google returns a *lot* of results for Python
    Logging :-)

    I am looking for a tool that will automatically add logging to existing
    code e.g. Function Entries and Exits, Return values etc.

    Thanks,
    Davy Mitchell

    Mood News
    - BBC News Headlines Auto-Classified as Good, Bad or Neutral.


  • Peter Hansen

    #2
    Re: Automatic Logging

    daftspaniel@gma il.com wrote:[color=blue]
    > Sorry if this is a FAQ but Google returns a *lot* of results for Python
    > Logging :-)
    >
    > I am looking for a tool that will automatically add logging to existing
    > code e.g. Function Entries and Exits, Return values etc.[/color]

    Perhaps you are looking for nothing more than this standard function:
    [color=blue][color=green][color=darkred]
    >>> help(sys.settra ce)[/color][/color][/color]
    Help on built-in function settrace in module sys:

    settrace(...)
    settrace(functi on)

    Set the global debug tracing function. It will be called on each
    function call. See the debugger chapter in the library manual.


    Combine that with the standard library "logging" package and it meets
    your specs as stated above.

    -Peter

    Comment

    • Gregory Petrosyan

      #3
      Re: Automatic Logging

      My decorator bike:


      import logging
      import traceback

      logging.basicCo nfig(level=logg ing.DEBUG,
      format="%(ascti me)s %(levelname)s:\ n%(message)s\n" ,
      filename="/tmp/py.log",
      filemode='w')

      def log(f):
      def new_f(*args, **kwds):
      try:
      indent = len("(in '%s') %s(" % (f.__module__,
      f.func_name))
      nice_args = [repr(a) for a in args]
      nice_args = (',\n'+' '*indent).join( nice_args)
      nice_kwds = [str(a) + ' = ' + repr(b) for a,b in
      kwds.items()]
      if nice_kwds:
      nice_kwds[0] = ' '*indent + nice_kwds[0]
      nice_kwds = (',\n'+' '*indent).join( nice_kwds)
      nice_args = nice_args + ',\n' + nice_kwds
      else:
      nice_args = nice_args + ', {}'
      logging.info("( in '%s') %s(%s) starts" % \
      (f.__module__, f.func_name, nice_args))
      result = f(*args, **kwds)
      except:

      logging.error(' \n'.join(traceb ack.format_exc( ).split('\n')[1:])[:-1])
      raise
      return result
      new_f.func_name = f.func_name
      return new_f

      Comment

      • Gregory Petrosyan

        #4
        Re: Automatic Logging

        Sorry for broken indentation.

        Comment

        • daftspaniel@gmail.com

          #5
          Re: Automatic Logging

          Thanks very much.

          I found a good example of using sys.settrace at


          Cheers,
          Davy Mitchell

          Mood News
          - BBC News Headlines Auto-Classified as Good, Bad or Neutral.


          Comment

          Working...