diff --git a/cmd/tile38-server/main.go b/cmd/tile38-server/main.go index 99c9a10f..c250f609 100644 --- a/cmd/tile38-server/main.go +++ b/cmd/tile38-server/main.go @@ -10,6 +10,7 @@ import ( _ "net/http/pprof" "os" "os/signal" + "path/filepath" "runtime" "runtime/pprof" "strconv" @@ -17,6 +18,7 @@ import ( "sync" "syscall" + "github.com/tidwall/gjson" "github.com/tidwall/tile38/core" "github.com/tidwall/tile38/internal/hservice" "github.com/tidwall/tile38/internal/log" @@ -76,6 +78,7 @@ Basic Options: -p port : listening port (default: 9851) -d path : data directory (default: data) -s socket : listen on unix socket file + -l encoding : set log encoding to json or text (default: text) -q : no logging. totally silent output -v : enable verbose logging -vv : enable very verbose logging @@ -177,10 +180,6 @@ Developer Options: case "--nohup", "-nohup": nohup = true continue - case "--logjson", "-logjson": - log.LogJSON = true - log.Build("") - continue case "--appendonly", "-appendonly": i++ if i < len(os.Args) { @@ -261,6 +260,7 @@ Developer Options: unixSocket string verbose bool veryVerbose bool + logEncoding string quiet bool pidfile string cpuprofile string @@ -273,6 +273,7 @@ Developer Options: flag.StringVar(&host, "h", "", "The listening host") flag.StringVar(&unixSocket, "s", "", "Listen on a unix socket") 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(&quiet, "q", false, "Quiet logging. Totally silent") 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.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 if quiet { logw = ioutil.Discard diff --git a/internal/server/server.go b/internal/server/server.go index a9270f47..74a0a607 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -154,6 +154,8 @@ func Serve(opts Options) error { core.QueueFileName = path.Join(opts.Dir, "queue.db") } + log.Infof("Server started, Tile38 version %s, git %s", core.Version, core.GitSHA) + // Initialize the s s := &Server{ unix: opts.UnixSocketPath, @@ -195,14 +197,6 @@ func Serve(opts Options) error { 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 // with `"ok":false`. T38HTTP500ERRORS=1 s.http500Errors, _ = strconv.ParseBool(os.Getenv("T38HTTP500ERRORS"))