Skip to content

Commit

Permalink
add: zap を導入
Browse files Browse the repository at this point in the history
  • Loading branch information
sakuhanight committed Mar 16, 2024
1 parent 4d1f128 commit ae208ed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
24 changes: 23 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"emojitool/cmd/get"
"fmt"
"github.com/fsnotify/fsnotify"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"os"

"github.com/spf13/cobra"
Expand All @@ -39,7 +41,7 @@ This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
//Run: func(cmd *cobra.Command, args []string) {},
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -62,6 +64,9 @@ func init() {

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.emojitool.yaml)")

rootCmd.PersistentFlags().Bool("verbose", false, "output debug logs")
viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))

rootCmd.PersistentFlags().String("host", "", "misskey server domain")
viper.BindPFlag("host", rootCmd.PersistentFlags().Lookup("host"))

Expand Down Expand Up @@ -100,4 +105,21 @@ func initConfig() {
viper.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("config file changed: ", e.Name)
})

// loger setup
config := zap.NewProductionConfig()
encoderConfig := zap.NewProductionEncoderConfig()
encoderConfig.TimeKey = "timestamp"
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
config.EncoderConfig = encoderConfig

if viper.GetBool("verbose") {
config.Level = zap.NewAtomicLevelAt(zapcore.DebugLevel)
} else {
config.Level = zap.NewAtomicLevelAt(zapcore.InfoLevel)
}

logger, _ := config.Build(zap.AddCallerSkip(1))
defer logger.Sync()
zap.ReplaceGlobals(logger)
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ require (
github.com/spf13/viper v1.18.2 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
Expand Down

0 comments on commit ae208ed

Please sign in to comment.