sanic/sanic/log.py

153 lines
3.9 KiB
Python
Raw Normal View History

2016-10-15 20:59:00 +01:00
import logging
2017-09-13 18:35:34 +01:00
import sys
2016-10-15 20:59:00 +01:00
from enum import Enum
from typing import TYPE_CHECKING, Any, Dict
from warnings import warn
from sanic.helpers import is_atty
# Python 3.11 changed the way Enum formatting works for mixed-in types.
if sys.version_info < (3, 11, 0):
class StrEnum(str, Enum):
pass
else:
if not TYPE_CHECKING:
from enum import StrEnum
2022-01-09 10:22:09 +00:00
LOGGING_CONFIG_DEFAULTS: Dict[str, Any] = dict( # no cov
2017-09-13 15:40:42 +01:00
version=1,
disable_existing_loggers=False,
loggers={
"sanic.root": {"level": "INFO", "handlers": ["console"]},
2017-09-13 15:40:42 +01:00
"sanic.error": {
"level": "INFO",
"handlers": ["error_console"],
"propagate": True,
2018-10-14 01:55:33 +01:00
"qualname": "sanic.error",
2017-09-11 07:19:09 +01:00
},
2017-09-13 15:40:42 +01:00
"sanic.access": {
"level": "INFO",
"handlers": ["access_console"],
"propagate": True,
2018-10-14 01:55:33 +01:00
"qualname": "sanic.access",
},
"sanic.server": {
"level": "INFO",
"handlers": ["console"],
"propagate": True,
"qualname": "sanic.server",
},
2017-09-13 15:40:42 +01:00
},
handlers={
"console": {
"class": "logging.StreamHandler",
"formatter": "generic",
2018-10-14 01:55:33 +01:00
"stream": sys.stdout,
2017-09-13 15:40:42 +01:00
},
"error_console": {
"class": "logging.StreamHandler",
"formatter": "generic",
2018-10-14 01:55:33 +01:00
"stream": sys.stderr,
2017-09-13 15:40:42 +01:00
},
"access_console": {
"class": "logging.StreamHandler",
"formatter": "access",
2018-10-14 01:55:33 +01:00
"stream": sys.stdout,
2017-09-13 15:40:42 +01:00
},
},
formatters={
"generic": {
2023-03-20 11:25:06 +00:00
"format": "%(asctime)s [%(process)s] [%(levelname)s] %(message)s",
2017-09-13 15:40:42 +01:00
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
2018-10-14 01:55:33 +01:00
"class": "logging.Formatter",
2017-09-13 15:40:42 +01:00
},
"access": {
2018-10-14 01:55:33 +01:00
"format": "%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: "
2023-03-20 11:25:06 +00:00
+ "%(request)s %(message)s %(status)s %(byte)s",
2017-09-13 15:40:42 +01:00
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
2018-10-14 01:55:33 +01:00
"class": "logging.Formatter",
2017-09-13 15:40:42 +01:00
},
2018-10-14 01:55:33 +01:00
},
2017-09-11 07:19:09 +01:00
)
2022-06-28 08:53:03 +01:00
"""
Defult logging configuration
"""
2017-09-11 07:19:09 +01:00
class Colors(StrEnum): # no cov
END = "\033[0m"
BOLD = "\033[1m"
BLUE = "\033[34m"
GREEN = "\033[32m"
PURPLE = "\033[35m"
RED = "\033[31m"
SANIC = "\033[38;2;255;13;104m"
YELLOW = "\033[01;33m"
class VerbosityFilter(logging.Filter):
verbosity: int = 0
def filter(self, record: logging.LogRecord) -> bool:
verbosity = getattr(record, "verbosity", 0)
return verbosity <= self.verbosity
_verbosity_filter = VerbosityFilter()
2022-01-09 10:22:09 +00:00
logger = logging.getLogger("sanic.root") # no cov
2021-01-29 14:19:10 +00:00
"""
General Sanic logger
"""
2022-06-28 08:53:03 +01:00
logger.addFilter(_verbosity_filter)
2021-01-29 14:19:10 +00:00
2022-01-09 10:22:09 +00:00
error_logger = logging.getLogger("sanic.error") # no cov
2021-01-29 14:19:10 +00:00
"""
Logger used by Sanic for error logging
"""
2022-06-28 08:53:03 +01:00
error_logger.addFilter(_verbosity_filter)
2021-01-29 14:19:10 +00:00
2022-01-09 10:22:09 +00:00
access_logger = logging.getLogger("sanic.access") # no cov
2021-01-29 14:19:10 +00:00
"""
Logger used by Sanic for access logging
"""
2022-06-28 08:53:03 +01:00
access_logger.addFilter(_verbosity_filter)
server_logger = logging.getLogger("sanic.server") # no cov
"""
Logger used by Sanic for server related messages
"""
logger.addFilter(_verbosity_filter)
2022-01-09 10:22:09 +00:00
def deprecation(message: str, version: float): # no cov
"""
Add a deprecation notice
Example when a feature is being removed. In this case, version
should be AT LEAST next version + 2
deprecation("Helpful message", 99.9)
Example when a feature is deprecated but not being removed:
deprecation("Helpful message", 0)
:param message: The message of the notice
:type message: str
:param version: The version when the feature will be removed. If it is
not being removed, then set version=0.
:type version: float
"""
version_display = f" v{version}" if version else ""
version_info = f"[DEPRECATION{version_display}] "
if is_atty():
version_info = f"{Colors.RED}{version_info}"
message = f"{Colors.YELLOW}{message}{Colors.END}"
warn(version_info + message, DeprecationWarning)