-
Notifications
You must be signed in to change notification settings - Fork 5
Logs decorator
Roberto Prevato edited this page Sep 21, 2019
·
1 revision
import time
import logging
from essentials.decorators.logs import log
logger = logging.getLogger(f'fn_calls')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
@log()
def a_function_that_gets_logged_at_each_call():
# do something...
time.sleep(0.235)
a_function_that_gets_logged_at_each_call()
Logs an output like the following:
__main__.a_function_that_gets_logged_at_each_call; called; call id: 53c43283-5a94-4d53-87dc-31af49e6d634
__main__.a_function_that_gets_logged_at_each_call; completed; call id: 53c43283-5a94-4d53-87dc-31af49e6d634; elapsed 235.39783200067177 ms
Supporting also async/await functions:
import time
import asyncio
import logging
from essentials.decorators.logs import log
logger = logging.getLogger(f'fn_calls')
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
@log()
async def a_function_that_gets_logged_at_each_call():
# do something...
await asyncio.sleep(0.235)
loop = asyncio.get_event_loop()
loop.run_until_complete(a_function_that_gets_logged_at_each_call())