Table of Contents
Colorssanic.logging.colordeprecationsanic.logging.deprecationVerbosityFiltersanic.logging.filterAutoAccessFormattersanic.logging.formatterAutoFormattersanic.logging.formatterDebugAccessFormattersanic.logging.formatterDebugFormattersanic.logging.formatterJSONAccessFormattersanic.logging.formatterJSONFormattersanic.logging.formatterLegacyAccessFormattersanic.logging.formatterLegacyFormattersanic.logging.formatterProdAccessFormattersanic.logging.formatterProdFormattersanic.logging.formattersetup_loggingsanic.logging.setupsanic.logging.color.Colors#
Colors for log messages. If the output is not a TTY, the colors will be
class Colors(value, names = None, module = None, qualname = None, type = None, start = 1, boundary = None)
disabled.
Can be used like this:
from sanic.log import logger, Colors
logger.info(f"{Colors.GREEN}This is a green message{Colors.END}")
Parameters
- END
Reset the color
- BOLD
Bold text
- BLUE
Blue text
- GREEN
Green text
- PURPLE
Purple text
- RED
Red text
- SANIC
Sanic pink
- YELLOW
Yellow text
- GREY
Grey text
sanic.logging.deprecation.deprecation#
Add a deprecation notice
def deprecation(message: <class 'str'>, version: <class 'float'>)
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 Deprecation message
- version
float Version when the feature will be removed
sanic.logging.filter.VerbosityFilter#
Filter log records based on verbosity level.
class VerbosityFilter(name = )
filter#
Determine if the specified record is to be logged.
def filter(self, record: <class 'logging.LogRecord'>): -> <class 'bool'>
Returns True if the record should be logged, or False otherwise. If deemed appropriate, the record may be modified in-place.
sanic.logging.formatter.AutoAccessFormatter#
Automatically sets up the formatter based on the environment.
class AutoAccessFormatter(args): -> None
It will switch between the Debug and Production formatters based upon how the environment is set up. Additionally, it will automatically detect if the output is a TTY and colorize the output accordingly.
format#
Format the specified record as text.
def format(self, record: logging.LogRecord): -> str
The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
formatException#
Format and return the specified exception information as a string.
def formatException(self, ei)
This default implementation just uses traceback.print_exception()
formatMessage#
def formatMessage(self, record)
formatStack#
This method is provided as an extension point for specialized
def formatStack(self, stack_info)
formatting of stack information.
The input data is a string as returned from a call to
formatTime#
Return the creation time of the specified LogRecord as formatted text.
def formatTime(self, record, datefmt = None)
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the 'converter' attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the 'converter' attribute in the Formatter class.
sanic.logging.formatter.AutoFormatter#
Automatically sets up the formatter based on the environment.
class AutoFormatter(args): -> None
It will switch between the Debug and Production formatters based upon how the environment is set up. Additionally, it will automatically detect if the output is a TTY and colorize the output accordingly.
format#
Format the specified record as text.
def format(self, record: logging.LogRecord): -> str
The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
formatException#
Format and return the specified exception information as a string.
def formatException(self, ei)
This default implementation just uses traceback.print_exception()
formatMessage#
def formatMessage(self, record)
formatStack#
This method is provided as an extension point for specialized
def formatStack(self, stack_info)
formatting of stack information.
The input data is a string as returned from a call to
formatTime#
Return the creation time of the specified LogRecord as formatted text.
def formatTime(self, record, datefmt = None)
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the 'converter' attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the 'converter' attribute in the Formatter class.
sanic.logging.formatter.DebugAccessFormatter#
Automatically sets up the formatter based on the environment.
class DebugAccessFormatter(args): -> None
It will switch between the Debug and Production formatters based upon how the environment is set up. Additionally, it will automatically detect if the output is a TTY and colorize the output accordingly.
format#
Format the specified record as text.
def format(self, record: logging.LogRecord): -> str
The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
formatException#
Format and return the specified exception information as a string.
def formatException(self, ei)
This default implementation just uses traceback.print_exception()
formatMessage#
def formatMessage(self, record)
formatStack#
This method is provided as an extension point for specialized
def formatStack(self, stack_info)
formatting of stack information.
The input data is a string as returned from a call to
formatTime#
Return the creation time of the specified LogRecord as formatted text.
def formatTime(self, record, datefmt = None)
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the 'converter' attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the 'converter' attribute in the Formatter class.
sanic.logging.formatter.DebugFormatter#
The DebugFormatter is used for development and debugging purposes.
class DebugFormatter(args): -> None
It can be used directly, or it will be automatically selected if the environment is set up for development and is using the AutoFormatter.
format#
Format the specified record as text.
def format(self, record: logging.LogRecord): -> str
The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
formatException#
Format and return the specified exception information as a string.
def formatException(self, ei)
This default implementation just uses traceback.print_exception()
formatMessage#
def formatMessage(self, record)
formatStack#
This method is provided as an extension point for specialized
def formatStack(self, stack_info)
formatting of stack information.
The input data is a string as returned from a call to
formatTime#
Return the creation time of the specified LogRecord as formatted text.
def formatTime(self, record, datefmt = None)
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the 'converter' attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the 'converter' attribute in the Formatter class.
sanic.logging.formatter.JSONAccessFormatter#
The JSONAccessFormatter is used to output access logs in JSON format.
class JSONAccessFormatter(args): -> None
This is useful for logging to a file or to a log aggregator that understands JSON. It will output all the fields from the LogRecord as well as the extra fields that are passed in.
You can use it as follows:
from sanic.log import LOGGING_CONFIG_DEFAULTS
LOGGING_CONFIG_DEFAULTS["formatters"] = {
"generic": {
"class": "sanic.logging.formatter.JSONFormatter"
},
"access": {
"class": "sanic.logging.formatter.JSONAccessFormatter"
},
}
format#
Format the specified record as text.
def format(self, record: logging.LogRecord): -> str
The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
formatException#
Format and return the specified exception information as a string.
def formatException(self, ei)
This default implementation just uses traceback.print_exception()
formatMessage#
def formatMessage(self, record)
formatStack#
This method is provided as an extension point for specialized
def formatStack(self, stack_info)
formatting of stack information.
The input data is a string as returned from a call to
formatTime#
Return the creation time of the specified LogRecord as formatted text.
def formatTime(self, record, datefmt = None)
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the 'converter' attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the 'converter' attribute in the Formatter class.
format_dict#
def format_dict(self, record: dict): -> str
to_dict#
def to_dict(self, record: logging.LogRecord): -> dict
sanic.logging.formatter.JSONFormatter#
The JSONFormatter is used to output logs in JSON format.
class JSONFormatter(args): -> None
This is useful for logging to a file or to a log aggregator that understands JSON. It will output all the fields from the LogRecord as well as the extra fields that are passed in.
You can use it as follows:
from sanic.log import LOGGING_CONFIG_DEFAULTS
LOGGING_CONFIG_DEFAULTS["formatters"] = {
"generic": {
"class": "sanic.logging.formatter.JSONFormatter"
},
"access": {
"class": "sanic.logging.formatter.JSONFormatter"
},
}
format#
Format the specified record as text.
def format(self, record: logging.LogRecord): -> str
The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
formatException#
Format and return the specified exception information as a string.
def formatException(self, ei)
This default implementation just uses traceback.print_exception()
formatMessage#
def formatMessage(self, record)
formatStack#
This method is provided as an extension point for specialized
def formatStack(self, stack_info)
formatting of stack information.
The input data is a string as returned from a call to
formatTime#
Return the creation time of the specified LogRecord as formatted text.
def formatTime(self, record, datefmt = None)
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the 'converter' attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the 'converter' attribute in the Formatter class.
format_dict#
def format_dict(self, record: dict): -> str
to_dict#
def to_dict(self, record: logging.LogRecord): -> dict
sanic.logging.formatter.LegacyAccessFormatter#
The LegacyFormatter is used if you want to use the old style of logging.
class LegacyAccessFormatter(args): -> None
You can use it as follows, typically in conjunction with the LegacyFormatter:
from sanic.log import LOGGING_CONFIG_DEFAULTS
LOGGING_CONFIG_DEFAULTS["formatters"] = {
"generic": {
"class": "sanic.logging.formatter.LegacyFormatter"
},
"access": {
"class": "sanic.logging.formatter.LegacyAccessFormatter"
},
}
format#
Format the specified record as text.
def format(self, record: logging.LogRecord): -> str
The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
formatException#
Format and return the specified exception information as a string.
def formatException(self, ei)
This default implementation just uses traceback.print_exception()
formatMessage#
def formatMessage(self, record)
formatStack#
This method is provided as an extension point for specialized
def formatStack(self, stack_info)
formatting of stack information.
The input data is a string as returned from a call to
formatTime#
Return the creation time of the specified LogRecord as formatted text.
def formatTime(self, record, datefmt = None)
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the 'converter' attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the 'converter' attribute in the Formatter class.
sanic.logging.formatter.LegacyFormatter#
The LegacyFormatter is used if you want to use the old style of logging.
class LegacyFormatter(args): -> None
You can use it as follows, typically in conjunction with the LegacyAccessFormatter:
from sanic.log import LOGGING_CONFIG_DEFAULTS
LOGGING_CONFIG_DEFAULTS["formatters"] = {
"generic": {
"class": "sanic.logging.formatter.LegacyFormatter"
},
"access": {
"class": "sanic.logging.formatter.LegacyAccessFormatter"
},
}
format#
Format the specified record as text.
def format(self, record: logging.LogRecord): -> str
The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
formatException#
Format and return the specified exception information as a string.
def formatException(self, ei)
This default implementation just uses traceback.print_exception()
formatMessage#
def formatMessage(self, record)
formatStack#
This method is provided as an extension point for specialized
def formatStack(self, stack_info)
formatting of stack information.
The input data is a string as returned from a call to
formatTime#
Return the creation time of the specified LogRecord as formatted text.
def formatTime(self, record, datefmt = None)
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the 'converter' attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the 'converter' attribute in the Formatter class.
sanic.logging.formatter.ProdAccessFormatter#
Automatically sets up the formatter based on the environment.
class ProdAccessFormatter(args): -> None
It will switch between the Debug and Production formatters based upon how the environment is set up. Additionally, it will automatically detect if the output is a TTY and colorize the output accordingly.
format#
Format the specified record as text.
def format(self, record: logging.LogRecord): -> str
The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
formatException#
Format and return the specified exception information as a string.
def formatException(self, ei)
This default implementation just uses traceback.print_exception()
formatMessage#
def formatMessage(self, record)
formatStack#
This method is provided as an extension point for specialized
def formatStack(self, stack_info)
formatting of stack information.
The input data is a string as returned from a call to
formatTime#
Return the creation time of the specified LogRecord as formatted text.
def formatTime(self, record, datefmt = None)
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the 'converter' attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the 'converter' attribute in the Formatter class.
sanic.logging.formatter.ProdFormatter#
The ProdFormatter is used for production environments.
class ProdFormatter(args): -> None
It can be used directly, or it will be automatically selected if the environment is set up for production and is using the AutoFormatter.
format#
Format the specified record as text.
def format(self, record: logging.LogRecord): -> str
The record's attribute dictionary is used as the operand to a string formatting operation which yields the returned string. Before formatting the dictionary, a couple of preparatory steps are carried out. The message attribute of the record is computed using LogRecord.getMessage(). If the formatting string uses the time (as determined by a call to usesTime(), formatTime() is called to format the event time. If there is exception information, it is formatted using formatException() and appended to the message.
formatException#
Format and return the specified exception information as a string.
def formatException(self, ei)
This default implementation just uses traceback.print_exception()
formatMessage#
def formatMessage(self, record)
formatStack#
This method is provided as an extension point for specialized
def formatStack(self, stack_info)
formatting of stack information.
The input data is a string as returned from a call to
formatTime#
Return the creation time of the specified LogRecord as formatted text.
def formatTime(self, record, datefmt = None)
This method should be called from format() by a formatter which wants to make use of a formatted time. This method can be overridden in formatters to provide for any specific requirement, but the basic behaviour is as follows: if datefmt (a string) is specified, it is used with time.strftime() to format the creation time of the record. Otherwise, an ISO8601-like (or RFC 3339-like) format is used. The resulting string is returned. This function uses a user-configurable function to convert the creation time to a tuple. By default, time.localtime() is used; to change this for a particular formatter instance, set the 'converter' attribute to a function with the same signature as time.localtime() or time.gmtime(). To change it for all formatters, for example if you want all logging times to be shown in GMT, set the 'converter' attribute in the Formatter class.
sanic.logging.setup.setup_logging#
def setup_logging(debug: <class 'bool'>, no_color: <class 'bool'> = False, log_extra: typing.Union[bool, sanic.helpers.Default] = <Default>): -> None