~nataliabidart/ubuntuone-client/careful-logging

« back to all changes in this revision

Viewing changes to ubuntuone/logger.py

  • Committer: Natalia B. Bidart
  • Date: 2011-08-30 15:47:27 UTC
  • Revision ID: natalia.bidart@canonical.com-20110830154727-7txcjhjbq87oca0v
Do not log sensible data in CredentialsMangementTool (LP: #837488).

Show diffs side-by-side

added added

removed removed

Lines of Context:
240
240
            return True
241
241
 
242
242
 
243
 
def log_call(log_func):
 
243
def log_call(log_func, with_args=True, with_result=True):
244
244
    """Decorator to add a log entry using 'log_func'.
245
245
 
 
246
    If not 'with_args', do not log arguments. Same apply to 'with_result'.
 
247
 
246
248
    An example of use would be:
247
249
 
248
250
    @log_call(logger.debug)
257
259
        @functools.wraps(f)
258
260
        def inner(*args, **kwargs):
259
261
            """Call f(*args, **kwargs)."""
260
 
            log_func('%s: args %r, kwargs %r.', f.__name__, args, kwargs)
 
262
            if with_args:
 
263
                a, kw = args, kwargs
 
264
            else:
 
265
                a, kw = '<hidden args>', '<hidden kwargs>'
 
266
            log_func('%s: args %r, kwargs %r.', f.__name__, a, kw)
 
267
 
261
268
            res = f(*args, **kwargs)
262
 
            log_func('%s: result %r.', f.__name__, res)
 
269
 
 
270
            if with_result:
 
271
                log_func('%s: result %r.', f.__name__, res)
 
272
            else:
 
273
                log_func('%s: result %r.', f.__name__, '<hidden result>')
 
274
 
263
275
            return res
264
276
 
265
277
        return inner