This Rust project analyzes log files to provide insights into their content. It can perform the following tasks:
- Count the total number of lines in a log file.
- Count the occurrences of specific keywords within the log file.
- Identify and extract IP addresses from the log file.
log-analyzer/
├── Cargo.lock
├── Cargo.toml
├── src/
│ └── main.rs
└── test_log.log
Cargo.lock
: Auto-generated file that specifies the exact versions of dependencies used.Cargo.toml
: Project configuration file that defines dependencies.src/main.rs
: The main Rust source code file.test_log.log
: A sample log file for testing.
- Line Counting: Efficiently counts the total number of lines in a given log file.
- Keyword Search: Allows you to specify keywords and counts their occurrences throughout the log file.
- IP Address Extraction: Identifies and lists all IP addresses found within the log file.
- Rust and Cargo installed. You can follow the instructions on the official Rust website to install them: https://www.rust-lang.org/tools/install
- Clone the repository:
bash
git clone <repository-url>
cd log-analyzer
- Build the project:
bash
cargo build --release
- Run the following command:
bash
cargo run --release <path-to-log-file> <keyword1> <keyword2> ...
The program will print:
- The total number of lines
- The number of occurrences of the keywords
- A list of the ip addresses
For example, to analyze test_log.log
for the keywords "error" and "warning":
bash
cargo run --release test_log.log error warning
Assume test_log.log
contains the following:
2023-10-27 00:00:00 INFO: System started. IP: 192.168.1.1
2023-10-27 00:00:05 WARNING: Low disk space. IP: 10.0.0.1
2023-10-27 00:00:10 ERROR: Failed to connect. IP: 8.8.8.8
2023-10-27 00:00:15 INFO: System is running.
2023-10-27 00:00:20 ERROR: File not found. IP: 172.16.0.1
Running:
bash
cargo run --release test_log.log error warning
Will output:
Total lines: 5
Occurrences of 'error': 2
Occurrences of 'warning': 1
IP Addresses found: ["192.168.1.1", "10.0.0.1", "8.8.8.8", "172.16.0.1"]
Contributions are welcome! Please feel free to submit pull requests or open issues.
This project is licensed under the MIT License.