Why Django Writing Doesn't Work

This is my settings module:

LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'file': { 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': '/django-python/django/testapp/testapp.log', }, }, 'loggers': { 'django': { 'handlers': ['file'], 'level': 'DEBUG', 'propagate': True, }, }, } 

and this is my code in a view file:

 import logging logger = logging.getLogger(__name__) logger.info("this is an error message!!") 

I get previous logs from different modules, but in this case, the log is "this error message."

+7
django django-logging
source share
3 answers

Your logging configuration only logs in the django namespace.

This line:

 logger = logging.getLogger(__name__) 

... tells the registrar to use your module name as the namespace for these logs ( docs ). If your module is called mymodule , you can catch these logs by adding something like this to your logging configuration:

 'loggers': { 'django' : {...}, 'mymodule': { 'handlers': ['file'], 'level': 'DEBUG', 'propagate': True, }, }, 
+16
source share

you have to add the registrar configuration because of your application name - something like

  'your_app_name': { 'handlers': ['file'], 'level': 'DEBUG', 'propagate': True, }, 

By now, you have only declared a logger for django default messages (e.g. system errors)


Please note that the logger message level is important, so when you use

  logger.info("this is an error message!!") 

A way to print a message, your registrar level must be INFO or more strict.

+3
source share
 import logging logger = logging.getLogger(__name__) 

after adding:

 logging.basicConfig( level = logging.DEBUG, format = '%(name)s %(levelname)s %(message)s', ) 

or just add settings.py:

import log

 logging.basicConfig( level = logging.DEBUG, format = '%(name)s %(levelname)s %(message)s', ) 

we can change the format:

 format = '"%(levelname)s:%(name)s:%(message)s" ', 

or

 format = '%(name)s %(asctime)s %(levelname)s %(message)s', 
0
source share

All Articles