In development or testing environment, you may set DEBUG=True
in your settings.py
file and whenever any error is thrown, you see it in the browser.
Setting DEBUG=True
in production environment is not recommended. Debug should be set to False on live servers so that error trace is not visible in case of any error. Hackers or malicious users may exploit this and may attack your web application.
So how do you debug you application on live servers?
Answer is Logging the errors in log files. Lets see how to start logging errors in Django?
Logger is the entry point to any logging system. It is kind of a bucket with a name where you push your error message.
Handler decides what needs to be done for each message in logger.
Filters put restrictions on each message and decide which message should be passed from logger to handler. For example only messages with severity level equal to or higher than 'error' should be processed.
Formatters decide the format in which error messages should be displayed as text.
Now lets see the coding version of the process. We will implement simplest version of logging.
create a separate file for logging settings. Lets name it logger_settings.py
. Place this file along with settings file in project folder.
Put the below code in this file and save. We will explain later what each thing means in the code.
LOGGING = { 'version':1, 'disable_existing_loggers': False, 'formatters':{ 'large':{ 'format':'%(asctime)s %(levelname)s %(process)d %(pathname)s %(funcName)s %(lineno)d %(message)s ' }, 'tiny':{ 'format':'%(asctime)s %(message)s ' } }, 'handlers':{ 'errors_file':{ 'level':'ERROR', 'class':'logging.handlers.TimedRotatingFileHandler', 'when':'midnight', 'interval':1, 'filename':'logs/ErrorLoggers.log', 'formatter':'large', }, 'info_file':{ 'level':'INFO', 'class':'logging.handlers.TimedRotatingFileHandler', 'when':'midnight', 'interval':1, 'filename':'logs/InfoLoggers.log', 'formatter':'large', }, }, 'loggers':{ 'error_logger':{ 'handlers':['errors_file'], 'level':'WARNING', 'propagate':False, }, 'info_logger':{ 'handlers':['info_file'], 'level':'INFO', 'propagate':False, }, }, }
Now import the logger_settings.py
file in main settings.py
file. Add these lines in settings.py
file.
# importing logger settings try: from .logger_settings import * except Exception as e: # in case of any error, pass silently. pass
In any view, throw an error and log it. Import logging and log the error in exception handling.
import logging def index(request): try: raise Exception("Custom error thrown by newbie developer :D") except Exception as e: logging.getLogger("error_logger").error(repr(e)) pass
Once you hit the url pointing to above view function, you will see an ErrorLogger<today's date>.log file will be created in current working project directory myproject/logs/
and error will be logged in it.
disable_existing_loggers
if set to True
disable any default loggers. This doesn't remove loggers from system but simply deactivate them and any such loggers simply discard any logging message received by them.error_file
and info_file
. error_file
handler will handle messages with log level equal to or greater than error as defined by level key. logging.handlers.TimedRotatingFileHandler
value in class field defines that files are created on time basis. When field have value midnight which means new file is created at midnight. File name is the path to file. This path should have write permission. Formatter key define the formatter to use, large in this case.error_logger
and info_logger
. We defined the log level and handler to use for each logger. If you do not want parent of any logger to capture and log the message then set propagation to false.
Here we extended that project and uploaded on github.
Github url: https://github.com/anuragrana/Error_Logging_Django.git
Now host your Django app on server and access it anywhere. Test your application with real users.