def log_duration(operation, level='info', log=None):
"""Log the duration of some process.
Parameters
----------
operation : str
What is being timed?
level : str, optional
The level to log the start and end messages at.
log : Logger, optional
The logger object to write to. By default this is the logger for the
calling frame.
"""
log = _get_logger_for_contextmanager(log)
log.log(level.upper(), operation)
start = datetime.now()
try:
yield
finally:
now = datetime.now()
log.log(
level.upper(),
'completed {} (completed in {})',
operation,
naturaldelta(now - start),
)
评论列表
文章目录