I am planning to use FileHandler for logging sanic service logs to a file. I would also like to have all the logs go to a single file as well. For example,
Logs like these should go in the service.log file:
[2020-12-11 19:00:32 +0530] [12236] [INFO] Goin' Fast @ http://0.0.0.0:8561
INFO:sanic.root:Starting worker [12239]
INFO:sanic.root:Starting worker [12240]
[2020-12-11 13:30:33 +0000] [12239] [INFO] Starting worker [12239]
[2020-12-11 13:30:33 +0000] [12240] [INFO] Starting worker [12240]
In the same way, I would also like to have the logs in the same file(service.log) for the calls when a URL is hit from the client-side as below:
[2020-12-11 13:32:21 +0000] - (sanic.access)[INFO][127.0.0.1:55829]: GET http://localhost:8561/ping 200 15
and cases when exceptions arise like below which should also go into the same file(service.log):
File "/Users/ajay/.pyenv/versions/prescription_sanic/lib/python3.7/site-packages/tortoise/queryset.py", line 110, in resolve_filters
modifier &= node.resolve(model, annotations, custom_filters, model._meta.basetable)
File "/Users/ajay/.pyenv/versions/prescription_sanic/lib/python3.7/site-packages/tortoise/query_utils.py", line 386, in resolve
return self._resolve_kwargs(model, table)
File "/Users/ajay/.pyenv/versions/prescription_sanic/lib/python3.7/site-packages/tortoise/query_utils.py", line 344, in _resolve_kwargs
filter_modifier = self._resolve_regular_kwarg(model, key, value, table)
File "/Users/ajay/.pyenv/versions/prescription_sanic/lib/python3.7/site-packages/tortoise/query_utils.py", line 303, in _resolve_regular_kwarg
criterion, join = _process_filter_kwarg(model, key, value, table)
File "/Users/ajay/.pyenv/versions/prescription_sanic/lib/python3.7/site-packages/tortoise/query_utils.py", line 43, in _process_filter_kwarg
else model._meta.db.executor_class._field_to_db(field_object, value, model)
File "/Users/ajay/.pyenv/versions/prescription_sanic/lib/python3.7/site-packages/tortoise/backends/base/executor.py", line 178, in _field_to_db
return field_object.to_db_value(attr, instance)
File "/Users/ajay/.pyenv/versions/prescription_sanic/lib/python3.7/site-packages/tortoise/fields/base.py", line 181, in to_db_value
return self.field_type(value) # pylint: disable=E1102
ValueError: invalid literal for int() with base 10: '456407^'
This currently happens when using the debug mode but using the below logging file, access logs go into service.log, error logs go into exceptions.log, and server start and stop logs go into the console. Is there any way to log them all in one file and also log them in a separate file as well to read them individually?
import sys
import logging
import aiotask_context as context
class RequestIdFilter(logging.Filter):
def filter(self, record):
record.request_id = context.get('X-REQUEST-ID')
return True
LOGGING_CONFIG_DEFAULTS = dict(
version=1,
disable_existing_loggers=False,
loggers={
"sanic.root": {
"level": "INFO",
"handlers": ["console"]
},
"sanic.error": {
"level": "DEBUG",
"handlers": ["error_console"],
"qualname": "sanic.error",
},
"sanic.access": {
"level": "INFO",
"handlers": ["access_console"],
"qualname": "sanic.access",
"filters": ["request_id"],
},
},
filters={
"request_id": {
"()": RequestIdFilter,
}
},
handlers={
"console": {
"class": "logging.StreamHandler",
"formatter": "generic",
"stream": sys.stdout,
},
"access_console": {
"class": "logging.FileHandler",
"formatter": "access",
"filename": "logs/sanic_service.log"
},
"error_console": {
"class": "logging.FileHandler",
"formatter": "generic",
"filename": "logs/sanic_exceptions.log"
},
},
formatters={
"generic": {
"format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s",
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
"class": "logging.Formatter",
},
"access": {
"format": "%(asctime)s - (%(name)s)[%(levelname)s] - [%(host)s]: "
+ "%(request)s %(message)s %(status)d %(byte)d %(time_spent)s %(request_id)s",
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
"class": "logging.Formatter",
},
},
)