Skip to content

Commit

Permalink
loguru config
Browse files Browse the repository at this point in the history
  • Loading branch information
phact committed Aug 7, 2024
1 parent 677ce36 commit f13e45e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions impl/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import inspect
import logging
import os
import sys
Expand Down Expand Up @@ -27,23 +28,25 @@
cass_logger.setLevel(logging.WARN)

class InterceptHandler(logging.Handler):
def emit(self, record):
# Get corresponding Loguru level if it exists
def emit(self, record: logging.LogRecord) -> None:
# Get corresponding Loguru level if it exists.
level: str | int
try:
level = logger.level(record.levelname).name
except ValueError:
level = record.levelno

# Find caller from where originated the logged message
frame, depth = logging.currentframe(), 2
while frame.f_code.co_filename == logging.__file__:
# Find caller from where originated the logged message.
frame, depth = inspect.currentframe(), 0
while frame and (depth == 0 or frame.f_code.co_filename == logging.__file__):
frame = frame.f_back
depth += 1

logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())

logging.basicConfig(handlers=[InterceptHandler()], level=0, force=True)

logging.root.handlers = [InterceptHandler()]
#logging.root.handlers = [InterceptHandler()]
logging.root.setLevel("DEBUG")

for name in logging.root.manager.loggerDict.keys():
Expand Down

0 comments on commit f13e45e

Please sign in to comment.