65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package models
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// LogEntry representa uma linha de log com metadados extraídos
|
|
type LogEntry struct {
|
|
Filename string `json:"filename"`
|
|
Line string `json:"line"`
|
|
Timestamp time.Time `json:"timestamp,omitempty"`
|
|
Level string `json:"level,omitempty"`
|
|
}
|
|
|
|
var (
|
|
// Regex para extrair timestamps em diferentes formatos
|
|
timestampRegex1 = regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}`)
|
|
timestampRegex2 = regexp.MustCompile(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}`)
|
|
|
|
// Regex para extrair nível de log
|
|
logLevelRegex = regexp.MustCompile(`(?i)\b(DEBUG|INFO|WARN|WARNING|ERROR|FATAL|TRACE)\b`)
|
|
)
|
|
|
|
// NewLogEntry cria uma nova entrada de log com parsing automático de timestamp e nível
|
|
func NewLogEntry(filename, line string) LogEntry {
|
|
entry := LogEntry{
|
|
Filename: filename,
|
|
Line: strings.TrimRight(line, "\r\n"),
|
|
}
|
|
|
|
// Extrair timestamp
|
|
if ts := timestampRegex1.FindString(line); ts != "" {
|
|
if parsed, err := time.Parse("2006-01-02T15:04:05", ts); err == nil {
|
|
entry.Timestamp = parsed
|
|
}
|
|
} else if ts := timestampRegex2.FindString(line); ts != "" {
|
|
if parsed, err := time.Parse("2006-01-02 15:04:05", ts); err == nil {
|
|
entry.Timestamp = parsed
|
|
}
|
|
}
|
|
|
|
// Extrair nível de log
|
|
if level := logLevelRegex.FindString(line); level != "" {
|
|
entry.Level = strings.ToUpper(level)
|
|
}
|
|
|
|
return entry
|
|
}
|
|
|
|
// TimestampString retorna o timestamp como string no formato original
|
|
// Retorna string vazia se não houver timestamp
|
|
func (e LogEntry) TimestampString() string {
|
|
if e.Timestamp.IsZero() {
|
|
return ""
|
|
}
|
|
|
|
// Tentar detectar o formato original da linha
|
|
if strings.Contains(e.Line, "T") {
|
|
return e.Timestamp.Format("2006-01-02T15:04:05")
|
|
}
|
|
return e.Timestamp.Format("2006-01-02 15:04:05")
|
|
}
|