解决日志api的message字段为空的问题
This commit is contained in:
110
agent/main.go
110
agent/main.go
@@ -812,37 +812,105 @@ func collectLogs() ([]LogEntry, error) {
|
||||
// 使用字符串处理,更方便处理空格
|
||||
lineStr := string(line)
|
||||
|
||||
// 使用strings.Fields分割日志行,自动处理连续空格
|
||||
// 尝试多种时间格式解析
|
||||
t := time.Time{}
|
||||
err := error(nil)
|
||||
timestampEndIndex := -1
|
||||
|
||||
// 格式1: "Jan 2 15:04:05" 格式(如messages文件)
|
||||
fields := strings.Fields(lineStr)
|
||||
if len(fields) < 6 {
|
||||
// 尝试其他时间格式或跳过
|
||||
continue
|
||||
if len(fields) >= 3 {
|
||||
timeStr := fmt.Sprintf("%s %s %s", fields[0], fields[1], fields[2])
|
||||
t, err = time.Parse("Jan 2 15:04:05", timeStr)
|
||||
if err == nil {
|
||||
// 设置当前年份
|
||||
year, _, _ := time.Now().Date()
|
||||
t = time.Date(year, t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
|
||||
// 找到时间戳结束位置:找到第三个字段后的第一个空格
|
||||
pos := 0
|
||||
for i := 0; i < 3; i++ {
|
||||
// 找到下一个字段的开始位置
|
||||
pos = strings.Index(lineStr[pos:], fields[i]) + len(fields[i])
|
||||
if pos >= len(lineStr) {
|
||||
break
|
||||
}
|
||||
}
|
||||
// 跳过后面的空格
|
||||
for pos < len(lineStr) && lineStr[pos] == ' ' {
|
||||
pos++
|
||||
}
|
||||
timestampEndIndex = pos
|
||||
}
|
||||
}
|
||||
|
||||
// 构建时间字符串:月份 日期 时间
|
||||
timeStr := fmt.Sprintf("%s %s %s", fields[0], fields[1], fields[2])
|
||||
|
||||
// 解析时间
|
||||
t, err := time.Parse("Jan 2 15:04:05", timeStr)
|
||||
// 格式2: ISO 8601格式(如dnf.log文件)
|
||||
if err != nil {
|
||||
// 查找第一个空格的位置
|
||||
spaceIndex := strings.Index(lineStr, " ")
|
||||
if spaceIndex > 0 {
|
||||
timestampStr := lineStr[:spaceIndex]
|
||||
t, err = time.Parse(time.RFC3339, timestampStr)
|
||||
if err != nil {
|
||||
// 尝试另一种ISO 8601格式(不含时区)
|
||||
t, err = time.Parse("2006-01-02T15:04:05", timestampStr)
|
||||
if err != nil {
|
||||
// 尝试带时区的另一种格式
|
||||
t, err = time.Parse("2006-01-02T15:04:05Z07:00", timestampStr)
|
||||
if err != nil {
|
||||
// 尝试DNF日志格式(带时区但没有冒号)
|
||||
t, err = time.Parse("2006-01-02T15:04:05Z0700", timestampStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
timestampEndIndex = spaceIndex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果所有格式都解析失败,跳过该日志行
|
||||
if err != nil {
|
||||
// 尝试其他时间格式
|
||||
continue
|
||||
}
|
||||
|
||||
// 设置当前年份
|
||||
year, _, _ := time.Now().Date()
|
||||
t = time.Date(year, t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local)
|
||||
// 解析source和message
|
||||
var source string
|
||||
var message string
|
||||
|
||||
// 寻找第一个冒号,用于分割source和message
|
||||
colonIndex := strings.Index(lineStr, ": ")
|
||||
if colonIndex == -1 {
|
||||
continue
|
||||
if timestampEndIndex > 0 {
|
||||
// 跳过时间戳部分
|
||||
afterTimestamp := strings.TrimSpace(lineStr[timestampEndIndex:])
|
||||
afterFields := strings.Fields(afterTimestamp)
|
||||
|
||||
// 寻找冒号分隔符
|
||||
colonIndex := strings.Index(afterTimestamp, ":")
|
||||
|
||||
if colonIndex > 0 {
|
||||
// 如果找到冒号,使用冒号前的部分作为source
|
||||
sourcePart := strings.TrimSpace(afterTimestamp[:colonIndex])
|
||||
source = fmt.Sprintf("%s %s", filepath.Base(logFile), sourcePart)
|
||||
// 冒号后的部分作为message
|
||||
message = strings.TrimSpace(afterTimestamp[colonIndex+1:])
|
||||
} else if len(afterFields) > 0 {
|
||||
// 如果没有冒号,使用第一个字段作为source
|
||||
source = fmt.Sprintf("%s %s", filepath.Base(logFile), afterFields[0])
|
||||
// 剩余部分作为message
|
||||
if len(afterFields) > 1 {
|
||||
message = strings.Join(afterFields[1:], " ")
|
||||
} else {
|
||||
message = afterTimestamp
|
||||
}
|
||||
} else {
|
||||
// 简单处理
|
||||
source = filepath.Base(logFile)
|
||||
message = afterTimestamp
|
||||
}
|
||||
} else {
|
||||
// 简单处理,使用文件名作为source,整行作为message
|
||||
source = filepath.Base(logFile)
|
||||
message = lineStr
|
||||
}
|
||||
|
||||
// 解析source和message,添加文件名作为source的一部分
|
||||
source := fmt.Sprintf("%s %s", filepath.Base(logFile), lineStr[:colonIndex])
|
||||
message := lineStr[colonIndex+2:]
|
||||
|
||||
// 创建日志条目
|
||||
logEntry := LogEntry{
|
||||
Sequence: len(logs) + 1,
|
||||
|
||||
Reference in New Issue
Block a user