feat: base flask x gunicorn server
This commit is contained in:
+32
-1
@@ -2,8 +2,11 @@ import enum
|
||||
import logging
|
||||
import typing
|
||||
|
||||
import gunicorn.config
|
||||
import gunicorn.glogging
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
from .params import Parameters
|
||||
from app.params import Parameters
|
||||
|
||||
|
||||
class TermColor(enum.StrEnum):
|
||||
@@ -57,6 +60,20 @@ class ColoredLoggingFormatter(logging.Formatter):
|
||||
self.trace: bool = trace
|
||||
super().__init__()
|
||||
|
||||
@classmethod
|
||||
def flat_format(cls, level: int) -> str:
|
||||
return (
|
||||
cls.pre_format
|
||||
+ cls.FORMAT_COLORS.get(
|
||||
level,
|
||||
TermColor.MAGENTA,
|
||||
)
|
||||
+ TermColor.BOLD
|
||||
+ cls.level_format
|
||||
+ TermColor.RESET
|
||||
+ cls.post_format
|
||||
)
|
||||
|
||||
@typing.override
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
log_color: TermColor | str = self.FORMAT_COLORS.get(
|
||||
@@ -75,6 +92,20 @@ class ColoredLoggingFormatter(logging.Formatter):
|
||||
return formatter.format(record)
|
||||
|
||||
|
||||
class GunicornLogger(gunicorn.glogging.Logger):
|
||||
@typing.override
|
||||
def __init__(self, cfg: gunicorn.config.Config) -> None:
|
||||
super().__init__(cfg)
|
||||
|
||||
self._set_handler(
|
||||
self.error_log, cfg.errorlog, ColoredLoggingFormatter(trace=False)
|
||||
)
|
||||
if cfg.accesslog is not None:
|
||||
self._set_handler(
|
||||
self.access_log, cfg.accesslog, ColoredLoggingFormatter(trace=False)
|
||||
)
|
||||
|
||||
|
||||
def setup_logs(params: Parameters) -> None:
|
||||
stream_handler = logging.StreamHandler()
|
||||
stream_handler.setFormatter(ColoredLoggingFormatter(trace=params.debug))
|
||||
|
||||
Reference in New Issue
Block a user