I'm using the logging library and I want to call a function whenever any event is logged. Something like this:
import logging
#logging config here
async def on_log(message, level): #call this whenever something is logged
print(level, ":", message)
logging.debug("Hello world")
Edit: on_log
is a coroutine
Edit2: I tried implementing one of your suggestions but I'm not 100% sure about how to add it to my current logging configuration.
from logging import *
import datetime
import sys
class MyLogger(Handler):
def emit(*args):
for item in args:
print(item)
time = datetime.datetime.today()
logtime = f"{time.year}-{time.month}-{time.day}__{time.hour}h-{time.minute}m-{time.second}s"
file_handler = FileHandler(filename=f'./logs/{logtime}.log') # File output
stdout_handler = StreamHandler(sys.stdout) # Console output
basicConfig(
format=f'[%(asctime)s][%(levelname)s] %(message)s',
datefmt='%H:%M:%S',
level=DEBUG,
handlers=[file_handler, stdout_handler, MyLogger]
)
I get this error:
AttributeError: type object 'MyLogger' has no attribute 'formatter'