Skip to content

Commit

Permalink
add logging to base producers and consumers
Browse files Browse the repository at this point in the history
  • Loading branch information
northdpole committed Jun 3, 2024
1 parent 4a92fee commit 8d6d4c4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
5 changes: 3 additions & 2 deletions cmd/draconctl/main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package main

import (
"log/slog"
"os"

"github.com/spf13/cobra"

"github.com/ocurity/dracon/cmd/draconctl/components"
"github.com/ocurity/dracon/cmd/draconctl/migrations"
"github.com/ocurity/dracon/cmd/draconctl/pipelines"
draconLogger "github.com/ocurity/dracon/pkg/log"
)

var rootCmd = &cobra.Command{
Expand All @@ -20,11 +22,10 @@ func main() {
ID: "top-level",
Title: "Top-level Commands:",
})

pipelines.RegisterPipelinesSubcommands(rootCmd)
migrations.RegisterMigrationsSubcommands(rootCmd)
components.RegisterComponentsSubcommands(rootCmd)

draconLogger.SetDefault(slog.LevelInfo, "", false)
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand Down
13 changes: 13 additions & 0 deletions components/consumers/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ package consumers
import (
"flag"
"fmt"
"log/slog"
"os"

v1 "github.com/ocurity/dracon/api/proto/v1"

draconLogger "github.com/ocurity/dracon/pkg/log"
"github.com/ocurity/dracon/pkg/putil"
)

Expand All @@ -24,16 +27,26 @@ var (
inResults string
// Raw represents if the non-enriched results should be used.
Raw bool
// Debug flag initializes the logger with a debug level
Debug bool
)

func init() {
flag.StringVar(&inResults, "in", "", "the directory where dracon producer/enricher outputs are")
flag.BoolVar(&Raw, "raw", false, "if the non-enriched results should be used")
flag.BoolVar(&Debug, "debug", false, "turn on debug logging")

}

// ParseFlags will parse the input flags for the consumer and perform simple validation.
func ParseFlags() error {
flag.Parse()
if Debug {
draconLogger.SetDefault(slog.LevelDebug, os.Getenv(EnvDraconScanID), true)
} else {
draconLogger.SetDefault(slog.LevelInfo, os.Getenv(EnvDraconScanID), true)
}

if len(inResults) < 1 {
return fmt.Errorf("in is undefined")
}
Expand Down
10 changes: 10 additions & 0 deletions components/producers/producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"fmt"
"io"
"log"
"log/slog"
"os"
"path/filepath"
"strings"
"time"

v1 "github.com/ocurity/dracon/api/proto/v1"

draconLogger "github.com/ocurity/dracon/pkg/log"
"github.com/ocurity/dracon/pkg/putil"
)

Expand All @@ -28,6 +30,8 @@ var (
OutFile string
// Append flag will append to the outfile instead of overwriting, useful when there's multiple inresults.
Append bool
// Debug flag initializes the logger with a debug level
Debug bool
)

const (
Expand All @@ -45,9 +49,15 @@ const (
func ParseFlags() error {
flag.StringVar(&InResults, "in", "", "")
flag.StringVar(&OutFile, "out", "", "")
flag.BoolVar(&Debug, "debug", false, "turn on debug logging")
flag.BoolVar(&Append, "append", false, "Append to output file instead of overwriting it")

flag.Parse()
if Debug {
draconLogger.SetDefault(slog.LevelDebug, os.Getenv(EnvDraconScanID), true)
} else {
draconLogger.SetDefault(slog.LevelInfo, os.Getenv(EnvDraconScanID), true)
}
if InResults == "" {
return fmt.Errorf("in is undefined")
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/log/dracon-logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ import (
"os"
)

func SetDefault(logLevel slog.Level) {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel}))
func SetDefault(logLevel slog.Level, scanID string, jsonLogging bool) {
var logger *slog.Logger
if jsonLogging {
logger = slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel}))
} else {
logger = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel}))
}
if scanID != "" {
logger = logger.With("scanID", scanID)
}
slog.SetDefault(logger)
}

0 comments on commit 8d6d4c4

Please sign in to comment.