mirror of
https://github.com/hacdias/webdav.git
synced 2026-09-22 03:20:41 +08:00
feat: introduce structured, field logging (#87)
* feat: introduce structured, field logging
The logging is currently minimal and only for initial message of listening and user login attempts
* chore: major version upgarde
there was a breaking API change in 8cd6d0a585 for which the git tag was v4.0.0 but the Go SIV was missed. This commit updates the SIV and retracts the defective version of v4.1.0.
This commit is contained in:
+20
-5
@@ -1,7 +1,6 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -9,6 +8,8 @@ import (
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
v "github.com/spf13/viper"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -27,6 +28,7 @@ func init() {
|
||||
flags.StringP("address", "a", "0.0.0.0", "address to listen to")
|
||||
flags.StringP("port", "p", "0", "port to listen to")
|
||||
flags.StringP("prefix", "P", "/", "URL path prefix")
|
||||
flags.String("log_format", "console", "logging format")
|
||||
}
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
@@ -67,18 +69,31 @@ set WD_CERT.`,
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
loggerConfig := zap.NewProductionConfig()
|
||||
loggerConfig.DisableCaller = true
|
||||
loggerConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
loggerConfig.Encoding = cfg.LogFormat
|
||||
logger, err := loggerConfig.Build()
|
||||
if err != nil {
|
||||
// if we fail to configure proper logging, then the user has deliberately
|
||||
// misconfigured the logger. Abort.
|
||||
panic(err)
|
||||
}
|
||||
zap.ReplaceGlobals(logger)
|
||||
defer func() {
|
||||
_ = zap.L().Sync()
|
||||
}()
|
||||
// Tell the user the port in which is listening.
|
||||
fmt.Println("Listening on", listener.Addr().String())
|
||||
zap.L().Info("Listening", zap.String("address", listener.Addr().String()))
|
||||
|
||||
// Starts the server.
|
||||
if getOptB(flags, "tls") {
|
||||
if err := http.ServeTLS(listener, cfg, getOpt(flags, "cert"), getOpt(flags, "key")); err != nil {
|
||||
log.Fatal(err)
|
||||
zap.L().Fatal("shutting server", zap.Error(err))
|
||||
}
|
||||
} else {
|
||||
if err := http.Serve(listener, cfg); err != nil {
|
||||
log.Fatal(err)
|
||||
zap.L().Fatal("shutting server", zap.Error(err))
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user