Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Log with slog to stdout in JSON form #595

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"strings"

"github.com/jetstack/preflight/pkg/logs"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand All @@ -17,6 +18,9 @@ var rootCmd = &cobra.Command{
configuration checks using Open Policy Agent (OPA).

Preflight checks are bundled into Packages`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
logs.Initialize()
},
}

func init() {
Expand All @@ -28,6 +32,8 @@ func init() {
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
logs.AddFlags(rootCmd.PersistentFlags())

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
48 changes: 47 additions & 1 deletion pkg/logs/logs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
package logs

import (
"io"
"log"
"log/slog"
"os"

"github.com/spf13/pflag"
)

var Log = log.New(os.Stderr, "", log.LstdFlags)
// Deprecated: Log is a `log` logger, which is being phased out.
var Log = log.Default()

// Write logs to stdout by default
var logOutput io.Writer = os.Stdout

// SetOutput changes the global log output writer
func SetOutput(w io.Writer) {
logOutput = w
}

var logLevel int

// AddFlags adds log related flags to the supplied flag set
func AddFlags(fs *pflag.FlagSet) {
fs.IntVarP(&logLevel,
"log-level", "v", int(slog.LevelInfo),
"Set the logging verbosity. 8: >= ERROR, 4: >= WARN, 0: >=INFO, -4: >= DEBUG")
}

// Initialize configures the global log and slog loggers to write JSON to stdout.
//
// Errors, warnings, info and debug messages are all logged to stdout.
// By default, log messages with level >= INFO will be written.
// If verbose logging is enabled, log messages with level >= DEBUG will be written.
//
// The log module doesn't support levels. All its messages will be logged in
// JSON format, at INFO level.
func Initialize() {
// This is a work around to remove the `vcert: ` prefix which is added by the vcert module.
// See https://github.com/Venafi/vcert/pull/512
log.SetPrefix("")
slog.SetLogLoggerLevel(slog.LevelInfo)

level := slog.Level(logLevel)
logger := slog.New(
slog.NewJSONHandler(logOutput, &slog.HandlerOptions{
Level: level,
}),
)
// This sets both the global slog logger and the global legacy log logger.
slog.SetDefault(logger)
}