refactor: set log encoding early

This commit is contained in:
Benjamin Ramser 2021-12-27 12:20:14 +01:00
parent 20cc624918
commit 407fd4c71c
2 changed files with 18 additions and 12 deletions

View File

@ -10,6 +10,7 @@ import (
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"os/signal" "os/signal"
"path/filepath"
"runtime" "runtime"
"runtime/pprof" "runtime/pprof"
"strconv" "strconv"
@ -17,6 +18,7 @@ import (
"sync" "sync"
"syscall" "syscall"
"github.com/tidwall/gjson"
"github.com/tidwall/tile38/core" "github.com/tidwall/tile38/core"
"github.com/tidwall/tile38/internal/hservice" "github.com/tidwall/tile38/internal/hservice"
"github.com/tidwall/tile38/internal/log" "github.com/tidwall/tile38/internal/log"
@ -76,6 +78,7 @@ Basic Options:
-p port : listening port (default: 9851) -p port : listening port (default: 9851)
-d path : data directory (default: data) -d path : data directory (default: data)
-s socket : listen on unix socket file -s socket : listen on unix socket file
-l encoding : set log encoding to json or text (default: text)
-q : no logging. totally silent output -q : no logging. totally silent output
-v : enable verbose logging -v : enable verbose logging
-vv : enable very verbose logging -vv : enable very verbose logging
@ -177,10 +180,6 @@ Developer Options:
case "--nohup", "-nohup": case "--nohup", "-nohup":
nohup = true nohup = true
continue continue
case "--logjson", "-logjson":
log.LogJSON = true
log.Build("")
continue
case "--appendonly", "-appendonly": case "--appendonly", "-appendonly":
i++ i++
if i < len(os.Args) { if i < len(os.Args) {
@ -261,6 +260,7 @@ Developer Options:
unixSocket string unixSocket string
verbose bool verbose bool
veryVerbose bool veryVerbose bool
logEncoding string
quiet bool quiet bool
pidfile string pidfile string
cpuprofile string cpuprofile string
@ -273,6 +273,7 @@ Developer Options:
flag.StringVar(&host, "h", "", "The listening host") flag.StringVar(&host, "h", "", "The listening host")
flag.StringVar(&unixSocket, "s", "", "Listen on a unix socket") flag.StringVar(&unixSocket, "s", "", "Listen on a unix socket")
flag.StringVar(&dir, "d", "data", "The data directory") flag.StringVar(&dir, "d", "data", "The data directory")
flag.StringVar(&logEncoding, "l", "text", "The log encoding json or text (default: text)")
flag.BoolVar(&verbose, "v", false, "Enable verbose logging") flag.BoolVar(&verbose, "v", false, "Enable verbose logging")
flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent") flag.BoolVar(&quiet, "q", false, "Quiet logging. Totally silent")
flag.BoolVar(&veryVerbose, "vv", false, "Enable very verbose logging") flag.BoolVar(&veryVerbose, "vv", false, "Enable very verbose logging")
@ -281,6 +282,17 @@ Developer Options:
flag.StringVar(&memprofile, "memprofile", "", "write memory profile to `file`") flag.StringVar(&memprofile, "memprofile", "", "write memory profile to `file`")
flag.Parse() flag.Parse()
if logEncoding == "json" {
log.LogJSON = true
data, _ := os.ReadFile(filepath.Join(dir, "config"))
if gjson.GetBytes(data, "logconfig.encoding").String() == "json" {
c := gjson.GetBytes(data, "logconfig").String()
log.Build(c)
} else {
log.Build("")
}
}
var logw io.Writer = os.Stderr var logw io.Writer = os.Stderr
if quiet { if quiet {
logw = ioutil.Discard logw = ioutil.Discard

View File

@ -154,6 +154,8 @@ func Serve(opts Options) error {
core.QueueFileName = path.Join(opts.Dir, "queue.db") core.QueueFileName = path.Join(opts.Dir, "queue.db")
} }
log.Infof("Server started, Tile38 version %s, git %s", core.Version, core.GitSHA)
// Initialize the s // Initialize the s
s := &Server{ s := &Server{
unix: opts.UnixSocketPath, unix: opts.UnixSocketPath,
@ -195,14 +197,6 @@ func Serve(opts Options) error {
return err return err
} }
lcfg := s.config.logConfig()
if lcfg != "" {
log.LogJSON = true
log.Build(lcfg)
}
log.Infof("Server started, Tile38 version %s, git %s", core.Version, core.GitSHA)
// Send "500 Internal Server" error instead of "200 OK" for json responses // Send "500 Internal Server" error instead of "200 OK" for json responses
// with `"ok":false`. T38HTTP500ERRORS=1 // with `"ok":false`. T38HTTP500ERRORS=1
s.http500Errors, _ = strconv.ParseBool(os.Getenv("T38HTTP500ERRORS")) s.http500Errors, _ = strconv.ParseBool(os.Getenv("T38HTTP500ERRORS"))