loguru
loguru copied to clipboard
Not obtaining correct function call in the logs due to @logger.catch(reraise=True)
Hello, first of all, thanks for the great package. I've been using @logger.catch(reraise=True)
wherever I've put a try/exception block. This is my logging_config.json
:
{
"logger": {
"filename": "logs/access.log",
"level": "info",
"rotation": "20 days",
"retention": "1 months",
"format": "<level>{level: <8}</level> <green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> request id: {extra[request_id]} - <cyan>{name}</cyan>:<cyan>{function}</cyan> - <level>{message}</level>"
}
}
and following is my custom_logger.py
file:
import json
import logging
import sys
from pathlib import Path
from loguru import logger
LOGS_DIR = Path("logs")
# Create Directories
LOGS_DIR.mkdir(parents=True, exist_ok=True)
class InterceptHandler(logging.Handler):
loglevel_mapping = {
50: "CRITICAL",
40: "ERROR",
30: "WARNING",
20: "INFO",
10: "DEBUG",
0: "NOTSET",
}
def emit(self, record):
try:
level = logger.level(record.levelname).name
except AttributeError:
level = self.loglevel_mapping[record.levelno]
frame, depth = logging.currentframe(), 2
while frame.f_code.co_filename == logging.__file__:
frame = frame.f_back
depth += 1
log = logger.bind(request_id="app")
log.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())
class CustomizeLogger:
@classmethod
def make_logger(cls, config_path: Path):
config = cls.load_logging_config(config_path)
logging_config = config.get("logger")
logger = cls.customize_logging(
logging_config.get("filename"),
level=logging_config.get("level"),
retention=logging_config.get("retention"),
rotation=logging_config.get("rotation"),
format=logging_config.get("format"),
)
return logger
@classmethod
def customize_logging(cls, filepath: Path, level: str, rotation: str, retention: str, format: str):
logger.remove()
logger.add(sys.stdout, enqueue=True, backtrace=True, level=level.upper(), format=format)
logger.add(
str(filepath),
rotation=rotation,
retention=retention,
enqueue=True,
backtrace=True,
level=level.upper(),
format=format,
)
logging.basicConfig(handlers=[InterceptHandler()], level=0)
logging.getLogger("uvicorn.access").handlers = [InterceptHandler()]
for _log in ["uvicorn", "uvicorn.error", "fastapi"]:
_logger = logging.getLogger(_log)
_logger.handlers = [InterceptHandler()]
for _log in ["posthog", "posthog.error", "backoff"]:
_logger = logging.getLogger(_log)
_logger.handlers = [InterceptHandler()]
_logger.setLevel = 50
return logger.bind(request_id=None, method=None)
@classmethod
def load_logging_config(cls, config_path):
config = None
with open(config_path) as config_file:
config = json.load(config_file)
return config
The logs that are generated are shown below:
INFO 2022-09-07 11:39:41.042 request id: None - app.api:index_chunked_files - ✅ Received valid input: toxic_utils.py : text/x-python
WARNING 2022-09-07 11:39:41.049 request id: None - loguru._logger:catch_wrapper - text/x-python is not supported
ERROR 2022-09-07 11:39:41.052 request id: None - loguru._logger:catch_wrapper - An error has been caught in function 'catch_wrapper', process 'SpawnProcess-1' (7), thread 'MainThread' (140477093877568):
Instead of loguru._logger:catch_wrapper
, how can I obtain the exact function where this exception was triggered similar to the first line showing app.api:index_chunked_files
?
Hi @LaxmanSinghTomar. Sorry but I'm unable to reproduce your problem. Are you able to provide a minimal reproducible example please, that would help me investigate the issue.