Automatic Logging

Discussion in 'Python' started by daftspaniel@gmail.com, Jan 26, 2006.

  1. Guest

    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.
    http://www.latedecember.com/sites/moodnews/
     
    , Jan 26, 2006
    #1
    1. Advertising

  2. Peter Hansen Guest

    wrote:
    > 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.


    Perhaps you are looking for nothing more than this standard function:

    >>> help(sys.settrace)

    Help on built-in function settrace in module sys:

    settrace(...)
    settrace(function)

    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
     
    Peter Hansen, Jan 26, 2006
    #2
    1. Advertising

  3. My decorator bike:


    import logging
    import traceback

    logging.basicConfig(level=logging.DEBUG,
    format="%(asctime)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(traceback.format_exc().split('\n')[1:])[:-1])
    raise
    return result
    new_f.func_name = f.func_name
    return new_f
     
    Gregory Petrosyan, Jan 27, 2006
    #3
  4. Sorry for broken indentation.
     
    Gregory Petrosyan, Jan 27, 2006
    #4
  5. Guest

    , Jan 27, 2006
    #5
    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. Guest
    Replies:
    1
    Views:
    774
    Guest
    Jun 29, 2004
  2. Stefan Siegl
    Replies:
    0
    Views:
    963
    Stefan Siegl
    Aug 27, 2003
  3. Chishun Kwong
    Replies:
    2
    Views:
    426
    Eduardo Francos
    Sep 17, 2003
  4. janne
    Replies:
    0
    Views:
    9,509
    janne
    Sep 10, 2004
  5. Christoph Haas
    Replies:
    0
    Views:
    457
    Christoph Haas
    Jun 12, 2006
Loading...

Share This Page