Skip to content

Commit e11a804

Browse files
authored
feat: Support loglevel (#98)
Signed-off-by: Ce Gao <[email protected]>
1 parent 991692e commit e11a804

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

cmd/ormb/cmd/prerun.go

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

33
import (
4-
"fmt"
54
"os"
65
"path/filepath"
76

7+
"github.com/sirupsen/logrus"
88
"github.com/spf13/cobra"
99
"github.com/spf13/viper"
1010

@@ -17,7 +17,10 @@ func preRunE(cmd *cobra.Command, args []string) error {
1717
if err != nil {
1818
return err
1919
}
20-
fmt.Printf("Using %s as the root path\n", rootPath)
20+
21+
logrus.WithFields(logrus.Fields{
22+
"root-path": rootPath,
23+
}).Debugln("Create the ormb client with the given root path")
2124

2225
ormbClient, err = ormb.New(
2326
oras.ClientOptRootPath(rootPath),

cmd/ormb/cmd/root.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ limitations under the License.
1616
package cmd
1717

1818
import (
19-
"fmt"
20-
"os"
2119
"path/filepath"
2220

23-
"github.com/kleveross/ormb/pkg/ormb"
24-
"github.com/spf13/cobra"
25-
2621
homedir "github.com/mitchellh/go-homedir"
22+
"github.com/sirupsen/logrus"
23+
"github.com/spf13/cobra"
2724
"github.com/spf13/viper"
25+
26+
"github.com/kleveross/ormb/pkg/ormb"
2827
)
2928

3029
var cfgFile string
30+
var logLevel uint32
3131

3232
var ormbClient ormb.Interface
3333

@@ -42,26 +42,36 @@ var rootCmd = &cobra.Command{
4242
// This is called by main.main(). It only needs to happen once to the rootCmd.
4343
func Execute() {
4444
if err := rootCmd.Execute(); err != nil {
45-
fmt.Println(err)
46-
os.Exit(1)
45+
logrus.WithField("error", err).Panicln("Failed to run the command")
4746
}
4847
}
4948

5049
func init() {
5150
viper.SetEnvPrefix("ORMB")
51+
cobra.OnInitialize(initLogger)
5252
cobra.OnInitialize(initConfig)
5353

5454
// Here you will define your flags and configuration settings.
5555
// Cobra supports persistent flags, which, if defined here,
5656
// will be global for your application.
5757

5858
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ormb/config.yaml)")
59+
rootCmd.PersistentFlags().Uint32Var(&logLevel, "log-level", 4, "Log level")
5960

6061
// Cobra also supports local flags, which will only run
6162
// when this action is called directly.
6263
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
6364
}
6465

66+
func initLogger() {
67+
logrus.SetLevel(logrus.Level(logLevel))
68+
69+
logrus.SetFormatter(&logrus.TextFormatter{
70+
FullTimestamp: true,
71+
})
72+
logrus.SetReportCaller(false)
73+
}
74+
6575
// initConfig reads in config file and ENV variables if set.
6676
func initConfig() {
6777
if cfgFile != "" {
@@ -71,8 +81,7 @@ func initConfig() {
7181
// Find home directory.
7282
home, err := homedir.Dir()
7383
if err != nil {
74-
fmt.Println(err)
75-
os.Exit(1)
84+
logrus.WithField("error", err).Panicln("Failed to find the home directory")
7685
}
7786

7887
// Search config in home directory with name "config" (without extension).
@@ -87,6 +96,8 @@ func initConfig() {
8796

8897
// If a config file is found, read it in.
8998
if err := viper.ReadInConfig(); err == nil {
90-
fmt.Println("Using config file:", viper.ConfigFileUsed())
99+
logrus.WithFields(logrus.Fields{
100+
"config": viper.ConfigFileUsed(),
101+
}).Debugln("Found the config file")
91102
}
92103
}

0 commit comments

Comments
 (0)