A comprehensive Java-based system for reading, parsing, processing, and analyzing application logs with persistent storage in PostgreSQL.
This log processing system is designed to efficiently handle large volumes of application logs by:
- Reading log files line by line
- Parsing structured log entries
- Aggregating and analyzing log data
- Storing results in a PostgreSQL database for further analysis
The system follows a modular, pipeline-based architecture with clear separation of concerns:
Log File → Reader → Parser → Aggregator → Database- Reads log files using buffered I/O for efficiency
- Uses a consumer-based callback pattern for flexible line processing
- Handles file I/O errors gracefully
- Parses individual log lines into structured
LogEntryobjects - Expects log format:
YYYY-MM-DD HH:MM:SS LEVEL user=USERNAME action=ACTION - Returns
nullfor invalid log entries (safeguards against malformed data) - Extracts: timestamp, log level, user information, and action performed
- Data model representing a single log entry
- Fields:
timestamp: LocalDateTime of when the event occurredlevel: Log level (e.g., ERROR, INFO, DEBUG)action: The action performed (e.g., login, logout, file access)user: Username associated with the log entry
- Processes a list of
LogEntryobjects - Generates three key aggregations using Java Streams:
- Errors per Hour: Count of ERROR-level logs grouped by hour of day
- Errors per User: Count of ERROR-level logs grouped by username
- User Activity: Total activity count per user across all log levels
- Provides results in
Mapformat for easy database insertion
- Connects to PostgreSQL database at
localhost:5432/log_analytics - Writes aggregated data using UPSERT operations
- Tables supported:
errors_per_hour: Hour and error counterrors_per_user: Username and error countuser_activity: Username and total activity count
- Uses prepared statements for security and performance
- Orchestrates the entire pipeline
- Reads log file → Parses entries → Aggregates data → Writes to database
- Example usage processes
logs/test1.log
- Java 8 or higher
- PostgreSQL 10 or higher
- PostgreSQL JDBC driver
Create the required PostgreSQL database and tables:
CREATE DATABASE log_analytics;
CREATE TABLE errors_per_hour (
hour INT PRIMARY KEY,
error_count BIGINT NOT NULL
);
CREATE TABLE errors_per_user (
username VARCHAR(255) PRIMARY KEY,
error_count BIGINT NOT NULL
);
CREATE TABLE user_activity (
username VARCHAR(255) PRIMARY KEY,
activity_count BIGINT NOT NULL
);Update database credentials in DatabaseWriter.java:
URL: PostgreSQL connection URL (default:jdbc:postgresql://localhost:5432/log_analytics)USER: Database username (default:postgres)PASSWORD: Database password
The system expects log files with the following format:
YYYY-MM-DD HH:MM:SS LEVEL user=USERNAME action=ACTION2024-01-15 10:23:45 ERROR user=admin action=failed_login
2024-01-15 10:24:12 INFO user=john action=login
2024-01-15 10:25:30 ERROR user=admin action=unauthorized_access
2024-01-15 10:26:01 INFO user=jane action=file_uploadjavac -d bin src/**/*.javajava -cp bin:. MainEdit Main.java and change the log file path:
reader.readFile("path/to/your/log/file.log", line -> {
LogEntry entry = parser.parse(line);
if (entry != null) {
entries.add(entry);
}
});The system produces:
- Console Output: Aggregated statistics (errors per hour, errors per user, user activity)
- Database Storage: Persistent storage of aggregations for long-term analysis
- Invalid log entries are silently skipped (logged as
null) - File I/O errors are printed to stderr
- Database connection errors are handled with exception output
- Malformed log entries don't crash the system; processing continues
- Time Complexity: O(n) where n is the number of log entries
- Space Complexity: O(m) where m is the number of unique hours/users
- Scalability: Buffered I/O and streaming operations support large log files
- Database Operations: Batch processing with UPSERT for efficient updates
- Multi-threaded log processing for improved performance
- Support for additional log formats
- Real-time log streaming capabilities
- Advanced filtering and searching features
- Dashboard visualization of analytics
- Log rotation and compression support
log-processing-system/
├── Main.java # Entry point and pipeline orchestration
├── README.md # This file
├── db/
│ └── DatabaseWriter.java # PostgreSQL database operations
├── logs/
│ └── test1.log # Sample log file
├── model/
│ └── LogEntry.java # Log entry data model
├── parser/
│ └── LogParser.java # Log line parsing logic
├── processor/
│ └── LogAggregator.java # Log aggregation and analysis
└── reader/
└── LogFileReader.java # Log file reading operationsThis project is provided as-is for educational and organizational purposes.
For issues or questions regarding the log processing system, refer to the individual component documentation or review the source code comments.