Skip to content

Commit

Permalink
Feature/441 improve logging (#599)
Browse files Browse the repository at this point in the history
* Added zerolog adapter

* Zerolog configured as previous

* Added config for pretty log format

* fix pkg configs too

* Added structured log methods

* Simplified logger interface

* Replaced go-logger to zerolog

* fix time format in json log

* Linter fixes:
- mute errcheck
- fix goimports

* Added Logger.Clone() method

* Fix logger mock

Co-authored-by: Andrey Kolkov <[email protected]>
  • Loading branch information
androndo and Andrey Kolkov authored Jan 29, 2021
1 parent 2828498 commit ec3e4fb
Show file tree
Hide file tree
Showing 88 changed files with 419 additions and 217 deletions.
2 changes: 1 addition & 1 deletion api/controller/pattern_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/api"
"github.com/moira-alert/moira/api/dto"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert"
"github.com/op/go-logging"
. "github.com/smartystreets/goconvey/convey"
)

Expand Down
2 changes: 1 addition & 1 deletion api/controller/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/golang/mock/gomock"
"github.com/op/go-logging"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"

"github.com/moira-alert/moira"
"github.com/moira-alert/moira/api"
Expand Down
10 changes: 5 additions & 5 deletions checker/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/checker/metrics/conversion"
"github.com/moira-alert/moira/expression"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
metricSource "github.com/moira-alert/moira/metric_source"
"github.com/moira-alert/moira/metric_source/local"
"github.com/moira-alert/moira/metrics"
mock_metric_source "github.com/moira-alert/moira/mock/metric_source"
mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert"
"github.com/op/go-logging"
. "github.com/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -322,7 +322,7 @@ func TestTriggerChecker_PrepareMetrics(t *testing.T) {

func TestGetMetricStepsStates(t *testing.T) {
logger, _ := logging.GetLogger("Test")
logging.SetLevel(logging.INFO, "Test")
logger.Level("info") // nolint: errcheck
var warnValue float64 = 10
var errValue float64 = 20

Expand Down Expand Up @@ -503,7 +503,7 @@ func TestGetMetricStepsStates(t *testing.T) {

func TestCheckForNODATA(t *testing.T) {
logger, _ := logging.GetLogger("Test")
logging.SetLevel(logging.INFO, "Test")
logger.Level("info") // nolint: errcheck
metricLastState := moira.MetricState{
EventTimestamp: 11,
Maintenance: 11111,
Expand Down Expand Up @@ -907,7 +907,7 @@ func TestCheck(t *testing.T) {
func TestIgnoreNodataToOk(t *testing.T) {
mockCtrl := gomock.NewController(t)
logger, _ := logging.GetLogger("Test")
logging.SetLevel(logging.INFO, "Test")
logger.Level("info") // nolint: errcheck
defer mockCtrl.Finish()

var retention int64 = 10
Expand Down Expand Up @@ -970,7 +970,7 @@ func TestHandleTrigger(t *testing.T) {
mockCtrl := gomock.NewController(t)
dataBase := mock_moira_alert.NewMockDatabase(mockCtrl)
logger, _ := logging.GetLogger("Test")
logging.SetLevel(logging.INFO, "Test")
logger.Level("info") // nolint: errcheck
defer mockCtrl.Finish()

var retention int64 = 10
Expand Down
2 changes: 1 addition & 1 deletion checker/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

"github.com/golang/mock/gomock"
"github.com/moira-alert/moira"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert"
"github.com/op/go-logging"
. "github.com/smartystreets/goconvey/convey"
)

Expand Down
8 changes: 4 additions & 4 deletions checker/trigger_checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"github.com/golang/mock/gomock"
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/database"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
metricSource "github.com/moira-alert/moira/metric_source"
"github.com/moira-alert/moira/metric_source/local"
"github.com/moira-alert/moira/metrics"
mock_moira_alert "github.com/moira-alert/moira/mock/moira-alert"
"github.com/op/go-logging"
. "github.com/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -77,21 +77,21 @@ func TestInitTriggerChecker(t *testing.T) {
Timestamp: 1502694427,
State: moira.StateOK,
Suppressed: false,
Values: map[string]float64{"t1": value}, //nolint
Values: map[string]float64{"t1": value}, //nolint
EventTimestamp: 1501680428,
},
"2": {
Timestamp: 1502694427,
State: moira.StateOK,
Suppressed: false,
Values: map[string]float64{"t1": value}, //nolint
Values: map[string]float64{"t1": value}, //nolint
EventTimestamp: 1501679827,
},
"3": {
Timestamp: 1502694427,
State: moira.StateOK,
Suppressed: false,
Values: map[string]float64{"t1": value}, //nolint
Values: map[string]float64{"t1": value}, //nolint
EventTimestamp: 1501679887,
},
},
Expand Down
5 changes: 3 additions & 2 deletions cmd/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ func getDefault() config {
MetricsTTL: "1h",
},
Logger: cmd.LoggerConfig{
LogFile: "stdout",
LogLevel: "info",
LogFile: "stdout",
LogLevel: "info",
LogPrettyFormat: false,
},
API: apiConfig{
Listen: ":8081",
Expand Down
5 changes: 3 additions & 2 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/moira-alert/moira/cmd"
"github.com/moira-alert/moira/database/redis"
"github.com/moira-alert/moira/index"
"github.com/moira-alert/moira/logging/go-logging"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
metricSource "github.com/moira-alert/moira/metric_source"
"github.com/moira-alert/moira/metric_source/local"
"github.com/moira-alert/moira/metric_source/remote"
Expand Down Expand Up @@ -61,7 +61,8 @@ func main() {

apiConfig := config.API.getSettings(config.Redis.MetricsTTL, config.Remote.MetricsTTL)

logger, err := logging.ConfigureLog(config.Logger.LogFile, config.Logger.LogLevel, serviceName)
logger, err := logging.ConfigureLog(config.Logger.LogFile, config.Logger.LogLevel, serviceName, config.Logger.LogPrettyFormat)

if err != nil {
fmt.Fprintf(os.Stderr, "Can not configure log: %s\n", err.Error())
os.Exit(1)
Expand Down
5 changes: 3 additions & 2 deletions cmd/checker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ func getDefault() config {
MetricsTTL: "1h",
},
Logger: cmd.LoggerConfig{
LogFile: "stdout",
LogLevel: "info",
LogFile: "stdout",
LogLevel: "info",
LogPrettyFormat: false,
},
Checker: checkerConfig{
NoDataCheckInterval: "60s",
Expand Down
6 changes: 3 additions & 3 deletions cmd/checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/moira-alert/moira/checker/worker"
"github.com/moira-alert/moira/cmd"
"github.com/moira-alert/moira/database/redis"
"github.com/moira-alert/moira/logging/go-logging"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
"github.com/moira-alert/moira/metrics"
)

Expand Down Expand Up @@ -61,7 +61,7 @@ func main() {
os.Exit(1)
}

logger, err = logging.ConfigureLog(config.Logger.LogFile, config.Logger.LogLevel, serviceName)
logger, err = logging.ConfigureLog(config.Logger.LogFile, config.Logger.LogLevel, serviceName, config.Logger.LogPrettyFormat)
if err != nil {
fmt.Fprintf(os.Stderr, "Can not configure log: %s\n", err.Error())
os.Exit(1)
Expand Down Expand Up @@ -97,7 +97,7 @@ func main() {
SourceProvider: metricSourceProvider,
Metrics: checkerMetrics,
TriggerCache: cache.New(checkerSettings.CheckInterval, time.Minute*60), //nolint
LazyTriggersCache: cache.New(time.Minute*10, time.Minute*60), //nolint
LazyTriggersCache: cache.New(time.Minute*10, time.Minute*60), //nolint
PatternCache: cache.New(checkerSettings.CheckInterval, time.Minute*60), //nolint
}
err = checkerWorker.Start()
Expand Down
14 changes: 8 additions & 6 deletions cmd/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
)

type config struct {
LogFile string `yaml:"log_file"`
LogLevel string `yaml:"log_level"`
Redis cmd.RedisConfig `yaml:"redis"`
Cleanup cleanupConfig `yaml:"cleanup"`
LogFile string `yaml:"log_file"`
LogLevel string `yaml:"log_level"`
LogPrettyFormat bool `yaml:"log_pretty_format"`
Redis cmd.RedisConfig `yaml:"redis"`
Cleanup cleanupConfig `yaml:"cleanup"`
}

type cleanupConfig struct {
Expand All @@ -19,8 +20,9 @@ type cleanupConfig struct {

func getDefault() config {
return config{
LogFile: "stdout",
LogLevel: "info",
LogFile: "stdout",
LogLevel: "info",
LogPrettyFormat: false,
Redis: cmd.RedisConfig{
Host: "localhost",
Port: "6379",
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/cmd"
"github.com/moira-alert/moira/database/redis"
"github.com/moira-alert/moira/logging/go-logging"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
)

// Moira version
Expand Down Expand Up @@ -121,7 +121,7 @@ func initApp() (cleanupConfig, moira.Logger, moira.Database) {
os.Exit(1)
}

logger, err := logging.ConfigureLog(config.LogFile, config.LogLevel, "cli")
logger, err := logging.ConfigureLog(config.LogFile, config.LogLevel, "cli", config.LogPrettyFormat)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't configure main logger: %v\n", err)
os.Exit(1)
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"

"github.com/moira-alert/moira/database/redis"
"github.com/moira-alert/moira/logging/go-logging"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"

"github.com/moira-alert/moira"

Expand All @@ -18,7 +18,7 @@ func TestUpdateUsers(t *testing.T) {
}

conf := getDefault()
logger, err := logging.ConfigureLog(conf.LogFile, conf.LogLevel, "cli")
logger, err := logging.ConfigureLog(conf.LogFile, conf.LogLevel, "cli", conf.LogPrettyFormat)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ func (graphiteConfig *GraphiteConfig) GetSettings() metrics.GraphiteRegistryConf

// LoggerConfig is logger settings structure that initialises at the start of moira
type LoggerConfig struct {
LogFile string `yaml:"log_file"`
LogLevel string `yaml:"log_level"`
LogFile string `yaml:"log_file"`
LogLevel string `yaml:"log_level"`
LogPrettyFormat bool `yaml:"log_pretty_format"`
}

// TelemetryConfig is settings for listener, pprof, graphite
Expand Down
5 changes: 3 additions & 2 deletions cmd/filter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ func getDefault() config {
MetricsTTL: "1h",
},
Logger: cmd.LoggerConfig{
LogFile: "stdout",
LogLevel: "info",
LogFile: "stdout",
LogLevel: "info",
LogPrettyFormat: false,
},
Filter: filterConfig{
Listen: ":2003",
Expand Down
4 changes: 2 additions & 2 deletions cmd/filter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/moira-alert/moira/filter/heartbeat"
matchedmetrics "github.com/moira-alert/moira/filter/matched_metrics"
"github.com/moira-alert/moira/filter/patterns"
"github.com/moira-alert/moira/logging/go-logging"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
"github.com/moira-alert/moira/metrics"
"github.com/xiam/to"
)
Expand Down Expand Up @@ -59,7 +59,7 @@ func main() {
os.Exit(1)
}

logger, err = logging.ConfigureLog(config.Logger.LogFile, config.Logger.LogLevel, serviceName)
logger, err = logging.ConfigureLog(config.Logger.LogFile, config.Logger.LogLevel, serviceName, config.Logger.LogPrettyFormat)
if err != nil {
fmt.Fprintf(os.Stderr, "Can not configure log: %s\n", err.Error())
os.Exit(1)
Expand Down
5 changes: 3 additions & 2 deletions cmd/notifier/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ func getDefault() config {
},

Logger: cmd.LoggerConfig{
LogFile: "stdout",
LogLevel: "info",
LogFile: "stdout",
LogLevel: "info",
LogPrettyFormat: false,
},
Notifier: notifierConfig{
SenderTimeout: "10s",
Expand Down
4 changes: 2 additions & 2 deletions cmd/notifier/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/moira-alert/moira"
"github.com/moira-alert/moira/cmd"
"github.com/moira-alert/moira/database/redis"
"github.com/moira-alert/moira/logging/go-logging"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
"github.com/moira-alert/moira/metrics"
"github.com/moira-alert/moira/notifier"
"github.com/moira-alert/moira/notifier/events"
Expand Down Expand Up @@ -60,7 +60,7 @@ func main() {
os.Exit(1)
}

logger, err = logging.ConfigureLog(config.Logger.LogFile, config.Logger.LogLevel, serviceName)
logger, err = logging.ConfigureLog(config.Logger.LogFile, config.Logger.LogLevel, serviceName, config.Logger.LogPrettyFormat)
if err != nil {
fmt.Fprintf(os.Stderr, "Can not configure log: %s\n", err.Error())
os.Exit(1)
Expand Down
6 changes: 3 additions & 3 deletions database/redis/bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
. "github.com/smartystreets/goconvey/convey"

"github.com/moira-alert/moira/database"
"github.com/moira-alert/moira/logging/go-logging"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
)

func TestBotDataStoring(t *testing.T) {
logger, _ := logging.ConfigureLog("stdout", "info", "test")
logger, _ := logging.ConfigureLog("stdout", "info", "test", true)
dataBase := newTestDatabase(logger, config)
dataBase.flush()
defer dataBase.flush()
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestBotDataStoring(t *testing.T) {
}

func TestBotDataStoringErrorConnection(t *testing.T) {
logger, _ := logging.ConfigureLog("stdout", "info", "test")
logger, _ := logging.ConfigureLog("stdout", "info", "test", true)
dataBase := newTestDatabase(logger, emptyConfig)
dataBase.flush()
defer dataBase.flush()
Expand Down
3 changes: 2 additions & 1 deletion database/redis/contact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"fmt"
"testing"

"github.com/op/go-logging"
. "github.com/smartystreets/goconvey/convey"

"github.com/moira-alert/moira"
"github.com/moira-alert/moira/database"
logging "github.com/moira-alert/moira/logging/zerolog_adapter"
)

var user1 = "user1"
var user2 = "user2"

func TestContacts(t *testing.T) {
logger, _ := logging.GetLogger("dataBase")

dataBase := newTestDatabase(logger, config)
dataBase.flush()
defer dataBase.flush()
Expand Down
Loading

0 comments on commit ec3e4fb

Please sign in to comment.