I am trying to configure my local logger to write the logs to the docker container, so I can see the logs in the list displayed by the command docker[-compose] logs <container> --tail 100
In settings.py
, I configured LOGGING
variable like this:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'default': {
'format': '[DJANGO] %(levelname)s %(asctime)s %(module)s '
'%(name)s.%(funcName)s:%(lineno)s: %(message)s'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'default',
}
},
'loggers': {
'': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': True,
}
},
}
In the files where I need the loggers, at the beginning, I am writing:
import logging
logger = logging.getLogger(__name__)
Then, in my code where I need it I write:
.......
logger.error('something happened')
.......
As I configured my logger, the log will be displayed in the console. But my problem is:
How can I display it in docker container when I call the aforementioned command ?
Example like desired output:
........
web_1 | [2022-02-23 17:37:10 +0200] [9] [INFO] ASGI 'lifespan' protocol appears unsupported.
web_1 | [2022-02-23 17:37:10 +0200] [9] [INFO] Application startup complete.
web_1 | [2022-02-23 17:37:10 +0200] [10] [INFO] Application startup complete.
web_1 | My log somewhere here..
........
Thank you very much!
2
Answers
You did everything right in Django to make your logs visible in docker logs. The only thing that I spotted is that you propagate such logs to the higher-level handler (e.g. gunicorn). You have two options:
The first option is to avoid propagation by setting
propagate
toFalse
:The other option is to configure your application server to avoid filtering, but this will depend on the configuration of your app server.
Maybe add this command in you docker-compose to fire at startup.