Per-Arne Andersen
4 years ago
9 changed files with 119 additions and 40 deletions
@ -0,0 +1,21 @@ |
|||||
|
|
||||
|
def setup_logging(): |
||||
|
import logging |
||||
|
from loguru import logger |
||||
|
class InterceptHandler(logging.Handler): |
||||
|
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 |
||||
|
while 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=1) |
@ -0,0 +1,11 @@ |
|||||
|
{ |
||||
|
"logger": { |
||||
|
"path": "./logs", |
||||
|
"filename": "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> {extra[request_id]} - <cyan>{name}</cyan>:<cyan>{function}</cyan> - <level>{message}</level>" |
||||
|
|
||||
|
} |
||||
|
} |
@ -1,2 +1,11 @@ |
|||||
|
from loguru import logger |
||||
|
from fastapi import HTTPException |
||||
from jinja2 import Environment, PackageLoader |
from jinja2 import Environment, PackageLoader |
||||
jinja_env = Environment(loader=PackageLoader(__name__, 'templates')) |
jinja_env = Environment(loader=PackageLoader(__name__, 'templates')) |
||||
|
|
||||
|
|
||||
|
class WGMHTTPException(HTTPException): |
||||
|
|
||||
|
def __init__(self, status_code: int, detail: str = None): |
||||
|
HTTPException.__init__(self, status_code, detail) |
||||
|
logger.opt(depth=1).error(detail) |
Loading…
Reference in new issue