Skip to content

Commit

Permalink
feat: --logs to file
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Dec 14, 2024
1 parent 95467e8 commit a9b298a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ tmp/
.DS_Store
go-watch-logs
*.sqlite
*.sqlite3
*.sqlite3
logs/
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ require (
github.com/gravwell/gravwell/v3 v3.8.34
github.com/jasonlvhit/gocron v0.0.1
github.com/kevincobain2000/go-msteams v1.1.1
github.com/lmittmann/tint v1.0.5
github.com/mattn/go-isatty v0.0.20
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/stretchr/testify v1.9.0
)

require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fatih/color v1.16.0 // indirect
Expand All @@ -23,6 +24,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/sys v0.23.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
modernc.org/libc v1.57.0 // indirect
modernc.org/mathutil v1.6.0 // indirect
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/MatusOllah/slogcolor v1.4.0 h1:NW4xI8BdBOY6Lt14004OInbbr0p+NURAMg15jzOjKds=
github.com/MatusOllah/slogcolor v1.4.0/go.mod h1:5y1H50XuQIBvuYTJlmokWi+4FuPiJN5L7Z0jM4K4bYA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -23,13 +25,13 @@ github.com/jasonlvhit/gocron v0.0.1 h1:qTt5qF3b3srDjeOIR4Le1LfeyvoYzJlYpqvG7tJX5
github.com/jasonlvhit/gocron v0.0.1/go.mod h1:k9a3TV8VcU73XZxfVHCHWMWF9SOqgoku0/QlY2yvlA4=
github.com/kevincobain2000/go-msteams v1.1.1 h1:vZ8AYvVmiCdC+VZwsw7RFhb89RG/GasX9kvbdKheFN4=
github.com/kevincobain2000/go-msteams v1.1.1/go.mod h1:+HowoQQHg9HLfx3CYQGImGGYw20+kN9rFmUXgxrqBzo=
github.com/lmittmann/tint v1.0.5 h1:NQclAutOfYsqs2F1Lenue6OoWCajs5wJcP3DfWVpePw=
github.com/lmittmann/tint v1.0.5/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
Expand Down Expand Up @@ -65,8 +67,11 @@ golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ var filePathsMutex sync.Mutex

func main() {
flags()
pkg.SetupLoggingStdout(f.LogLevel)
pkg.SetupLoggingStdout(f.LogLevel, f.Log)
flag.VisitAll(func(f *flag.Flag) {
slog.Info(f.Name, slog.String("value", f.Value.String()))
})
parseProxy()
wantsVersion()
validate()
Expand Down Expand Up @@ -220,9 +223,10 @@ func notify(result *pkg.ScanResult) {
}

func flags() {
flag.StringVar(&f.FilePath, "file-path", "", "full path to the log file")
flag.StringVar(&f.FilePath, "f", "", "(short for --file-path) full path to the log file")
flag.StringVar(&f.DBPath, "db-path", pkg.GetHomedir()+"/.go-watch-logs.db", "path to store db file")
flag.StringVar(&f.FilePath, "file-path", "", "full path to the file to watch")
flag.StringVar(&f.FilePath, "f", "", "(short for --file-path) full path to the file to watch")
flag.StringVar(&f.Log, "log", "", "full path to output log file")
flag.StringVar(&f.DBPath, "db-path", pkg.GetHomedir()+"/.go-watch-logs.db", "path to store db file. Note dir must exist prior")
flag.StringVar(&f.Match, "match", "", "regex for matching errors (empty to match all lines)")
flag.StringVar(&f.Ignore, "ignore", "", "regex for ignoring errors (empty to ignore none)")
flag.StringVar(&f.PostAlways, "post-always", "", "run this shell command after every scan")
Expand Down
15 changes: 15 additions & 0 deletions pkg/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package pkg

import (
"database/sql"
"log/slog"
"os"
"time"

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

func InitDB(dbName string) (*sql.DB, error) {
slog.Info("Initializing database", "dbName", dbName)
db, err := sql.Open("sqlite", dbName)
if err != nil {
slog.Error("Error opening database", "error", err.Error())
return nil, err
}

Expand All @@ -21,6 +24,18 @@ func InitDB(dbName string) (*sql.DB, error) {
)
`)
if err != nil {
slog.Error("Error creating state table", "error", err.Error())
return nil, err
}
_, err = db.Exec(`
CREATE TABLE IF NOT EXISTS plot (
key TEXT PRIMARY KEY,
value INTEGER
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`)
if err != nil {
slog.Error("Error creating plot table", "error", err.Error())
return nil, err
}
db.SetMaxOpenConns(5)
Expand Down
1 change: 1 addition & 0 deletions pkg/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Flags struct {
DBPath string
PostAlways string
PostMin string
Log string

Min int
Every uint64
Expand Down
18 changes: 16 additions & 2 deletions pkg/log.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
package pkg

import (
"fmt"
"log/slog"
"os"

"github.com/MatusOllah/slogcolor"
"github.com/mattn/go-isatty"
"github.com/natefinch/lumberjack"
)

func SetupLoggingStdout(logLevel int) {
func SetupLoggingStdout(logLevel int, logFile string) error {
opts := &slogcolor.Options{
Level: slog.Level(logLevel),
TimeFormat: "2006-01-02 15:04:05",
NoColor: !isatty.IsTerminal(os.Stderr.Fd()),
SrcFileMode: slogcolor.ShortFile,
}
slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts)))
if logFile == "" {
slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts)))
}
slog.SetDefault(slog.New(slogcolor.NewHandler(&lumberjack.Logger{
Filename: logFile,
MaxSize: 1, // megabytes
MaxBackups: 3,
MaxAge: 3, // days
LocalTime: true,
Compress: true,
}, opts)))
fmt.Println("logging to file", logFile)
return nil
}

0 comments on commit a9b298a

Please sign in to comment.