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
|
|
|
|
2021-11-07 19:39:03 +00:00
|
|
|
from enum import Enum
|
2022-11-29 10:17:48 +00:00
|
|
|
from typing import TYPE_CHECKING, Any, Dict
|
2021-12-23 22:30:27 +00:00
|
|
|
from warnings import warn
|
2017-05-09 04:41:34 +01:00
|
|
|
|
2023-07-05 10:14:47 +01:00
|
|
|
from sanic.helpers import is_atty
|
2022-06-19 12:48:06 +01:00
|
|
|
|
2021-11-07 19:39:03 +00:00
|
|
|
|
2022-11-29 10:17:48 +00:00
|
|
|
# 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={
|
2018-11-06 14:39:38 +00:00
|
|
|
"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",
|
|
|
|
},
|
2022-09-22 22:01:33 +01:00
|
|
|
"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
|
|
|
|
|
|
|
|
2022-11-29 10:17:48 +00:00
|
|
|
class Colors(StrEnum): # no cov
|
2021-11-07 19:39:03 +00:00
|
|
|
END = "\033[0m"
|
2022-09-18 15:17:23 +01:00
|
|
|
BOLD = "\033[1m"
|
|
|
|
BLUE = "\033[34m"
|
|
|
|
GREEN = "\033[32m"
|
|
|
|
PURPLE = "\033[35m"
|
|
|
|
RED = "\033[31m"
|
2022-06-16 10:35:49 +01:00
|
|
|
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
|
|
|
|
|
2021-11-07 19:39:03 +00:00
|
|
|
|
2022-06-16 10:35:49 +01:00
|
|
|
_verbosity_filter = VerbosityFilter()
|
2021-11-07 19:39:03 +00:00
|
|
|
|
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)
|
2021-12-23 22:30:27 +00:00
|
|
|
|
2022-09-22 22:01:33 +01:00
|
|
|
server_logger = logging.getLogger("sanic.server") # no cov
|
|
|
|
"""
|
|
|
|
Logger used by Sanic for server related messages
|
|
|
|
"""
|
|
|
|
logger.addFilter(_verbosity_filter)
|
|
|
|
|
2021-12-23 22:30:27 +00:00
|
|
|
|
2022-01-09 10:22:09 +00:00
|
|
|
def deprecation(message: str, version: float): # no cov
|
2023-03-21 09:25:35 +00:00
|
|
|
"""
|
|
|
|
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}] "
|
2022-06-19 12:48:06 +01:00
|
|
|
if is_atty():
|
2021-12-23 22:30:27 +00:00
|
|
|
version_info = f"{Colors.RED}{version_info}"
|
|
|
|
message = f"{Colors.YELLOW}{message}{Colors.END}"
|
|
|
|
warn(version_info + message, DeprecationWarning)
|