diff --git a/go.mod b/go.mod index a3f1dd9..c1c9522 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/rakutentech/go-watch-logs go 1.22.3 require ( + github.com/MatusOllah/slogcolor v1.4.0 github.com/glebarez/go-sqlite v1.22.0 github.com/gravwell/gravwell/v3 v3.8.34 github.com/jasonlvhit/gocron v0.0.1 @@ -10,13 +11,14 @@ require ( github.com/lmittmann/tint v1.0.5 github.com/mattn/go-isatty v0.0.20 github.com/stretchr/testify v1.9.0 - gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) require ( 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 github.com/google/uuid v1.6.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect diff --git a/go.sum b/go.sum index e314013..8bb89ca 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,12 @@ +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= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/glebarez/go-sqlite v1.22.0 h1:uAcMJhaA6r3LHMTFgP0SifzgXg46yJkgxqyuyec+ruQ= github.com/glebarez/go-sqlite v1.22.0/go.mod h1:PlBIdHe0+aUEFn+r2/uthrWq4FxbzugL0L8Li6yQJbc= @@ -21,6 +25,9 @@ github.com/kevincobain2000/go-msteams v1.1.1 h1:vZ8AYvVmiCdC+VZwsw7RFhb89RG/GasX 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/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= @@ -48,6 +55,7 @@ golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= @@ -57,8 +65,6 @@ 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/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index ccbd582..e664cb1 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ func main() { } if len(newFilePaths) == 0 { slog.Error("No files found", "filePath", f.FilePath) - return + slog.Warn("Keep watching for new files") } if len(newFilePaths) > f.FilePathsCap { slog.Warn("Too many files found", "count", len(newFilePaths), "cap", f.FilePathsCap) @@ -105,6 +105,7 @@ func syncFilePaths() { } if len(newFilePaths) == 0 { slog.Error("No files found", "filePath", f.FilePath) + slog.Warn("Keep watching for new files") return } diff --git a/pkg/log.go b/pkg/log.go index aec3f9f..4acd4b1 100644 --- a/pkg/log.go +++ b/pkg/log.go @@ -1,35 +1,19 @@ package pkg import ( - "fmt" "log/slog" "os" - "path/filepath" - "time" - "github.com/lmittmann/tint" + "github.com/MatusOllah/slogcolor" "github.com/mattn/go-isatty" ) func SetupLoggingStdout(logLevel int) { - w := os.Stderr - handler := tint.NewHandler(w, &tint.Options{ - NoColor: !isatty.IsTerminal(w.Fd()), - AddSource: true, - Level: slog.Level(logLevel), // Set the logging level using the loglevel parameter - ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr { - if a.Key == slog.TimeKey { - t := a.Value.Time() - a.Value = slog.StringValue(t.Format(time.DateTime)) - } - // change file to short file with line number - if a.Key == slog.SourceKey { - source := a.Value.Any().(*slog.Source) - a.Value = slog.StringValue(filepath.Base(source.File) + ":" + fmt.Sprint(source.Line)) - } - return a - }, - }) - - slog.SetDefault(slog.New(handler)) + 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))) }