How to Configure Logging for Horilla HRMS

Logging is an essential part of any application, allowing developers and administrators to monitor system activities, debug issues, and track errors effectively.
In this blog, we will walk through setting up a robust logging system for Horilla HRMS using Django’s built-in logging framework.
Why is Logging Important?
Logging provides insights into how your application is functioning and helps in:
- Debugging issues in development and production environments.
- Tracking errors and warnings to prevent potential failures.
- Maintaining records of application usage and activities.
Setting Up Logging in Horilla HRMS
Django provides a flexible logging configuration using Python’s built-in logging module. Let’s configure logging to capture important application events.
Step 1: Define Log File Path
Before setting up logging, specify a path where logs will be stored. Add the following lines to horilla/settings.py file as follows:
import os
from os.path import join
from pathlib import Path
import environ
from django.contrib.messages import constants as messages
BASE_DIR = Path(__file__).resolve().parent.parent
LOG_FILE_PATH = os.path.join(BASE_DIR, "horilla.log") #add these line
# Other horilla/settings.py content
...
Step 2: Configure the Logging Dictionary
Now, define the logging configuration inside horilla/settings.py:
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"format": "[{levelname} | {asctime} | {module}] {message}",
"style": "{",
},
"simple": {
"format": "{levelname} | {message}",
"style": "{",
},
},
"handlers": {
"file": {
"level": "INFO",
"class": "logging.FileHandler",
"filename": LOG_FILE_PATH,
"formatter": "verbose",
},
"console": {
"level": "INFO",
"class": "logging.StreamHandler",
"formatter": "simple",
},
# rotating logs mentioned in step 4
"rotating_file": {
"level": "INFO",
"class": "logging.handlers.RotatingFileHandler",
"filename": LOG_FILE_PATH,
"maxBytes": 5 * 1024 * 1024, # 5MB
"backupCount": 5,
"formatter": "verbose",
},
},
"loggers": {
"django": {
"handlers": [
"console",
"rotating_file",
"file",
],
"level": "INFO",
"propagate": True,
},
"django.request": {
"handlers": [
"console",
"rotating_file",
"file",
],
"level": "ERROR",
"propagate": False,
},
},
}
Step 3: Understanding the Logging Configuration
Formatters: Define how log messages are formatted. The verbose formatter includes timestamps, module names, and log levels.
Handlers: Specify where logs should be directed. We have:
- file handler to write logs to horilla.log in the base directory
- console handler to print logs on the terminal.
Loggers: Determine which parts of Django’s framework will generate logs. We have configured:
- The django logger to log INFO level messages and above.
- The django.request logger to capture ERROR level messages only.
Run your Django project, and you should see logs appearing in horilla.logs and in your console.
Eg. horilla.log file
[INFO | 2025-03-08 15:28:09,474 | autoreload] Watching for file changes with StatReloader
[INFO | 2025-03-08 15:28:12,386 | basehttp] "GET /inbox/notifications/api/unread_list/?max=5 HTTP/1.1" 200 7335
[INFO | 2025-03-08 15:28:12,386 | basehttp] "GET /inbox/notifications/api/unread_list/?max=5 HTTP/1.1" 200 7335
[INFO | 2025-03-08 15:28:12,446 | basehttp] "GET /inbox/notifications/api/unread_list/?max=5 HTTP/1.1" 200 7335
[INFO | 2025-03-08 15:28:12,446 | basehttp] "GET /inbox/notifications/api/unread_list/?max=5 HTTP/1.1" 200 7335
[INFO | 2025-03-08 15:29:11,904 | basehttp] "GET /inbox/notifications/api/unread_list/?max=5 HTTP/1.1" 200 7335
[INFO | 2025-03-08 15:29:11,904 | basehttp] "GET /inbox/notifications/api/unread_list/?max=5 HTTP/1.1" 200 7335
Step 4: Rotating Logs (Optional)
For better log management, use TimedRotatingFileHandler or RotatingFileHandler to prevent log files from growing indefinitely. You can implement this in the handlers section:
from logging.handlers import RotatingFileHandler
“rotating_file”: {
“level”: “INFO”,
“class”: “logging.handlers.RotatingFileHandler”,
“filename”: LOG_FILE_PATH,
“maxBytes”: 5*1024*1024, # 5MB
“backupCount”: 5,
“formatter”: “verbose”,
},
This configuration ensures that the log file does not exceed 5MB, and keeps a backup of the last five log files.
Step 5: Rotating Logs (Optional)
For better log management, use RotatingFileHandler to prevent log files from growing indefinitely. The configuration for rotating logs has been included inside the logging dictionary as shown in Step 2.
Conclusion
Setting up logging in Horilla HRMS ensures efficient monitoring of application activities, errors, and performance issues. With a structured logging system in place, developers can streamline debugging, enhance maintenance, and proactively address issues. Implementing rotating logs further helps in managing log files effectively, preventing them from consuming excessive disk space. By following this guide, you can establish a reliable logging mechanism that contributes to a smoother and more maintainable development experience.