Skip to content

Commit e97839d

Browse files
refact: fn
1 parent c643d92 commit e97839d

File tree

5 files changed

+40
-29
lines changed

5 files changed

+40
-29
lines changed

Diff for: README.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -57,39 +57,43 @@ go-watch-logs --file-path=my.log --match='HTTP/1.1" 50' --every=60
5757
## Help
5858

5959
```sh
60-
-db-path string
61-
path to store db file (default "/Users/pulkit.kathuria/.go-watch-logs.db")
6260
-every uint
6361
run every n seconds (0 to run once)
6462
-f string
65-
(short for --file-path) full path to the log file
63+
(short for --file-path) full path to the file to watch
6664
-file-path string
67-
full path to the log file
65+
full path to the file to watch
6866
-file-paths-cap int
6967
max number of file paths to watch (default 100)
70-
-health-check-every uint
71-
run health check every n seconds (0 to disable)
68+
-file-recent-secs uint
69+
only files modified in the last n seconds, 0 to disable (default 86400)
7270
-ignore string
7371
regex for ignoring errors (empty to ignore none)
72+
-log-file string
73+
full path to output log file. Empty will log to stdout
7474
-log-level int
7575
log level (0=info, -4=debug, 4=warn, 8=error)
7676
-match string
7777
regex for matching errors (empty to match all lines)
78+
-mbf int
79+
max buffer in MB, default is 0 (not provided) for go's default 64KB
7880
-mem-limit int
79-
memory limit in MB (0 to disable) (default 100)
81+
memory limit in MB (0 to disable) (default 128)
8082
-min int
8183
on minimum num of matches, it should notify (default 1)
8284
-ms-teams-hook string
8385
ms teams webhook
84-
-post-min string
86+
-post-cmd string
8587
run this shell command after every scan when min errors are found
8688
-proxy string
8789
http proxy for webhooks
90+
-streak int
91+
on minimum num of streak matches, it should notify (default 1)
8892
-test
8993
Quickly test paths or regex
90-
# will test if the input matches the regex
94+
# will test if the input matches the regex
9195
echo test123 | go-watch-logs --match=123 --test
92-
# will test if the file paths are found and list them
96+
# will test if the file paths are found and list them
9397
go-watch-logs --file-path=./ssl_access.*log --test
9498
9599
-version

Diff for: main.go

+18-17
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func cronWatch() {
9999

100100
func syncFilePaths() {
101101
slog.Info("Syncing files")
102-
var err error
102+
103103
fpCrawled, err := pkg.FilesByPattern(f.FilePath, f.FileRecentSecs)
104104
if err != nil {
105105
slog.Error("Error finding files", "error", err.Error())
@@ -111,24 +111,25 @@ func syncFilePaths() {
111111
return
112112
}
113113

114+
// Filter and cap file paths
114115
filePathsMutex.Lock()
115-
fpCrawled = pkg.Capped(f.FilePathsCap, fpCrawled)
116+
defer filePathsMutex.Unlock()
116117

117-
fpFiltered := make([]string, 0, len(fpCrawled))
118+
filePaths = filterTextFiles(pkg.Capped(f.FilePathsCap, fpCrawled))
118119

119-
for _, filePath := range fpCrawled {
120-
isText, err := pkg.IsTextFile(filePath)
121-
if err != nil || !isText {
122-
continue
120+
syncCaches()
121+
slog.Info("Files synced", "fileCount", len(filePaths), "cacheCount", len(caches))
122+
}
123+
124+
// filterTextFiles filters file paths to include only text files.
125+
func filterTextFiles(paths []string) []string {
126+
filtered := make([]string, 0, len(paths))
127+
for _, path := range paths {
128+
if isText, err := pkg.IsTextFile(path); err == nil && isText {
129+
filtered = append(filtered, path)
123130
}
124-
fpFiltered = append(fpFiltered, filePath)
125131
}
126-
filePaths = fpFiltered
127-
128-
filePathsMutex.Unlock()
129-
syncCaches()
130-
slog.Info("Files found", "count", len(filePaths))
131-
slog.Info("Caches set", "count", len(caches))
132+
return filtered
132133
}
133134

134135
func validate() {
@@ -137,7 +138,7 @@ func validate() {
137138
}
138139
if f.FilePath == "" {
139140
slog.Error("file-path is required")
140-
os.Exit(1)
141+
return
141142
}
142143
}
143144

@@ -174,8 +175,8 @@ func reportResult(result *pkg.ScanResult) {
174175
slog.Info("History", "max streak", f.Streak, "current streaks", result.Streak, "symbols", pkg.StreakSymbols(result.Streak, f.Streak, f.Min))
175176
slog.Info("Scan", "count", result.ScanCount)
176177

177-
// is first scan, cache isn't ready, so skip the notification
178-
if result.ScanCount == 1 {
178+
if result.IsFirstScan() {
179+
slog.Info("First scan, skipping notification")
179180
return
180181
}
181182

Diff for: pkg/log.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
const (
1515
AppLogLevelDebug = -4
16+
SlogErrorLabel = "ERROR"
1617
)
1718

1819
// GlobalHandler is a custom handler that catches all logs
@@ -23,7 +24,7 @@ type GlobalHandler struct {
2324
}
2425

2526
func (h *GlobalHandler) Handle(ctx context.Context, r slog.Record) error {
26-
if r.Level.String() == "ERROR" {
27+
if r.Level.String() == SlogErrorLabel {
2728
err := fmt.Errorf("global log capture - Level: %s, Message: %s", r.Level.String(), r.Message)
2829
NotifyOwnError(err, r, h.msTeamsHook, h.proxy)
2930
}

Diff for: pkg/notify.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
func NotifyOwnError(e error, r slog.Record, msTeamsHook, proxy string) {
13+
slog.Info("Sending own error to MS Teams")
1314
hostname, _ := os.Hostname()
1415
details := []gmt.Details{
1516
{
@@ -41,7 +42,7 @@ func NotifyOwnError(e error, r slog.Record, msTeamsHook, proxy string) {
4142
}
4243

4344
func Notify(result *ScanResult, f Flags, version string) {
44-
slog.Info("Sending to MS Teams")
45+
slog.Info("Sending scan results to MS Teams")
4546
details := []gmt.Details{
4647
{
4748
Label: "go-watch-log version",

Diff for: pkg/watcher.go

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ type ScanResult struct {
7272
ScanCount int // Total number of scans performed
7373
}
7474

75+
func (r *ScanResult) IsFirstScan() bool {
76+
return r.ScanCount == 1
77+
}
78+
7579
func (w *Watcher) Scan() (*ScanResult, error) {
7680
matchCounts := 0
7781
firstLine := ""

0 commit comments

Comments
 (0)