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

add global --gcplog flag to emit GCP-compatible JSON logs #1186

Merged
merged 3 commits into from
May 2, 2024
Merged
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
16 changes: 12 additions & 4 deletions pkg/cli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"net/http"

"chainguard.dev/apko/pkg/log"
"github.com/chainguard-dev/clog/gcp"
charmlog "github.com/charmbracelet/log"
"github.com/spf13/cobra"
"sigs.k8s.io/release-utils/version"
Expand All @@ -28,6 +29,7 @@ import (
func New() *cobra.Command {
var logPolicy []string
var level log.CharmLogLevel
var gcplog bool
cmd := &cobra.Command{
Use: "melange",
DisableAutoGenTag: true,
Expand All @@ -36,17 +38,23 @@ func New() *cobra.Command {
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
http.DefaultTransport = userAgentTransport{http.DefaultTransport}

out, err := log.Writer(logPolicy)
if err != nil {
return fmt.Errorf("failed to create log writer: %w", err)
if gcplog {
slog.SetDefault(slog.New(gcp.NewHandler(slog.Level(level))))
} else {
out, err := log.Writer(logPolicy)
if err != nil {
return fmt.Errorf("failed to create log writer: %w", err)
}
slog.SetDefault(slog.New(charmlog.NewWithOptions(out, charmlog.Options{ReportTimestamp: true, Level: charmlog.Level(level)})))
}
slog.SetDefault(slog.New(charmlog.NewWithOptions(out, charmlog.Options{ReportTimestamp: true, Level: charmlog.Level(level)})))

return nil
},
}
cmd.PersistentFlags().StringSliceVar(&logPolicy, "log-policy", []string{"builtin:stderr"}, "log policy (e.g. builtin:stderr, /tmp/log/foo)")
cmd.PersistentFlags().Var(&level, "log-level", "log level (e.g. debug, info, warn, error)")
cmd.PersistentFlags().BoolVar(&gcplog, "gcplog", false, "use GCP logging")
_ = cmd.PersistentFlags().MarkHidden("gcplog")

cmd.AddCommand(Build())
cmd.AddCommand(Bump())
Expand Down
Loading