Skip to content

Commit a9b298a

Browse files
feat: --logs to file
1 parent 95467e8 commit a9b298a

File tree

7 files changed

+52
-10
lines changed

7 files changed

+52
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ tmp/
2424
.DS_Store
2525
go-watch-logs
2626
*.sqlite
27-
*.sqlite3
27+
*.sqlite3
28+
logs/

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ require (
88
github.com/gravwell/gravwell/v3 v3.8.34
99
github.com/jasonlvhit/gocron v0.0.1
1010
github.com/kevincobain2000/go-msteams v1.1.1
11-
github.com/lmittmann/tint v1.0.5
1211
github.com/mattn/go-isatty v0.0.20
12+
github.com/natefinch/lumberjack v2.0.0+incompatible
1313
github.com/stretchr/testify v1.9.0
1414
)
1515

1616
require (
17+
github.com/BurntSushi/toml v1.4.0 // indirect
1718
github.com/davecgh/go-spew v1.1.1 // indirect
1819
github.com/dustin/go-humanize v1.0.1 // indirect
1920
github.com/fatih/color v1.16.0 // indirect
@@ -23,6 +24,7 @@ require (
2324
github.com/pmezard/go-difflib v1.0.0 // indirect
2425
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
2526
golang.org/x/sys v0.23.0 // indirect
27+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
2628
gopkg.in/yaml.v3 v3.0.1 // indirect
2729
modernc.org/libc v1.57.0 // indirect
2830
modernc.org/mathutil v1.6.0 // indirect

go.sum

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
2+
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
13
github.com/MatusOllah/slogcolor v1.4.0 h1:NW4xI8BdBOY6Lt14004OInbbr0p+NURAMg15jzOjKds=
24
github.com/MatusOllah/slogcolor v1.4.0/go.mod h1:5y1H50XuQIBvuYTJlmokWi+4FuPiJN5L7Z0jM4K4bYA=
35
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -23,13 +25,13 @@ github.com/jasonlvhit/gocron v0.0.1 h1:qTt5qF3b3srDjeOIR4Le1LfeyvoYzJlYpqvG7tJX5
2325
github.com/jasonlvhit/gocron v0.0.1/go.mod h1:k9a3TV8VcU73XZxfVHCHWMWF9SOqgoku0/QlY2yvlA4=
2426
github.com/kevincobain2000/go-msteams v1.1.1 h1:vZ8AYvVmiCdC+VZwsw7RFhb89RG/GasX9kvbdKheFN4=
2527
github.com/kevincobain2000/go-msteams v1.1.1/go.mod h1:+HowoQQHg9HLfx3CYQGImGGYw20+kN9rFmUXgxrqBzo=
26-
github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw=
27-
github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
2828
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
2929
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
3030
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
3131
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
3232
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
33+
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
34+
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
3335
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
3436
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
3537
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
@@ -65,8 +67,11 @@ golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI
6567
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
6668
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
6769
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
70+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
71+
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
6872
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
6973
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
74+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
7075
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7176
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
7277
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ var filePathsMutex sync.Mutex
2222

2323
func main() {
2424
flags()
25-
pkg.SetupLoggingStdout(f.LogLevel)
25+
pkg.SetupLoggingStdout(f.LogLevel, f.Log)
26+
flag.VisitAll(func(f *flag.Flag) {
27+
slog.Info(f.Name, slog.String("value", f.Value.String()))
28+
})
2629
parseProxy()
2730
wantsVersion()
2831
validate()
@@ -220,9 +223,10 @@ func notify(result *pkg.ScanResult) {
220223
}
221224

222225
func flags() {
223-
flag.StringVar(&f.FilePath, "file-path", "", "full path to the log file")
224-
flag.StringVar(&f.FilePath, "f", "", "(short for --file-path) full path to the log file")
225-
flag.StringVar(&f.DBPath, "db-path", pkg.GetHomedir()+"/.go-watch-logs.db", "path to store db file")
226+
flag.StringVar(&f.FilePath, "file-path", "", "full path to the file to watch")
227+
flag.StringVar(&f.FilePath, "f", "", "(short for --file-path) full path to the file to watch")
228+
flag.StringVar(&f.Log, "log", "", "full path to output log file")
229+
flag.StringVar(&f.DBPath, "db-path", pkg.GetHomedir()+"/.go-watch-logs.db", "path to store db file. Note dir must exist prior")
226230
flag.StringVar(&f.Match, "match", "", "regex for matching errors (empty to match all lines)")
227231
flag.StringVar(&f.Ignore, "ignore", "", "regex for ignoring errors (empty to ignore none)")
228232
flag.StringVar(&f.PostAlways, "post-always", "", "run this shell command after every scan")

pkg/database.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ package pkg
22

33
import (
44
"database/sql"
5+
"log/slog"
56
"os"
67
"time"
78

89
_ "github.com/glebarez/go-sqlite" // nolint: revive
910
)
1011

1112
func InitDB(dbName string) (*sql.DB, error) {
13+
slog.Info("Initializing database", "dbName", dbName)
1214
db, err := sql.Open("sqlite", dbName)
1315
if err != nil {
16+
slog.Error("Error opening database", "error", err.Error())
1417
return nil, err
1518
}
1619

@@ -21,6 +24,18 @@ func InitDB(dbName string) (*sql.DB, error) {
2124
)
2225
`)
2326
if err != nil {
27+
slog.Error("Error creating state table", "error", err.Error())
28+
return nil, err
29+
}
30+
_, err = db.Exec(`
31+
CREATE TABLE IF NOT EXISTS plot (
32+
key TEXT PRIMARY KEY,
33+
value INTEGER
34+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
35+
)
36+
`)
37+
if err != nil {
38+
slog.Error("Error creating plot table", "error", err.Error())
2439
return nil, err
2540
}
2641
db.SetMaxOpenConns(5)

pkg/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Flags struct {
88
DBPath string
99
PostAlways string
1010
PostMin string
11+
Log string
1112

1213
Min int
1314
Every uint64

pkg/log.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
package pkg
22

33
import (
4+
"fmt"
45
"log/slog"
56
"os"
67

78
"github.com/MatusOllah/slogcolor"
89
"github.com/mattn/go-isatty"
10+
"github.com/natefinch/lumberjack"
911
)
1012

11-
func SetupLoggingStdout(logLevel int) {
13+
func SetupLoggingStdout(logLevel int, logFile string) error {
1214
opts := &slogcolor.Options{
1315
Level: slog.Level(logLevel),
1416
TimeFormat: "2006-01-02 15:04:05",
1517
NoColor: !isatty.IsTerminal(os.Stderr.Fd()),
1618
SrcFileMode: slogcolor.ShortFile,
1719
}
18-
slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts)))
20+
if logFile == "" {
21+
slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts)))
22+
}
23+
slog.SetDefault(slog.New(slogcolor.NewHandler(&lumberjack.Logger{
24+
Filename: logFile,
25+
MaxSize: 1, // megabytes
26+
MaxBackups: 3,
27+
MaxAge: 3, // days
28+
LocalTime: true,
29+
Compress: true,
30+
}, opts)))
31+
fmt.Println("logging to file", logFile)
32+
return nil
1933
}

0 commit comments

Comments
 (0)