Log messages should include sufficient context to make them useful for debugging, monitoring, and audit trails. Include relevant entity IDs, operation details, and descriptive information that helps identify what was happening when the log entry was created.
Key practices:
Example of good contextual logging:
# Good - includes IDs and operation context
Rails.logger.info("Completed video conversion for upload ID #{upload.id} and job ID #{args[:job_id]}")
Rails.logger.error("Upload #{upload.id} URL remained blank after #{MAX_RETRIES} retries when optimizing video")
# Poor - lacks context
Rails.logger.info("Conversion completed")
Rails.logger.error("URL blank after retries")
This approach ensures log entries provide actionable information for troubleshooting and create comprehensive audit trails for critical operations like user impersonation or data processing workflows.
Enter the URL of a public GitHub repository