Skip to content

Commit

Permalink
Merge pull request #167 from ekristen/feat-structured-loggin
Browse files Browse the repository at this point in the history
feat: structured logging
  • Loading branch information
ekristen authored Jan 2, 2025
2 parents 9178f7a + 1cfd11d commit 82d7118
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
53 changes: 52 additions & 1 deletion pkg/commands/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path"
"runtime"

"github.com/ekristen/libnuke/pkg/log"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
Expand All @@ -30,6 +31,17 @@ func Flags() []cli.Flag {
Name: "log-full-timestamp",
Usage: "force log output to always show full timestamp",
},
&cli.StringFlag{
Name: "log-format",
Usage: "log format",
Value: "standard",
EnvVars: []string{"GCP_NUKE_LOG_FORMAT"},
},
&cli.BoolFlag{
Name: "json",
Usage: "output as json, shorthand for --log-format=json",
EnvVars: []string{"GCP_NUKE_LOG_FORMAT_JSON"},
},
}

return globalFlags
Expand All @@ -48,7 +60,29 @@ func Before(c *cli.Context) error {
}
}

logrus.SetFormatter(formatter)
logFormatter := &log.CustomFormatter{
FallbackFormatter: formatter,
}

if c.Bool("json") {
_ = c.Set("log-format", "json")
}

switch c.String("log-format") {
case "json":
logrus.SetFormatter(&logrus.JSONFormatter{
DisableHTMLEscape: true,
})
// note: this is a hack to remove the _handler key from the log output
logrus.AddHook(&StructuredHook{})
case "kv":
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
default:
logrus.SetFormatter(logFormatter)
}

switch c.String("log-level") {
case "trace":
Expand All @@ -65,3 +99,20 @@ func Before(c *cli.Context) error {

return nil
}

type StructuredHook struct {
}

func (h *StructuredHook) Levels() []logrus.Level {
return logrus.AllLevels
}

func (h *StructuredHook) Fire(e *logrus.Entry) error {
if e.Data == nil {
return nil
}

delete(e.Data, "_handler")

return nil
}
26 changes: 17 additions & 9 deletions pkg/commands/run/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ func execute(c *cli.Context) error {
return fmt.Errorf("no projects found")
}

logrus.Trace("preparing to run nuke")
logger := logrus.StandardLogger()

logger.Trace("preparing to run nuke")

params := &libnuke.Parameters{
Force: c.Bool("no-prompt"),
Expand All @@ -50,8 +52,10 @@ func execute(c *cli.Context) error {
parsedConfig, err := libconfig.New(libconfig.Options{
Path: c.Path("config"),
Deprecations: registry.GetDeprecatedResourceTypeMapping(),
Log: logger.WithField("component", "config"),
})
if err != nil {
logger.Errorf("Failed to parse config file %s", c.Path("config"))
return err
}

Expand All @@ -67,6 +71,7 @@ func execute(c *cli.Context) error {
n := libnuke.New(params, filters, parsedConfig.Settings)

n.SetRunSleep(5 * time.Second)
n.SetLogger(logger.WithField("component", "libnuke"))
n.RegisterVersion(fmt.Sprintf("> %s", common.AppVersion.String()))

p := &nuke.Prompt{Parameters: params, GCP: gcp}
Expand Down Expand Up @@ -101,41 +106,44 @@ func execute(c *cli.Context) error {
if slices.Contains(parsedConfig.Regions, "all") {
parsedConfig.Regions = gcp.Regions

logrus.Info(
logger.Info(
`"all" detected in region list, only enabled regions and "global" will be used, all others ignored`)

if len(parsedConfig.Regions) > 1 {
logrus.Warnf(`additional regions defined along with "all", these will be ignored!`)
logger.Warnf(`additional regions defined along with "all", these will be ignored!`)
}

logrus.Infof("The following regions are enabled for the account (%d total):", len(parsedConfig.Regions))
logger.Infof("The following regions are enabled for the account (%d total):", len(parsedConfig.Regions))

printableRegions := make([]string, 0)
for i, region := range parsedConfig.Regions {
printableRegions = append(printableRegions, region)
if i%6 == 0 { // print 5 regions per line
logrus.Infof("> %s", strings.Join(printableRegions, ", "))
logger.Infof("> %s", strings.Join(printableRegions, ", "))
printableRegions = make([]string, 0)
} else if i == len(parsedConfig.Regions)-1 {
logrus.Infof("> %s", strings.Join(printableRegions, ", "))
logger.Infof("> %s", strings.Join(printableRegions, ", "))
}
}
}

// Register the scanners for each region that is defined in the configuration.
for _, regionName := range parsedConfig.Regions {
if err := n.RegisterScanner(nuke.Project, scanner.New(regionName, projectResourceTypes, &nuke.ListerOpts{
scannerActual := scanner.New(regionName, projectResourceTypes, &nuke.ListerOpts{
Project: ptr.String(projectID),
Region: ptr.String(regionName),
Zones: gcp.GetZones(regionName),
EnabledAPIs: gcp.GetEnabledAPIs(),
ClientOptions: gcp.GetClientOptions(),
})); err != nil {
})
scannerActual.SetLogger(logger)

if err := n.RegisterScanner(nuke.Project, scannerActual); err != nil {
return err
}
}

logrus.Debug("running ...")
logger.Debug("running ...")

return n.Run(c.Context)
}
Expand Down

0 comments on commit 82d7118

Please sign in to comment.