Django unittest - How to disable console logging only - django

I have a test in django (v2.2) that generates an error on purpose to check that errors are logged in the proper log files.
But I get also a print in the console which is kind of annoying, I was looking for a way to hide the message.
If I disable logging with logging.disable(logging.CRITICAL), it will not log the error in the files.
I also tried to use with self.settings(LOGGING=logging) after removing the console handler from the loggers (see settings below) but it does not seem to be taken into account.
Also, setting disable_existing_logger to True does not help.
Do you have some ideas how to do that?
my settings:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'msg_filter': {
'()': MessageFilter,
'strings_to_be_ignored': [
'Not Found: /favicon.ico'
],
}
},
'formatters': {
'verbose': {
'format': '_____________________________________________________________'
'\n%(asctime)s - [%(levelname)s|%(name)s] %(message)s\n'
},
'simple': {
'format': '%(asctime)s - [%(levelname)s|%(name)s] %(message).50s'
},
},
'handlers': {
'warning': {
'level': 'WARNING',
'class': 'logging.FileHandler',
'filename': '/some_folder/warning.log',
'formatter': 'verbose',
'filters': ['msg_filter']
},
'warning_simple': {
'level': 'WARNING',
'class': 'logging.FileHandler',
'filename': '/some_folder/warning.log',
'formatter': 'simple',
'filters': ['msg_filter']
},
'info': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/some_folder/info.log',
'maxBytes': 1024 * 1024 * 2,
'backupCount': 9,
'formatter': 'simple',
'filters': ['msg_filter']
},
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'verbose',
'filters': ['msg_filter']
},
'console_simple': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'simple',
'filters': ['msg_filter']
}
},
'loggers': {
'': {
'handlers': ['console', 'info', 'warning'],
'level': 'DEBUG',
'propagate': False,
},
'django': {
'handlers': ['console_simple', 'info', 'warning_simple'],
'level': 'DEBUG',
'propagate': False,
}
}
}

You should set disable_existing_loggers variable True. This variable being true means the loggers is disabled.
Read this:
https://docs.djangoproject.com/en/3.0/topics/logging/#configuring-logging

you can add the --logging-clear-handlers option.
python manage.py test --logging-clear-handlers

Related

How to log Django warnings and errors to log file in Production?

What I want to achieve is that warning and errors that happen in production (i.e. DEBUG=False) are logged into to a rotating log file.
I tried this
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'handlers': {
'file': {
'level': 'WARNING',
'class': 'logging.FileHandler',
'filename': 'staging.log',
},
},
'loggers': {
'': {
'handlers': ['file'],
'level': 'WARNING',
'propagate': True,
},
'django': {
'handlers': ['file'],
'level': 'WARNING',
'propagate': True,
},
'django.request': {
'handlers': ['file'],
'level': 'WARNING',
'propagate': True,
},
},
}
However, the above logs everything, not just messages of log level WARNING and above.
You can follow the django logger here.
In your views.py:
import logging
logger = logging.getLogger(__name__)
then you can record using logger.error() or logger.warning() or logger.info().
It will create a logger file in your main project directory and it will list out all the logging details.
See this:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
'style': '{',
},
'simple': {
'format': '{levelname} {message}',
'style': '{',
},
},
'handlers': {
'logfile': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': "yourproject_name.log",
'maxBytes': 100000,
'backupCount': 2,
'formatter': 'verbose',
},
},
'loggers': {
'django': {
'handlers': ['logfile'],
'level': 'INFO',
'propagate': True,
},
'apps': {
'handlers': ['logfile'],
'level': 'DEBUG',
'propagate': True,
},
},
}

How to *always* log exceptions and stacktraces in django with DEBUG = False

How do I set DEBUG = False, but ensure exceptions (wherever they may be thrown) are properly logged, including a stack trace.
Here is my logging configuration:
LOGGING = {
'version': 1,
'filters': {
'require_debug_false': {'()': 'django.utils.log.RequireDebugFalse'},
'require_debug_true': {'()': 'django.utils.log.RequireDebugTrue'}
},
'formatters': {
'django.server': {
'()': 'django.utils.log.ServerFormatter',
'format': '[%(server_time)s] %(message)s'
}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'filters': ['require_debug_true'],
'level': 'DEBUG'
},
'django.server': {
'class': 'logging.StreamHandler',
'formatter': 'django.server',
'level': 'INFO'
},
'mail_admins': {
'class': 'django.utils.log.AdminEmailHandler',
'filters': ['require_debug_false'],
'level': 'ERROR'
}
},
'root': {
'level': 'DEBUG',
'handlers': ['console']
},
'loggers': {
'django.request': {
'handlers': ['console'],
'propagate': False,
'level': 'DEBUG',
},
'django': {
'handlers': ['console'],
'propagate': False,
'level': 'DEBUG',
},
'django.template': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
'django.security': {
'handlers': ['mail_admins'],
'level': 'ERROR',
'propagate': False,
},
},
}
Though there is no default config where all exceptions are being recorded in a log file, there are couple of options for you to try out in django:
Configure the ADMINS setting, to receive emails about all exceptions in your site
You can write a custom middleware, that has a process_exception method that writes to the logger with logger.error('Exception info)

store data in the log files

I am using Django 1.8 for a project. I have kept the logs in the settings as :
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(levelname)s %(asctime)s %(module)s \
%(process)d %(lineno)d %(thread)d %(message)s'
},
'simple': {
'format': '%(levelname)s %(lineno)d %(message)s'
},
},
'handlers': {
'null': {
'level': 'DEBUG',
'class': 'logging.NullHandler',
'formatter': 'verbose'
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
},
'console_simple': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
'formatter': 'verbose'
},
'requests': {
'level': 'INFO',
'class': 'logging.FileHandler',
'filename': '/var/log/request.log',
'formatter': 'verbose'
},
},
'loggers': {
'django': {
'handlers': ['null'],
'propagate': True,
'level': 'INFO',
},
'django.request': {
'handlers': ['console'],
'level': 'WARNING',
'propagate': False,
},
'logger': {
'handlers': ['requests'],
'level': 'INFO',
'propagate': False,
},
},
}
I get errors regularly as Bad Request, so I want to see what data is sent through the front-end to the function calls.
I want to know if there is a way so that I can store the data sent by the front-end to the functions?Any advances will be appreciated.
You can add the loggers to the log the content in log file.
'logfile': {
'level':'DEBUG',
'class':'logging.handlers.RotatingFileHandler',
'filename': SITE_ROOT + "/logfile",
'maxBytes': 50000,
'backupCount': 2,
'formatter': 'standard',
},
Take a look at Simple Log to File example for django 1.3+

Django error email report not being sent

I have struggling with django(1.5.1) error email reports not being sent.
here is my conf settings to use with gmail
DEFAULT_FROM_EMAIL = 'server#example.com'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_HOST_USER = 'server#example.com'
EMAIL_HOST_PASSWORD = 'passs'
EMAIL_USE_TLS = True
SERVER_EMAIL = 'server#example.com'
ADMINS = (
('Adam Min', 'adam#example.com'),
)
If I add MANAGERS = ADMINS then I receive emails for 404's
but without MANAGERS setting I receive nothing at all.
I have created a buggy url so I can test this.
Also I found this similar Q Django emailing on errors but it didn't help me.
EDIT:
also in config I have DEBUG = False
and this
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'verbose': {
'format': '%(levelname)s [%(asctime)s] %(module)s %(message)s'
},
'simple': {
'format': '%(levelname)s %(message)s'
},
},
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
}
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple'
},
'file': {
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'verbose',
'filename': '/var/www/logs/ibiddjango.log',
'maxBytes': 1024000,
'backupCount': 3,
},
'sql': {
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'verbose',
'filename': '/var/www/logs/sql.log',
'maxBytes': 102400,
'backupCount': 3,
},
'commands': {
'class': 'logging.handlers.RotatingFileHandler',
'formatter': 'verbose',
'filename': '/var/www/logs/commands.log',
'maxBytes': 10240,
'backupCount': 3,
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django': {
'handlers': ['file', 'console'],
'propagate': True,
'level': 'DEBUG',
},
'django.db.backends': {
'handlers': ['sql', 'console'],
'propagate': False,
'level': 'WARNING',
},
'scheduling': {
'handlers': ['commands', 'console'],
'propagate': True,
'level': 'DEBUG',
},
}
}
What am I missing?
it seems that your problem is in your logging configuration: in settings.py LOGGING:
'handlers': {
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
}
This config indicate that mail_admins handler work only in DEBUG = False because the filter used.
If you try with mode debug false or you can activate this handler in debug mode just comment the filters:
'handlers': {
'mail_admins': {
'level': 'ERROR',
#'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
},
}
Edit:
Your configuration doesn't call mail_admins handler. Add it to the django logger like this:
'loggers': {
'django': {
'handlers': ['file', 'console', 'mail_admins',],
'propagate': True,
'level': 'DEBUG',
},

Why does my Django 1.3 logging setup cause all messages to be output twice?

My Django 1.3 logging setup causes all log messages to be output twice. I've read that importing settings.py two or more times has caused this problem in the past, but Django 1.3 has new logging features, and I don't think I'm importing settings.py twice anywhere.
settings.py config:
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
'format': '%(levelname)s: %(message)s'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
},
'loggers': {
'custom': {
'handlers': ['console'],
'level': 'DEBUG',
},
}
}
Code:
import logging
log = logging.getLogger('custom')
log.debug('message1')
log.debug('message2')
Output:
DEBUG: message1
DEBUG:custom:message1
Thanks for your help.
Have you tried setting propagate = False? Along with disable_existing_loggers = True?
LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'simple': {
'format': '%(levelname)s: %(message)s'
},
},
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
},
'loggers': {
'custom': {
'handlers': ['console'],
'level': 'DEBUG',
'propagate': False,
},
}
}
You might want to try 'disable_existing_loggers': True?
I've been suffered by the same issue. And I fixed it by redirecting the root logs to a file, and project logs to both the file and the console.
I grep my code, and could not find anywhere basicConfig() exists, also tried to set disable_existing_loggers to True, It doesn't help, finally solved the problem by set a file logger. I guess it maybe a problem by design in some cases.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
# exact format is not important, this is the minimum information
'format': '%(asctime)s %(name)-12s %(lineno)d %(levelname)-8s %(message)s',
},
},
'handlers': {
'console': {
#'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'simple',
},
# Add Handler for mail_admins for `warning` and above
'mail_admins': {
'level': 'ERROR',
'class': 'django.utils.log.AdminEmailHandler',
},
'file': {
#'level': 'INFO',
'class': 'logging.FileHandler',
'formatter': 'simple',
'filename': os.path.join(os.path.dirname(PROJECT_ROOT), 'crawler.admin.log'),
},
},
'loggers': {
# root logger
'': {
'level': 'INFO',
'handlers': ['file', 'mail_admins'],
},
'scrapy': {
'level': 'WARNING',
'handlers': ['console', 'mail_admins'],
'propagate': False,
},
'crawleradmin': {
'level': 'INFO',
'handlers': ['console', 'file', 'mail_admins'],
# required to avoid double logging with root logger
'propagate': False,
},
},
}