Skip to content

add additional debug logs to cli #1942

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

Merged
merged 9 commits into from
Apr 29, 2025
Merged
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
26 changes: 21 additions & 5 deletions cli/cmd/digger/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,31 @@ import (
"github.com/diggerhq/digger/libs/policy"
lib_spec "github.com/diggerhq/digger/libs/spec"
"github.com/spf13/cobra"
"log"
"log/slog"
"os"
"runtime/debug"
)

func initLogger() {
logLevel := os.Getenv("DIGGER_LOG_LEVEL")
var level slog.Leveler
if logLevel == "DEBUG" {
level = slog.LevelDebug
} else {
level = slog.LevelInfo
}
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
}))

slog.SetDefault(logger)

}

var defaultCmd = &cobra.Command{
Use: "default",
Run: func(cmd *cobra.Command, args []string) {

initLogger()
specStr := os.Getenv("DIGGER_RUN_SPEC")
if specStr != "" {
var spec lib_spec.Spec
Expand All @@ -31,7 +47,7 @@ var defaultCmd = &cobra.Command{
}

var spec_err error

spec_err = spec2.RunSpec(
spec,
lib_spec.VCSProviderBasic{},
Expand Down Expand Up @@ -65,10 +81,10 @@ var defaultCmd = &cobra.Command{

defer func() {
if r := recover(); r != nil {
log.Println(fmt.Sprintf("stacktrace from panic: \n" + string(debug.Stack())))
slog.Error(fmt.Sprintf("stacktrace from panic: " + string(debug.Stack())))
err := usage.SendLogRecord(logLeader, fmt.Sprintf("Panic occurred. %s", r))
if err != nil {
log.Printf("Failed to send log record. %s\n", err)
slog.Error("Failed to send log record", "error", err)
}
os.Exit(1)
}
Expand Down
9 changes: 0 additions & 9 deletions cli/cmd/digger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"github.com/diggerhq/digger/cli/pkg/usage"
"log"
"os"
)

Expand All @@ -30,11 +29,3 @@ func main() {
}

}

func init() {
log.SetOutput(os.Stdout)

if os.Getenv("DEBUG") == "true" {
log.SetFlags(log.Ltime | log.Lshortfile)
}
}
8 changes: 4 additions & 4 deletions cli/cmd/digger/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
locking2 "github.com/diggerhq/digger/libs/locking"
core_policy "github.com/diggerhq/digger/libs/policy"
"github.com/spf13/cobra"
"log"
"log/slog"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -108,15 +108,15 @@ func PreRun(cmd *cobra.Command, args []string) {
if os.Getenv("NO_BACKEND") == "true" {
lock, err = locking2.GetLock()
} else {
log.Printf("Warning: not performing locking in cli since digger is invoked with orchestrator mode, any arguments to LOCKING_PROVIDER will be ignored")
slog.Warn("Not performing locking in cli since digger is invoked with orchestrator mode, any arguments to LOCKING_PROVIDER will be ignored")
lock = locking2.NoOpLock{}
err = nil
}
if err != nil {
log.Printf("Failed to create lock provider. %s\n", err)
slog.Error("Failed to create lock provider", "error", err)
os.Exit(2)
}
log.Println("Lock provider has been created successfully")
slog.Info("Lock provider has been created successfully")
}

var rootCmd = &cobra.Command{
Expand Down
Loading
Loading