Warning
These documents are OUTDATED as of 2023-12-31.
Please refer to the latest version of the documentation at sanic.dev.
Utility๏
sanic.compat๏
- class sanic.compat.Header๏
Bases:
CIMultiDict
Container used for both request and response headers. It is a subclass of [CIMultiDict](https://multidict.readthedocs.io/en/stable/multidict.html#cimultidictproxy)
It allows for multiple values for a single key in keeping with the HTTP spec. Also, all keys are case in-sensitive.
Please checkout [the MultiDict documentation](https://multidict.readthedocs.io/en/stable/multidict.html#multidict) for more details about how to use the object. In general, it should work very similar to a regular dictionary.
- get_all(key)๏
Convenience method mapped to
getall()
.- Parameters:
key (str) โ
- enum sanic.compat.StrEnum(value)๏
Bases:
str
,Enum
An enumeration.
- Member Type:
str
- enum sanic.compat.UpperStrEnum(value)๏
Bases:
StrEnum
Base class for string enums that are case insensitive.
- Member Type:
str
- sanic.compat.pypy_os_module_patch()๏
The PyPy os module is missing the โreadlinkโ function, which causes issues withaiofiles. This workaround replaces the missing โreadlinkโ function with โos.path.realpathโ, which serves the same purpose.
- Return type:
None
- sanic.compat.pypy_windows_set_console_cp_patch()๏
A patch function for PyPy on Windows that sets the console code page to UTF-8 encodingto allow for proper handling of non-ASCII characters. This function uses ctypes to call the Windows API functions SetConsoleCP and SetConsoleOutputCP to set the code page.
- Return type:
None
sanic.log๏
- enum sanic.log.Colors(value)๏
Bases:
StrEnum
An enumeration.
- Member Type:
str
Valid values are as follows:
- END = <Colors.END: '\x1b[0m'>๏
- BOLD = <Colors.BOLD: '\x1b[1m'>๏
- BLUE = <Colors.BLUE: '\x1b[34m'>๏
- GREEN = <Colors.GREEN: '\x1b[32m'>๏
- PURPLE = <Colors.PURPLE: '\x1b[35m'>๏
- RED = <Colors.RED: '\x1b[31m'>๏
- SANIC = <Colors.SANIC: '\x1b[38;2;255;13;104m'>๏
- YELLOW = <Colors.YELLOW: '\x1b[01;33m'>๏
- enum sanic.log.StrEnum(value)๏
Bases:
str
,Enum
An enumeration.
- Member Type:
str
- class sanic.log.VerbosityFilter(name='')๏
Bases:
Filter
- filter(record)๏
Determine if the specified record is to be logged.
Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.
- Parameters:
record (LogRecord) โ
- Return type:
bool
- sanic.log.deprecation(message, version)๏
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)
- Parameters:
message (str) โ The message of the notice
version (float) โ The version when the feature will be removed. If it is not being removed, then set version=0.
- sanic.log.LOGGING_CONFIG_DEFAULTS: Dict[str, Any] = {'disable_existing_loggers': False, 'formatters': {'access': {'class': 'logging.Formatter', 'datefmt': '[%Y-%m-%d %H:%M:%S %z]', 'format': '%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: %(request)s %(message)s %(status)s %(byte)s'}, 'generic': {'class': 'logging.Formatter', 'datefmt': '[%Y-%m-%d %H:%M:%S %z]', 'format': '%(asctime)s [%(process)s] [%(levelname)s] %(message)s'}}, 'handlers': {'access_console': {'class': 'logging.StreamHandler', 'formatter': 'access', 'stream': <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>}, 'console': {'class': 'logging.StreamHandler', 'formatter': 'generic', 'stream': <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>}, 'error_console': {'class': 'logging.StreamHandler', 'formatter': 'generic', 'stream': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>}}, 'loggers': {'sanic.access': {'handlers': ['access_console'], 'level': 'INFO', 'propagate': True, 'qualname': 'sanic.access'}, 'sanic.error': {'handlers': ['error_console'], 'level': 'INFO', 'propagate': True, 'qualname': 'sanic.error'}, 'sanic.root': {'handlers': ['console'], 'level': 'INFO'}, 'sanic.server': {'handlers': ['console'], 'level': 'INFO', 'propagate': True, 'qualname': 'sanic.server'}}, 'version': 1}๏
Defult logging configuration
- sanic.log.access_logger = <Logger sanic.access (WARNING)>๏
Logger used by Sanic for access logging
- sanic.log.error_logger = <Logger sanic.error (WARNING)>๏
Logger used by Sanic for error logging
- sanic.log.logger = <Logger sanic.root (WARNING)>๏
General Sanic logger
- sanic.log.server_logger = <Logger sanic.server (WARNING)>๏
Logger used by Sanic for server related messages