Skip to content

Commit d1029e1

Browse files
authored
feat: add logging framework (#21)
1 parent a363cda commit d1029e1

File tree

5 files changed

+95
-10
lines changed

5 files changed

+95
-10
lines changed

cmd/ocmTransfer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"github.com/openmcp-project/bootstrapper/internal/log"
45
ocmcli "github.com/openmcp-project/bootstrapper/internal/ocm-cli"
56

67
"github.com/spf13/cobra"
@@ -20,6 +21,10 @@ var ocmTransferCmd = &cobra.Command{
2021
"target",
2122
},
2223
RunE: func(cmd *cobra.Command, args []string) error {
24+
log := log.GetLogger()
25+
26+
log.Debugf("Executing ocmTransfer with source: %s, target: %s", args[0], args[1])
27+
2328
transferCommands := []string{
2429
"transfer",
2530
"componentversion",

cmd/root.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"os"
55

66
"github.com/spf13/cobra"
7+
8+
"github.com/openmcp-project/bootstrapper/internal/log"
79
)
810

9-
// rootCmd represents the base command when called without any subcommands
11+
// RootCmd represents the base command when called without any subcommands
1012
var RootCmd = &cobra.Command{
1113
Use: "openmcp-bootstrapper",
1214
Short: "The openMCP bootstrapper CLI",
@@ -27,13 +29,13 @@ func Execute() {
2729
}
2830

2931
func init() {
30-
// Here you will define your flags and configuration settings.
31-
// Cobra supports persistent flags, which, if defined here,
32-
// will be global for your application.
33-
34-
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bootstrapper.yaml)")
35-
36-
// Cobra also supports local flags, which will only run
37-
// when this action is called directly.
38-
// RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
32+
RootCmd.PersistentFlags().StringP("verbosity", "v", "info", "Set the verbosity level (panic, fatal, error, warn, info, debug, trace)")
33+
cobra.OnInitialize(func() {
34+
verbosity, err := RootCmd.PersistentFlags().GetString("verbosity")
35+
if err != nil {
36+
log.InitLogger("info")
37+
return
38+
}
39+
log.InitLogger(verbosity)
40+
})
3941
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/openmcp-project/bootstrapper
33
go 1.25.0
44

55
require (
6+
github.com/sirupsen/logrus v1.9.3
67
github.com/spf13/cobra v1.9.1
78
github.com/stretchr/testify v1.10.0
89
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397
@@ -15,5 +16,6 @@ require (
1516
github.com/pmezard/go-difflib v1.0.0 // indirect
1617
github.com/spf13/pflag v1.0.7 // indirect
1718
go.yaml.in/yaml/v2 v2.4.2 // indirect
19+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
1820
gopkg.in/yaml.v3 v3.0.1 // indirect
1921
)

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
23
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
34
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
45
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
@@ -8,19 +9,26 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
89
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
910
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1011
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
12+
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
13+
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
1114
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
1215
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
1316
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
1417
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
1518
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
19+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
20+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1621
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
1722
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1823
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
1924
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
2025
go.yaml.in/yaml/v3 v3.0.3 h1:bXOww4E/J3f66rav3pX3m8w6jDE4knZjGOw8b5Y6iNE=
2126
go.yaml.in/yaml/v3 v3.0.3/go.mod h1:tBHosrYAkRZjRAOREWbDnBXUf08JOwYq++0QNwQiWzI=
27+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
28+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
2229
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2330
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
31+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2432
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2533
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2634
k8s.io/utils v0.0.0-20250604170112-4c0f3b243397 h1:hwvWFiBzdWw1FhfY1FooPn3kzWuJ8tmbZBHi4zVsl1Y=

internal/log/logger.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package log
2+
3+
import (
4+
"os"
5+
"sync"
6+
7+
"github.com/sirupsen/logrus"
8+
)
9+
10+
var (
11+
logger *logrus.Logger
12+
loggerOnce sync.Once
13+
levelPrintNames = map[logrus.Level]string{
14+
logrus.PanicLevel: "Panic",
15+
logrus.FatalLevel: "Fatal",
16+
logrus.ErrorLevel: "Error",
17+
logrus.WarnLevel: "Warn",
18+
logrus.InfoLevel: "Info",
19+
logrus.DebugLevel: "Debug",
20+
logrus.TraceLevel: "Trace",
21+
}
22+
levelFlagNames = map[string]logrus.Level{
23+
"panic": logrus.PanicLevel,
24+
"fatal": logrus.FatalLevel,
25+
"error": logrus.ErrorLevel,
26+
"warn": logrus.WarnLevel,
27+
"info": logrus.InfoLevel,
28+
"debug": logrus.DebugLevel,
29+
"trace": logrus.TraceLevel,
30+
}
31+
)
32+
33+
// customFormatter implements logrus.Formatter to print errors in the format: Error: message
34+
// and other levels as usual.
35+
type customFormatter struct{}
36+
37+
func (f *customFormatter) Format(entry *logrus.Entry) ([]byte, error) {
38+
level, ok := levelPrintNames[entry.Level]
39+
if !ok {
40+
level = entry.Level.String()
41+
}
42+
return []byte(level + ": " + entry.Message + "\n"), nil
43+
}
44+
45+
// GetLogger returns a singleton logrus.Logger instance
46+
func GetLogger() *logrus.Logger {
47+
loggerOnce.Do(func() {
48+
logger = logrus.New()
49+
logger.SetLevel(logrus.InfoLevel)
50+
logger.SetFormatter(&customFormatter{})
51+
})
52+
return logger
53+
}
54+
55+
// SetLevel sets the log level for the logger
56+
func SetLevel(level logrus.Level) {
57+
GetLogger().SetLevel(level)
58+
}
59+
60+
// InitLogger sets the log level based on the verbosity string and exits on error
61+
func InitLogger(verbosity string) {
62+
level, ok := levelFlagNames[verbosity]
63+
if !ok {
64+
GetLogger().Error("Unknown verbosity level: " + verbosity)
65+
os.Exit(1)
66+
}
67+
SetLevel(level)
68+
}

0 commit comments

Comments
 (0)