Configure logging level and format
This commit is contained in:
parent
3e1a93296e
commit
dbd3b26f6c
@ -55,6 +55,7 @@ func RequestLogger(logger *logrus.Logger) gin.HandlerFunc {
|
|||||||
"method": c.Request.Method,
|
"method": c.Request.Method,
|
||||||
"remote_addr": c.ClientIP(),
|
"remote_addr": c.ClientIP(),
|
||||||
"duration": latency,
|
"duration": latency,
|
||||||
|
"path": path,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.Errors.Last(); err != nil {
|
if err := c.Errors.Last(); err != nil {
|
||||||
@ -66,7 +67,7 @@ func RequestLogger(logger *logrus.Logger) gin.HandlerFunc {
|
|||||||
fields["raw_query"] = c.Request.URL.RawQuery
|
fields["raw_query"] = c.Request.URL.RawQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
entry := logrus.WithFields(fields)
|
entry := logger.WithFields(fields)
|
||||||
msg := "http_request " + path
|
msg := "http_request " + path
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
|
@ -158,8 +158,9 @@ func initOptions() {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.Debug {
|
if err := configureLogger(opts); err != nil {
|
||||||
logger.SetLevel(logrus.DebugLevel)
|
exitWithMessage(err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.ReadOnly {
|
if options.ReadOnly {
|
||||||
@ -175,6 +176,29 @@ func initOptions() {
|
|||||||
printVersion()
|
printVersion()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func configureLogger(opts command.Options) error {
|
||||||
|
if options.Debug {
|
||||||
|
logger.SetLevel(logrus.DebugLevel)
|
||||||
|
} else {
|
||||||
|
lvl, err := logrus.ParseLevel(opts.LogLevel)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logger.SetLevel(lvl)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch options.LogFormat {
|
||||||
|
case "text":
|
||||||
|
logger.SetFormatter(&logrus.TextFormatter{})
|
||||||
|
case "json":
|
||||||
|
logger.SetFormatter(&logrus.JSONFormatter{})
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("invalid logger format: %v", options.LogFormat)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func printVersion() {
|
func printVersion() {
|
||||||
chunks := []string{fmt.Sprintf("Pgweb v%s", command.Version)}
|
chunks := []string{fmt.Sprintf("Pgweb v%s", command.Version)}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -18,6 +19,8 @@ const (
|
|||||||
type Options struct {
|
type Options struct {
|
||||||
Version bool `short:"v" long:"version" description:"Print version"`
|
Version bool `short:"v" long:"version" description:"Print version"`
|
||||||
Debug bool `short:"d" long:"debug" description:"Enable debugging mode"`
|
Debug bool `short:"d" long:"debug" description:"Enable debugging mode"`
|
||||||
|
LogLevel string `long:"log-level" description:"Logging level" default:"info"`
|
||||||
|
LogFormat string `long:"log-format" description:"Logging output format" default:"text"`
|
||||||
URL string `long:"url" description:"Database connection string"`
|
URL string `long:"url" description:"Database connection string"`
|
||||||
Host string `long:"host" description:"Server hostname or IP" default:"localhost"`
|
Host string `long:"host" description:"Server hostname or IP" default:"localhost"`
|
||||||
Port int `long:"port" description:"Server port" default:"5432"`
|
Port int `long:"port" description:"Server port" default:"5432"`
|
||||||
@ -62,6 +65,11 @@ func ParseOptions(args []string) (Options, error) {
|
|||||||
return opts, err
|
return opts, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = logrus.ParseLevel(opts.LogLevel)
|
||||||
|
if err != nil {
|
||||||
|
return opts, err
|
||||||
|
}
|
||||||
|
|
||||||
if opts.URL == "" {
|
if opts.URL == "" {
|
||||||
opts.URL = getPrefixedEnvVar("DATABASE_URL")
|
opts.URL = getPrefixedEnvVar("DATABASE_URL")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user