0👍
You can define a logging format and add that to your handler.
For your example this could look like:
'formatters': {
'mystyle': {
'format': u'%(asctime)s %(levelname)-6s %(lineno)-4s%(name)-15s %(message)s',
'datefmt': '%H:%M:%S',
},
'handlers': {
'console': {...},
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': 'debug.log',
'format': 'mystyle',
},
},
The formatter is defined in an own section to make it reusable. I.e. multiple handlers could use the same formatter.
A list of what you can use as placeholders in your format can be found here (I think there is something like stack trace as well):
https://docs.python.org/3/library/logging.html#logrecord-attributes
👤Lukr
0👍
After some grilling and trial and error, this is my settings for logging debuug and error logs. This settings has the following:
- Captures error logs in a separate file with a formatter that gets the details.
- Captures complete traceback if there is a 500 error.
- Captures all 4XX errors.
- Captures debug logs in its own file.
In settings.py file:
# Logging settings
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"filters": {
"require_debug_false": {
"()": "django.utils.log.RequireDebugFalse",
},
"require_debug_true": {
"()": "django.utils.log.RequireDebugTrue",
},
},
"formatters": {
"verbose": {
"format": "{name} {levelname} {asctime} {module} {process:d} {thread:d} {message}",
"datefmt": "%Y-%m-%d",
"style": "{",
},
"simple": {
"format": "[{asctime}] [{levelname}] [{module}] > {message}",
"datefmt": "%Y-%m-%d: %H:%M:%S",
"style": "{",
},
"with_error_detail": {
"format": "[{asctime}] [{module}] > {message} >> {exc_info}",
"datefmt": "%Y-%m-%d: %H:%M:%S",
"style": "{",
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
"file_debug": {
"class": "logging.FileHandler",
"filename": "logs/debug.log",
"formatter": "simple",
},
"file_error": {
"class": "logging.FileHandler",
"filename": "logs/error.log",
"formatter": "with_error_detail",
"level": "WARNING",
},
},
"loggers": {
"django": {
"handlers": ["console"],
"level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
},
"django.request": {
"handlers": ["file_error"],
"level": "WARNING",
"propagate": False,
# "filters": ["require_debug_false"],
},
"meetings": {
"handlers": ["file_debug"],
"level": "DEBUG",
"propagate": True,
},
"users": {
"handlers": ["console"],
"level": "DEBUG",
"propagate": False,
},
},
}
Here’s a sample of some logs captured (I am showing a part of the traceback here..):
[2023-08-15: 01:04:01] [ERROR] [log] > Internal Server Error: /api/users/register/user/
[2023-08-15: 01:04:20] [ERROR] [log] > Internal Server Error: /api/users/register/user/
[2023-08-15: 23:54:01] [log] > Internal Server Error: /api/users/register/user/ >> None
[2023-08-16: 00:06:43] [log] > Bad Request: /api/users/register/user/ >> None
[2023-08-16: 00:16:52] [log] > Unauthorized: /api/users/user/1234/ >> None
[2023-08-16: 00:17:19] [log] > Not Found: /api/meeting/timings/4699/ >> None
[2023-08-16: 00:17:54] [log] > Not Found: /api/meeting/timing/4699/ >> None
[2023-08-16: 00:20:34] [log] > Internal Server Error: /api/users/register/patient/ >> (<class 'AttributeError'>, AttributeError("Got AttributeError when attempting to get a value for field `email` on serializer `UserRegisterSerializer`.\nThe serializer field might be named incorrectly and not match any attribute or key on the `User` instance.\nOriginal exception text was: 'User' object has no attribute 'email'."), <traceback object at 0x00000240E8A48500>)
Traceback (most recent call last):
File "C:\Projects\my-django-project\.venv\lib\site-packages\rest_framework\fields.py", line 446, in get_attribute
return get_attribute(instance, self.source_attrs)
File "C:\Projects\my-django-project\.venv\lib\site-packages\rest_framework\fields.py", line 96, in get_attribute
- [Django]-Cannot concatenate 'str' and 'tuple' objects – Django – johnny cache
- [Django]-Producing a WAR file from a django project with SQLite
- [Django]-Django user proxy models fast access
Source:stackexchange.com