This commit is contained in:
kingecg 2025-05-28 21:10:13 +08:00
parent dbbef66641
commit 946857a8b1
5 changed files with 144 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
dist/

31
Makefile Normal file
View File

@ -0,0 +1,31 @@
all: build
test: test_log test_run
build:
go build -o dist/gologf
run: build
./dist/gologf -start "START_PATTERN" -end "END_PATTERN" example.log
test_log:
@echo "生成测试日志文件"
@mkdir -p tests
@echo "2023-01-01 10:00:00 INFO Starting application" > tests/test.log
@echo "2023-01-01 10:01:00 DEBUG This is a debug message" >> tests/test.log
@echo "2023-01-01 10:02:00 START_PATTERN Important data here" >> tests/test.log
@echo "2023-01-01 10:03:00 INFO Processing data" >> tests/test.log
@echo "2023-01-01 10:04:00 END_PATTERN Data processing completed" >> tests/test.log
@echo "2023-01-01 10:05:00 INFO Application finished" >> tests/test.log
test_run: build
@echo "\n测试1: 匹配START_PATTERN到END_PATTERN之间的内容"
./dist/gologf -start "START_PATTERN" -end "END_PATTERN" tests/test.log
@echo "\n测试2: 仅匹配START_PATTERN直到文件结束"
./dist/gologf -start "DEBUG" tests/test.log
@echo "\n测试3: 没有匹配的内容"
./dist/gologf -start "NOT_EXISTING" tests/test.log
.PHONY: all test test_log test_run run

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.pyer.club/kingecg/gologf
go 1.23.1

103
main.go Normal file
View File

@ -0,0 +1,103 @@
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"regexp"
)
func main() {
// 定义命令行参数
startPattern := flag.String("start", "", "起始模式")
endPattern := flag.String("end", "", "截止模式")
// 解析命令行参数
flag.Parse()
// 参数校验
if flag.NArg() != 1 || *startPattern == "" {
fmt.Printf("用法: %s -start <起始模式> [-end <截止模式>] <日志文件名>\n", os.Args[0])
os.Exit(1)
}
// 获取非标志参数作为文件名
filename := flag.Arg(0)
// 编译起始正则表达式
startRegex, err := regexp.Compile(*startPattern)
if err != nil {
fmt.Printf("编译起始模式正则表达式失败: %v\n", err)
os.Exit(1)
}
// 编译截止正则表达式(如果提供了的话)
var endRegex *regexp.Regexp
if *endPattern != "" {
endRegex, err = regexp.Compile(*endPattern)
if err != nil {
fmt.Printf("编译截止模式正则表达式失败: %v\n", err)
os.Exit(1)
}
}
// 打开日志文件
file, err := os.Open(filename)
if err != nil {
fmt.Printf("无法打开文件 %s: %v\n", filename, err)
os.Exit(1)
}
defer file.Close()
// 创建带缓冲的读取器
reader := bufio.NewReader(file)
var inside bool // 标记是否在起始和截止模式之间
var lines []string // 存储匹配到的日志行
for {
// 读取一行
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break // 文件结束
}
fmt.Printf("读取文件时出错: %v\n", err)
os.Exit(1)
}
// 匹配起始模式
if !inside && startRegex.FindStringIndex(line) != nil {
inside = true
lines = append(lines, line) // 包含起始行
continue
}
// 如果已匹配起始模式,并且有截止正则表达式,则检查截止模式
if inside && endRegex != nil {
if endRegex.FindStringIndex(line) != nil {
inside = false
// 选择性地包含截止行(根据需求决定)
// lines = append(lines, line)
break // 找到第一个截止模式后退出循环
}
}
// 如果在起始和截止模式之间,则收集该行
if inside {
lines = append(lines, line)
}
}
// 输出匹配到的日志行
for _, line := range lines {
fmt.Println(line)
}
// 如果没有找到匹配的行,输出提示信息
if len(lines) == 0 {
fmt.Println("未找到匹配的日志内容")
}
}

6
tests/test.log Normal file
View File

@ -0,0 +1,6 @@
2023-01-01 10:00:00 INFO Starting application
2023-01-01 10:01:00 DEBUG This is a debug message
2023-01-01 10:02:00 START_PATTERN Important data here
2023-01-01 10:03:00 INFO Processing data
2023-01-01 10:04:00 END_PATTERN Data processing completed
2023-01-01 10:05:00 INFO Application finished