Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
logging Django   1   21126
How to start logging errors in Django app

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?



Introduction:

Django uses python's built in logging module. Django's logging module consist four players.
  • Loggers
  • Handlers
  • Filters
  • Formatters


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.



Log levels in python:

DEBUG: Low level system information for debugging purposes
INFO: General system information
WARNING: Information describing a minor problem that has occurred.
ERROR: Information describing a major problem that has occurred.
CRITICAL: Information describing a critical problem that has occurred.

Now lets see the coding version of the process. We will implement simplest version of logging.



How to Log errors in log files:

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.



Understanding the code:

  • Django uses dictConfig format for logging configurations. Version defines the schema version to be used here. It accepts positive integer and as of now only acceptable valid value is 1 .
  • 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.
  • In the code above we defined two type of formatters, large and tiny. In large formatter we will be logging descriptive message with date time, log level, process, file name, function name, line number and error message. In tiny formatter we will be logging on message and date time.
  • We defined two type of handlers, 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.
  • We created two named loggers, 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.
 


Example with code:

In one of our previous article, we developed a hello world django project.

Here we extended that project and uploaded on github.


Github url: https://github.com/anuragrana/Error_Logging_Django.git  


For advance configuration:
  1. https://docs.python.org/3/library/logging.config.html
  2. https://docs.djangoproject.com/en/1.10/topics/logging/
 

Now host your Django app on server and access it anywhere. Test your application with real users.

logging Django   1   21126
1 comment on 'How To Start Logging Errors In Django App'
Login to comment

Subhranil Ghosh Feb. 4, 2020, 9:06 a.m.
how to change log file name? E.g- from ErrorLoggers.log.2020-01-23_12 to ErrorLoggers_2020-01-23_12.log or ErrorLoggers.2020-01-23_12.log

Related Articles:
Hello Word in Django: How to start with Django
Creating a hello world Django app, Starting the development of Django application in less than 5 minutes, How to start development in python Django, your first application in Django...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap