-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02cc5a4
commit d4520b7
Showing
2 changed files
with
31 additions
and
19 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
import logging | ||
import sys | ||
|
||
from loguru import _logger, logger | ||
|
||
__author__ = "lundberg" | ||
|
||
|
||
class InterceptHandler(logging.Handler): | ||
@logger.catch(default=True, onerror=lambda _: sys.exit(1)) | ||
def emit(self, record): | ||
# Get corresponding Loguru level if it exists | ||
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 | ||
skip_frames_in = [logging.__file__, __file__, _logger.__file__] | ||
while frame.f_code.co_filename in skip_frames_in: | ||
frame = frame.f_back | ||
depth += 1 | ||
|
||
logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage()) |