How can the Python built‑in logging module be configured and used to meet all requirements in a financial database plan? What log levels and handlers would you recommend?
💡 Model Answer
For a financial database, logging must capture query execution, schema changes, and security events. Configure a dedicated logger with multiple handlers: a RotatingFileHandler for audit trails, a SysLogHandler for real‑time monitoring, and optionally a KafkaHandler to stream logs to a SIEM. Example configuration:
import logging, logging.config
config = {
'version': 1,
'formatters': {
'json': {
'class': 'pythonjsonlogger.jsonlogger.JsonFormatter',
'format': '%(asctime)s %(levelname)s %(name)s %(message)s'
}
},
'handlers': {
'file': {
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/var/log/finance/db.log',
'maxBytes': 10485760,
'backupCount': 10,
'formatter': 'json',
'level': 'INFO'
},
'syslog': {
'class': 'logging.handlers.SysLogHandler',
'address': ('localhost', 514),
'formatter': 'json',
'level': 'WARNING'
}
},
'loggers': {
'db': {
'handlers': ['file', 'syslog'],
'level': 'INFO',
'propagate': False
}
}
}
logging.config.dictConfig(config)
logger = logging.getLogger('db')Use INFO for successful queries, WARNING for slow queries or retries, ERROR for failed transactions, and CRITICAL for data corruption or unauthorized access. Structured JSON logs make it easier to ingest into analytics or compliance tools. This configuration satisfies audit, monitoring, and regulatory requirements while keeping the logging code maintainable.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500