Introduction

Server logs contain valuable information for debugging and monitoring. Effective log parsing helps identify issues, track performance, and understand system behavior.

Common Log Formats

Apache Access Logs

127.0.0.1 - - [07/Dec/2024:10:00:00 +0000] "GET /api/users HTTP/1.1" 200 1234

Nginx Access Logs

127.0.0.1 - - [07/Dec/2024:10:00:00 +0000] "GET /api/users HTTP/1.1" 200 1234 "http://example.com" "Mozilla/5.0"

JSON Logs

{
  "timestamp": "2024-12-07T10:00:00Z",
  "level": "ERROR",
  "message": "Database connection failed",
  "error": "Connection timeout"
}

Parsing Techniques

Regex Parsing

const logLine =
  '127.0.0.1 - - [07/Dec/2024:10:00:00 +0000] "GET /api HTTP/1.1" 200';

const regex = /^(\S+) .* \[(.*?)\] "(.*?)" (\d+) (\d+)/;
const match = logLine.match(regex);

if (match) {
  const [, ip, timestamp, request, status, size] = match;
  console.log({ ip, timestamp, request, status, size });
}

Tools

Use our tools:

Conclusion

Log parsing enables:

Benefits:

  • Error identification
  • Performance analysis
  • Security monitoring
  • System understanding

Next Steps