feat: Using logging module
This commit is contained in:
parent
04fceee835
commit
08e2d6bd4a
@ -35,3 +35,14 @@ db:
|
|||||||
max-open-connections: 100
|
max-open-connections: 100
|
||||||
# max connection life time
|
# max connection life time
|
||||||
max-connection-life-time: 10s
|
max-connection-life-time: 10s
|
||||||
|
|
||||||
|
log:
|
||||||
|
level: debug
|
||||||
|
development: true
|
||||||
|
disalbe-caller: false
|
||||||
|
disable-stacktrace: false
|
||||||
|
# console or json
|
||||||
|
format: console
|
||||||
|
output-paths:
|
||||||
|
- stdout
|
||||||
|
- /tmp/howmuch.log
|
||||||
|
@ -23,11 +23,11 @@
|
|||||||
package howmuch
|
package howmuch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
@ -64,8 +64,20 @@ func initConfig() {
|
|||||||
viper.SetEnvKeyReplacer(replacer)
|
viper.SetEnvKeyReplacer(replacer)
|
||||||
|
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
if err := viper.ReadInConfig(); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
log.ErrorLog("Failed to read viper configuration file", "err", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintln(os.Stdout, "Using config file:", viper.ConfigFileUsed())
|
log.InfoLog("Using config file", "file", viper.ConfigFileUsed())
|
||||||
|
}
|
||||||
|
|
||||||
|
// logOptions set log options from the configs read by viper.
|
||||||
|
func logOptions() *log.Options {
|
||||||
|
return &log.Options{
|
||||||
|
Level: viper.GetString("log.level"),
|
||||||
|
Development: viper.GetBool("log.development"),
|
||||||
|
DisableCaller: viper.GetBool("log.disable-caller"),
|
||||||
|
DisableStacktrace: viper.GetBool("log.disable-stacktrace"),
|
||||||
|
Format: viper.GetString("log.format"),
|
||||||
|
OutputPaths: viper.GetStringSlice("log.output-paths"),
|
||||||
|
}
|
||||||
}
|
}
|
@ -27,6 +27,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"git.vinchent.xyz/vinchent/howmuch/internal/pkg/log"
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -47,6 +48,11 @@ func NewHowMuchCommand() *cobra.Command {
|
|||||||
Long: `howmuch is a expense-sharing application that can help friends
|
Long: `howmuch is a expense-sharing application that can help friends
|
||||||
to share their expense of an event or a trip`,
|
to share their expense of an event or a trip`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
// init log
|
||||||
|
log.Init(logOptions())
|
||||||
|
// Sync flush the buffer
|
||||||
|
defer log.Sync()
|
||||||
|
|
||||||
return run()
|
return run()
|
||||||
},
|
},
|
||||||
Args: func(cmd *cobra.Command, args []string) error {
|
Args: func(cmd *cobra.Command, args []string) error {
|
||||||
@ -76,18 +82,18 @@ to share their expense of an event or a trip`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func run() error {
|
func run() error {
|
||||||
fmt.Println("How much do I owe you?")
|
log.DebugLog("How much do I owe you?")
|
||||||
|
|
||||||
settings, _ := json.MarshalIndent(viper.AllSettings(), "", " ")
|
settings, _ := json.MarshalIndent(viper.AllSettings(), "", " ")
|
||||||
|
|
||||||
// watching reloading conf
|
// watching reloading conf
|
||||||
viper.OnConfigChange(func(e fsnotify.Event) {
|
viper.OnConfigChange(func(e fsnotify.Event) {
|
||||||
fmt.Println("Config file changed:", e.Name)
|
log.InfoLog("Config file changed:", e.Name)
|
||||||
})
|
})
|
||||||
viper.WatchConfig()
|
viper.WatchConfig()
|
||||||
|
|
||||||
fmt.Println(string(settings))
|
log.InfoLog(string(settings))
|
||||||
fmt.Println(viper.GetString("db.username"))
|
log.InfoLog(viper.GetString("db.username"))
|
||||||
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user