Created
July 8, 2023 12:36
-
-
Save quadrismegistus/5c6b92838525c9891753cf6437e8ca94 to your computer and use it in GitHub Desktop.
Quick "logwatch" (logger + stopwatch)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !pip install humanfriendly loguru | |
from humanfriendly import format_timespan | |
from loguru import logger | |
import time | |
class Logwatch: | |
def __init__(self, name='Task', level='DEBUG'): | |
self.started = None | |
self.ended = None | |
self.level=level | |
self.log = getattr(logger,self.level.lower()) | |
self.task_name = name | |
@property | |
def tdesc(self): return format_timespan(self.duration) | |
@property | |
def duration(self): return self.ended - self.started | |
@property | |
def desc(self): | |
if self.started is not None and self.ended is not None: | |
return f'{self.task_name} completed in {self.tdesc}' | |
else: | |
return f'{self.task_name} running ...' | |
def __enter__(self): | |
self.log(self.desc) | |
self.started = time.time() | |
def __exit__(self,*x): | |
self.ended = time.time() | |
self.log(self.desc) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment