## 프로젝트 개요
대규모 로그 파일에서 특정 키워드를 기준으로 라인을 필터링하여 결과를 출력하는 CLI 도구입니다. 메모리 최적화된 I/O 처리와 안전한 에러 처리를 통해 시스템 프로그래밍의 핵심 원칙을 적용했습니다.
## 주요 기능
- 명령행 인자로 입력/출력 파일 및 필터 키워드 지정
- 메모리 효율적인 스트리밍 방식으로 대규모 로그 처리
- 에러 상황에 대한 정교한 오류 처리
- 동적 필터링 조건 확장 가능
## 사용 방법
1. `cargo build --release`로 컴파일
2. `./log-filter input.log output.log ERROR` 형식으로 실행
3. 필터 키워드가 포함된 라인만 출력 파일에 저장
## 확장 가능성
- 로그 포맷 별 파서 추가
- 실시간 모니터링 모드 구현
- 멀티스레드 처리를 통한 성능 최적화
🦀 실용적인 로그 파일 필터링 도구
Rust 예제
중급
난이도
예제
타입
10/25
등록일
실용적인 로그 파일 필터링 도구
중급태그
코드 예제
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader, Write};
use clap::{Arg, App};
#[derive(Debug)]
enum FilterError {
Io(std::io::Error),
NoMatchingLine,
}
fn main() -> Result<(), FilterError> {
let matches = App::new("log-filter")
.arg(Arg::with_name("input")
.short("i")
.long("input")
.value_name("FILE")
.required(true)
.help("입력 로그 파일 경로"))
.arg(Arg::with_name("output")
.short("o")
.long("output")
.value_name("FILE")
.required(true)
.help("출력 파일 경로"))
.arg(Arg::with_name("filter")
.short("f")
.long("filter")
.value_name("KEYWORD")
.required(true)
.help("필터링 키워드"))
.get_matches();
let input_path = matches.value_of("input").unwrap();
let output_path = matches.value_of("output").unwrap();
let filter_keyword = matches.value_of("filter").unwrap();
// 입력 파일 열기 (메모리 효율적 처리)
let input_file = File::open(input_path).map_err(FilterError::Io)?;
let reader = BufReader::new(input_file);
// 출력 파일 생성 및 준비
let mut output_file = OpenOptions::new()
.write(true)
.create_new(true)
.open(output_path)
.map_err(FilterError::Io)?;
// 라인 별 처리 (메모리 최적화)
for line in reader.lines() {
let line = line.map_err(FilterError::Io)?;
if line.contains(filter_keyword) {
writeln!(output_file, "{}", line).map_err(FilterError::Io)?;
}
}
Ok(())
}
// 에러 처리 예시: 특정 조건 시 에러 발생
fn check_condition(condition: bool) -> Result<(), FilterError> {
if condition {
return Err(FilterError::NoMatchingLine);
}
Ok(())
}
등록일: 2025년 10월 25일 02:38
언어 정보
언어
Rust
카테고리
System
인기도
#9
학습 팁
코드를 직접 실행해보세요
변수를 바꿔가며 실험해보세요
오류가 나도 포기하지 마세요
다른 예제도 찾아보세요