Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Add log format flag and default to logfmt formatting for logs
Browse files Browse the repository at this point in the history
This commit adds a new log format flag which can be used for setting the format
of the logs to either logfmt (default) or json. It also cleans up logger flags
and initalization logic a bit.
  • Loading branch information
antekresic committed Sep 25, 2020
1 parent 40d466f commit ad7d968
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 20 deletions.
4 changes: 1 addition & 3 deletions cmd/promscale/main.go
Expand Up @@ -4,7 +4,6 @@
package main

import (
"flag"
"fmt"
"os"

Expand All @@ -14,15 +13,14 @@ import (
)

func main() {
logLevel := flag.String("log-level", "debug", "The log level to use [ \"error\", \"warn\", \"info\", \"debug\" ].")
cfg := &runner.Config{}
cfg, err := runner.ParseFlags(cfg)
if err != nil {
fmt.Println("Version: ", version.Version, "Commit Hash: ", version.CommitHash)
fmt.Println("Fatal error: cannot parse flags ", err)
os.Exit(1)
}
err = log.Init(*logLevel)
err = log.Init(cfg.LogCfg)
if err != nil {
fmt.Println("Version: ", version.Version, "Commit Hash: ", version.CommitHash)
fmt.Println("Fatal error: cannot start logger", err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/api/common_test.go
Expand Up @@ -12,7 +12,9 @@ import (
)

func TestCORSWrapper(t *testing.T) {
_ = log.Init("debug")
_ = log.Init(log.Config{
Level: "debug",
})
acceptSpecific, _ := regexp.Compile("^(?:" + "http://some-site.com" + ")$")
acceptAny, _ := regexp.Compile("^(?:" + ".*" + ")$")

Expand Down
4 changes: 3 additions & 1 deletion pkg/api/health_test.go
Expand Up @@ -27,7 +27,9 @@ func (m *mockHealthChecker) HealthCheck() error {
}

func TestHealth(t *testing.T) {
_ = log.Init("debug")
_ = log.Init(log.Config{
Level: "debug",
})

testCases := []struct {
name string
Expand Down
4 changes: 3 additions & 1 deletion pkg/api/labels_test.go
Expand Up @@ -15,7 +15,9 @@ import (
)

func TestLabels(t *testing.T) {
_ = log.Init("debug")
_ = log.Init(log.Config{
Level: "debug",
})
testCases := []struct {
name string
querier *mockQuerier
Expand Down
4 changes: 3 additions & 1 deletion pkg/api/query_range_test.go
Expand Up @@ -18,7 +18,9 @@ import (
)

func TestRangedQuery(t *testing.T) {
_ = log.Init("debug")
_ = log.Init(log.Config{
Level: "debug",
})
testCases := []struct {
name string
timeout string
Expand Down
4 changes: 3 additions & 1 deletion pkg/api/query_test.go
Expand Up @@ -121,7 +121,9 @@ func TestParseDuration(t *testing.T) {
}

func TestQuery(t *testing.T) {
_ = log.Init("debug")
_ = log.Init(log.Config{
Level: "debug",
})
testCases := []struct {
name string
timeout string
Expand Down
4 changes: 3 additions & 1 deletion pkg/api/series_test.go
Expand Up @@ -15,7 +15,9 @@ import (
)

func TestSeries(t *testing.T) {
_ = log.Init("debug")
_ = log.Init(log.Config{
Level: "debug",
})

testCases := []struct {
name string
Expand Down
4 changes: 3 additions & 1 deletion pkg/api/write_test.go
Expand Up @@ -24,7 +24,9 @@ import (
)

func TestWrite(t *testing.T) {
testutil.Ok(t, log.Init("debug"))
testutil.Ok(t, log.Init(log.Config{
Level: "debug",
}))
testCases := []struct {
name string
responseCode int
Expand Down
34 changes: 29 additions & 5 deletions pkg/log/log.go
Expand Up @@ -6,6 +6,7 @@
package log

import (
"flag"
"fmt"
"os"
"time"
Expand All @@ -25,16 +26,39 @@ var (
)
)

// Init starts logging given the minimum log level
func Init(logLevel string) error {
l := log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
logLevelOption, err := parseLogLevel(logLevel)
// Config represents a logger configuration used upon initialization.
type Config struct {
Level string
Format string
}

// ParseFlags parses the configuration flags for logging.
func ParseFlags(cfg *Config) *Config {
flag.StringVar(&cfg.Level, "log-level", "debug", "The log level to use [ \"error\", \"warn\", \"info\", \"debug\" ].")
flag.StringVar(&cfg.Format, "log-format", "logfmt", "The log format to use [ \"logfmt\", \"json\" ].")
return cfg
}

// Init starts logging given the configuration. By default, it uses logfmt format
// and minimum logging level.
func Init(cfg Config) error {
var l log.Logger
switch cfg.Format {
case "logfmt", "":
l = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
case "json":
l = log.NewJSONLogger(log.NewSyncWriter(os.Stderr))
default:
return fmt.Errorf("unrecognized log format %s", cfg.Format)
}

logLevelOption, err := parseLogLevel(cfg.Level)
if err != nil {
return err
}

l = level.NewFilter(l, logLevelOption)
logger = log.With(l, "ts", timestampFormat, "caller", log.Caller(4))
logger = log.With(l, "ts", timestampFormat, "caller", log.DefaultCaller)
return nil
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/pgmodel/end_to_end_tests/main_test.go
Expand Up @@ -41,7 +41,9 @@ func TestMain(m *testing.M) {
func() {
flag.Parse()
ctx := context.Background()
_ = log.Init("debug")
_ = log.Init(log.Config{
Level: "debug",
})
if !testing.Short() {
var err error

Expand Down
4 changes: 3 additions & 1 deletion pkg/pgmodel/upgrade_tests/upgrade_test.go
Expand Up @@ -51,7 +51,9 @@ func TestMain(m *testing.M) {
prevDBImage = "timescaledev/timescale_prometheus_extra:0.1.1-pg12"
}
flag.Parse()
_ = log.Init("debug")
_ = log.Init(log.Config{
Level: "debug",
})
code = m.Run()
os.Exit(code)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/runner/runner.go
Expand Up @@ -33,6 +33,7 @@ type Config struct {
ListenAddr string
TelemetryPath string
PgmodelCfg pgclient.Config
LogCfg log.Config
HaGroupLockID int64
RestElection bool
PrometheusTimeout time.Duration
Expand All @@ -57,6 +58,7 @@ var (

func ParseFlags(cfg *Config) (*Config, error) {
pgclient.ParseFlags(&cfg.PgmodelCfg)
log.ParseFlags(&cfg.LogCfg)

flag.StringVar(&cfg.ListenAddr, "web-listen-address", ":9201", "Address to listen on for web endpoints.")
flag.StringVar(&cfg.TelemetryPath, "web-telemetry-path", "/metrics", "Address to listen on for web endpoints.")
Expand Down
4 changes: 3 additions & 1 deletion pkg/runner/runner_test.go
Expand Up @@ -14,7 +14,9 @@ import (

func TestMain(m *testing.M) {
flag.Parse()
err := log.Init("debug")
err := log.Init(log.Config{
Level: "debug",
})

if err != nil {
fmt.Println("Error initializing logger", err)
Expand Down
4 changes: 3 additions & 1 deletion pkg/util/election_test.go
Expand Up @@ -205,7 +205,9 @@ func TestPrometheusLivenessCheck(t *testing.T) {
}
func TestMain(m *testing.M) {
flag.Parse()
err := log.Init("debug")
err := log.Init(log.Config{
Level: "debug",
})
if err != nil {
panic(err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/util/util_test.go
Expand Up @@ -17,7 +17,9 @@ const (
)

func init() {
err := log.Init("debug")
err := log.Init(log.Config{
Level: "debug",
})
if err != nil {
panic(err)
}
Expand Down

0 comments on commit ad7d968

Please sign in to comment.