From fad63a27b1d511d14190b42f7b78c695b5e95b4f Mon Sep 17 00:00:00 2001 From: Bohdan Dymchenko <142878290+bohdand-weka@users.noreply.github.com> Date: Wed, 17 Jan 2024 13:19:11 +0200 Subject: [PATCH] feat: add debug file logger (WH-1724) (#45) --- go.mod | 8 ++++---- go.sum | 18 +++++++++++------- internal/cli/local/local.go | 2 +- internal/env/env.go | 2 +- internal/local/k3s/k3s.go | 2 +- internal/utils/logging.go | 38 +++++++++++++++++++++++++++++++------ 6 files changed, 50 insertions(+), 20 deletions(-) diff --git a/go.mod b/go.mod index 6e45114..906c54e 100644 --- a/go.mod +++ b/go.mod @@ -9,9 +9,10 @@ require ( github.com/mittwald/go-helm-client v0.12.4 github.com/olekukonko/tablewriter v0.0.5 github.com/pelletier/go-toml v1.9.5 - github.com/rs/zerolog v1.20.0 + github.com/rs/zerolog v1.31.0 github.com/spf13/cobra v1.8.0 github.com/stretchr/testify v1.8.1 + golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa golang.org/x/mod v0.14.0 golang.org/x/sync v0.5.0 gopkg.in/yaml.v3 v3.0.1 @@ -73,7 +74,7 @@ require ( github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.17 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect github.com/mitchellh/copystructure v1.2.0 // indirect @@ -109,10 +110,9 @@ require ( github.com/xlab/treeprint v1.1.0 // indirect go.starlark.net v0.0.0-20221020143700-22309ac47eac // indirect golang.org/x/crypto v0.5.0 // indirect - golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect golang.org/x/net v0.8.0 // indirect golang.org/x/oauth2 v0.1.0 // indirect - golang.org/x/sys v0.14.0 // indirect + golang.org/x/sys v0.16.0 // indirect golang.org/x/term v0.6.0 // indirect golang.org/x/text v0.8.0 // indirect golang.org/x/time v0.1.0 // indirect diff --git a/go.sum b/go.sum index de75197..c91afce 100644 --- a/go.sum +++ b/go.sum @@ -120,6 +120,7 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= +github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= @@ -433,8 +434,10 @@ github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOA github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +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/mattn/go-oci8 v0.1.1/go.mod h1:wjDx6Xm9q7dFtHJvIlrI99JytznLw5wQ4R+9mNXJwGI= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= @@ -569,9 +572,9 @@ github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6po github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= -github.com/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= -github.com/rs/zerolog v1.20.0 h1:38k9hgtUBdxFwE34yS8rTHmHBa4eN16E4DJlv177LNs= -github.com/rs/zerolog v1.20.0/go.mod h1:IzD0RJ65iWH0w97OQQebJEvTZYvsCUm9WVLWBQrJRjo= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= +github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/rubenv/sql-migrate v1.3.1 h1:Vx+n4Du8X8VTYuXbhNxdEUoh6wiJERA0GlWocR5FrbA= github.com/rubenv/sql-migrate v1.3.1/go.mod h1:YzG/Vh82CwyhTFXy+Mf5ahAiiEOpAlHurg+23VEzcsk= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= @@ -878,8 +881,10 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -920,7 +925,6 @@ golang.org/x/tools v0.0.0-20190606124116-d0a3d012864b/go.mod h1:/rFqwRUd4F7ZHNgw golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190628153133-6cdbf07be9d0/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20190828213141-aed303cbaa74/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= diff --git a/internal/cli/local/local.go b/internal/cli/local/local.go index b2a8eb5..e666c86 100644 --- a/internal/cli/local/local.go +++ b/internal/cli/local/local.go @@ -26,7 +26,7 @@ var localCmd = &cobra.Command{ GroupID: "local", PersistentPreRun: func(cmd *cobra.Command, args []string) { if !env.VerboseLogging { - utils.SetGlobalLoggingLevel(utils.InfoLevel) + utils.SetLoggingLevel(utils.InfoLevel) } }, } diff --git a/internal/env/env.go b/internal/env/env.go index 6baa18c..8b65a1f 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -50,6 +50,6 @@ func InitEnv() { func initLogging() { if VerboseLogging { - utils.SetGlobalLoggingLevel(utils.DebugLevel) + utils.SetLoggingLevel(utils.DebugLevel) } } diff --git a/internal/local/k3s/k3s.go b/internal/local/k3s/k3s.go index d6b5d63..8cadd97 100644 --- a/internal/local/k3s/k3s.go +++ b/internal/local/k3s/k3s.go @@ -32,7 +32,7 @@ var logger = utils.GetLogger("K3S") func setupLogger(debug bool) { if debug { - utils.SetGlobalLoggingLevel(utils.DebugLevel) + utils.SetLoggingLevel(utils.DebugLevel) } } diff --git a/internal/utils/logging.go b/internal/utils/logging.go index ba5b87a..87e32f2 100644 --- a/internal/utils/logging.go +++ b/internal/utils/logging.go @@ -18,16 +18,42 @@ const ( WarnLevel = zerolog.WarnLevel ) +const debugLog = "/var/log/homecli.log" + +var stdoutWriter *zerolog.FilteredLevelWriter + func init() { + var logWriter zerolog.LevelWriter + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix - output := zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339} - log.Logger = zerolog.New(output).With().Timestamp().Logger() - SetGlobalLoggingLevel(WarnLevel) + + stdoutWriter = &zerolog.FilteredLevelWriter{ + Writer: zerolog.LevelWriterAdapter{ + Writer: zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}, + }, + Level: WarnLevel, + } + + logWriter = stdoutWriter + + f, err := os.OpenFile(debugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0660) + if err == nil { + debugWriter := zerolog.FilteredLevelWriter{ + Writer: zerolog.LevelWriterAdapter{Writer: f}, + Level: DebugLevel, + } + logWriter = zerolog.MultiLevelWriter(logWriter, &debugWriter) + } + + log.Logger = zerolog.New(logWriter).With().Timestamp().Logger() + zerolog.SetGlobalLevel(DebugLevel) + + SetLoggingLevel(WarnLevel) } -// SetGlobalLoggingLevel should be invoked once by each entry point -func SetGlobalLoggingLevel(level zerolog.Level) { - zerolog.SetGlobalLevel(level) +// SetLoggingLevel should be invoked once by each entry point +func SetLoggingLevel(level zerolog.Level) { + stdoutWriter.Level = level } // GetLogger returns a new logger instance with the specified component