Skip to content

Commit 0ae8be1

Browse files
committed
feat(logging): Capture imported library logs and redirect to zerolog
Signed-off-by: spbsoluble <1661003+spbsoluble@users.noreply.github.com>
1 parent d4381eb commit 0ae8be1

File tree

7 files changed

+74
-18
lines changed

7 files changed

+74
-18
lines changed

cmd/helpers.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ import (
3131
"github.com/rs/zerolog"
3232
"github.com/rs/zerolog/log"
3333
"github.com/spf13/cobra"
34-
35-
stdlog "log"
34+
//stdlog "log"
3635
)
3736

3837
func boolToPointer(b bool) *bool {
@@ -191,12 +190,23 @@ func informDebug(debugFlag bool) {
191190
}
192191

193192
func initLogger() {
194-
stdlog.SetOutput(io.Discard)
195-
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
196-
zerolog.SetGlobalLevel(zerolog.Disabled) // default to disabled
197-
log.Logger = log.With().Caller().Logger()
193+
// Configure zerolog to include caller information
194+
log.Logger = log.With().Caller().Logger().Output(
195+
zerolog.ConsoleWriter{
196+
Out: os.Stdout,
197+
TimeFormat: time.RFC3339,
198+
FormatCaller: func(caller interface{}) string {
199+
if c, ok := caller.(string); ok {
200+
return c // This will include the full file path and line number
201+
}
202+
return ""
203+
},
204+
},
205+
)
198206
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout, TimeFormat: time.RFC3339})
199207

208+
initStdLogger()
209+
200210
}
201211

202212
func intToPointer(i int) *int {

cmd/logging.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
6+
"github.com/rs/zerolog/log"
7+
)
8+
9+
// zerologWriter implements io.Writer and forwards standard log output to zerolog
10+
type zerologWriter struct{}
11+
12+
func (w zerologWriter) Write(p []byte) (n int, err error) {
13+
// Clean up the log message (remove timestamp, etc.)
14+
msg := string(p)
15+
msg = strings.TrimSpace(msg)
16+
17+
// Check if it's a debug message
18+
if strings.Contains(msg, "[DEBUG]") {
19+
msg = strings.Replace(msg, "[DEBUG]", "", 1)
20+
log.Debug().Msg(strings.TrimSpace(msg))
21+
} else if strings.Contains(msg, "[ERROR]") {
22+
msg = strings.Replace(msg, "[ERROR]", "", 1)
23+
log.Error().Msg(strings.TrimSpace(msg))
24+
} else if strings.Contains(msg, "[INFO]") {
25+
msg = strings.Replace(msg, "[INFO]", "", 1)
26+
log.Info().Msg(strings.TrimSpace(msg))
27+
28+
} else if strings.Contains(msg, "[WARN]") {
29+
msg = strings.Replace(msg, "[WARN]", "", 1)
30+
log.Warn().Msg(strings.TrimSpace(msg))
31+
32+
} else if strings.Contains(msg, "[FATAL]") {
33+
msg = strings.Replace(msg, "[FATAL]", "", 1)
34+
log.Fatal().Msg(strings.TrimSpace(msg))
35+
36+
} else if strings.Contains(msg, "[TRACE]") {
37+
msg = strings.Replace(msg, "[TRACE]", "", 1)
38+
log.Debug().Msg(strings.TrimSpace(msg)) // Converting TRACE to DEBUG for zerolog
39+
} else {
40+
// Default to info level
41+
log.Info().Msg(msg)
42+
}
43+
44+
return len(p), nil
45+
}

cmd/login.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ package cmd
1717
import (
1818
"bufio"
1919
"fmt"
20-
"io"
21-
stdlog "log"
2220
"os"
2321
"path"
2422
"strings"
@@ -70,7 +68,7 @@ WARNING: This will write the environmental credentials to disk and will be store
7068
if debugErr != nil {
7169
return debugErr
7270
}
73-
stdlog.SetOutput(io.Discard)
71+
//stdlog.SetOutput(io.Discard)
7472
informDebug(debugFlag)
7573
logGlobals()
7674

cmd/logout.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ package cmd
1616

1717
import (
1818
"fmt"
19-
"io"
20-
stdlog "log"
2119
"os"
2220

2321
"github.com/Keyfactor/keyfactor-auth-client-go/auth_providers"
@@ -39,7 +37,7 @@ var logoutCmd = &cobra.Command{
3937
if debugErr != nil {
4038
return debugErr
4139
}
42-
stdlog.SetOutput(io.Discard)
40+
//stdlog.SetOutput(io.Discard)
4341
informDebug(debugFlag)
4442

4543
logGlobals()

cmd/root.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ package cmd
1717
import (
1818
_ "embed"
1919
"fmt"
20-
"io"
2120
stdlog "log"
2221
"os"
2322
"strings"
@@ -738,7 +737,7 @@ var RootCmd = &cobra.Command{
738737
// Execute adds all child commands to the root command and sets flags appropriately.
739738
// This is called by main.main(). It only needs to happen once to the rootCmd.
740739
func Execute() {
741-
stdlog.SetOutput(io.Discard)
740+
//stdlog.SetOutput(io.Discard)
742741
err := RootCmd.Execute()
743742
if err != nil {
744743
os.Exit(1)
@@ -881,3 +880,9 @@ func init() {
881880

882881
RootCmd.AddCommand(makeDocsCmd)
883882
}
883+
884+
func initStdLogger() {
885+
// Redirect standard library's log to zerolog
886+
stdlog.SetOutput(zerologWriter{})
887+
stdlog.SetFlags(0) // Remove timestamp from standard logger
888+
}

cmd/rot.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ kfutil stores rot reconcile --import-csv <audit-file>
631631
if debugErr != nil {
632632
return debugErr
633633
}
634-
stdlog.SetOutput(io.Discard)
634+
//stdlog.SetOutput(io.Discard)
635635
informDebug(debugFlag)
636636

637637
var lookupFailures []string
@@ -880,7 +880,7 @@ the utility will first generate an audit report and then execute the add/remove
880880
if debugErr != nil {
881881
return debugErr
882882
}
883-
stdlog.SetOutput(io.Discard)
883+
//stdlog.SetOutput(io.Discard)
884884
informDebug(debugFlag)
885885

886886
var lookupFailures []string
@@ -1415,7 +1415,7 @@ the utility will first generate an audit report and then execute the add/remove
14151415
if debugErr != nil {
14161416
return debugErr
14171417
}
1418-
stdlog.SetOutput(io.Discard)
1418+
//stdlog.SetOutput(io.Discard)
14191419
informDebug(debugFlag)
14201420

14211421
// Authenticate

cmd/storeTypes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var storesTypesListCmd = &cobra.Command{
5555
if debugErr != nil {
5656
return debugErr
5757
}
58-
stdlog.SetOutput(io.Discard)
58+
//stdlog.SetOutput(io.Discard)
5959
informDebug(debugFlag)
6060

6161
// Authenticate

0 commit comments

Comments
 (0)