Skip to content

Commit

Permalink
Implement log level and format configuration through YAML (resolves k…
Browse files Browse the repository at this point in the history
…uskoman#224) (kuskoman#225)

* Implement log level and format

* Add tests
  • Loading branch information
satk0 authored Jan 16, 2024
1 parent 284979d commit 0ef6a94
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 42 deletions.
27 changes: 12 additions & 15 deletions cmd/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"os"
"strconv"

"github.com/joho/godotenv"
"github.com/joho/godotenv"
"github.com/kuskoman/logstash-exporter/collectors"
"github.com/kuskoman/logstash-exporter/config"
"github.com/kuskoman/logstash-exporter/server"
Expand All @@ -24,25 +24,22 @@ func main() {
return
}

warn := godotenv.Load()
logger, err := config.SetupSlog()
warn := godotenv.Load()
if warn != nil {
log.Printf("failed to load .env file: %s", warn)
}

exporterConfig, err := config.GetConfig(config.ExporterConfigLocation)
if err != nil {
if warn != nil {
log.Printf("failed to load .env file: %s", warn)
}
log.Fatalf("failed to get exporter config: %s", err)
os.Exit(1)
}

logger, err := config.SetupSlog(exporterConfig.Logging.Level, exporterConfig.Logging.Format)
if err != nil {
log.Fatalf("failed to setup slog: %s", err)
} else {
slog.SetDefault(logger)
if warn != nil {
slog.Warn("failed to load .env file", "err", warn)
}
}

exporterConfig, err := config.GetConfig(config.ExporterConfigLocation)
if err != nil {
slog.Error("failed to get exporter config", "err", err)
os.Exit(1)
}

host := exporterConfig.Server.Host
Expand Down
9 changes: 8 additions & 1 deletion config/exporter_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
defaultConfigLocation = "config.yml"
defaultPort = 9198
defaultLogLevel = "info"
defaultLogFormat = "text"
defaultLogstashURL = "http://localhost:9600"
defaultHttpTimeout = time.Second * 2
)
Expand Down Expand Up @@ -46,7 +47,8 @@ type ServerConfig struct {

// LoggingConfig represents the logging configuration
type LoggingConfig struct {
Level string `yaml:"level"`
Level string `yaml:"level"`
Format string `yaml:"format"`
}

// Config represents the overall configuration loaded from the YAML file
Expand Down Expand Up @@ -88,6 +90,11 @@ func mergeWithDefault(config *Config) *Config {
config.Logging.Level = defaultLogLevel
}

if config.Logging.Format == "" {
slog.Debug("using default log format", "format", defaultLogLevel)
config.Logging.Format = defaultLogFormat
}

if len(config.Logstash.Servers) == 0 {
slog.Debug("using default logstash server", "url", defaultLogstashURL)
config.Logstash.Servers = append(config.Logstash.Servers, &LogstashServer{
Expand Down
11 changes: 11 additions & 0 deletions config/exporter_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func TestMergeWithDefault(t *testing.T) {
if mergedConfig.Logging.Level != defaultLogLevel {
t.Errorf("expected level to be %v, got %v", defaultLogLevel, mergedConfig.Logging.Level)
}
if mergedConfig.Logging.Format != defaultLogFormat {
t.Errorf("expected format to be %v, got %v", defaultLogFormat, mergedConfig.Logging.Format)
}
if mergedConfig.Logstash.Servers[0].Host != defaultLogstashURL {
t.Errorf("expected URL to be %v, got %v", defaultLogstashURL, mergedConfig.Logstash.Servers[0].Host)
}
Expand All @@ -88,6 +91,9 @@ func TestMergeWithDefault(t *testing.T) {
if mergedConfig.Logging.Level != defaultLogLevel {
t.Errorf("expected level to be %v, got %v", defaultLogLevel, mergedConfig.Logging.Level)
}
if mergedConfig.Logging.Format != defaultLogFormat {
t.Errorf("expected format to be %v, got %v", defaultLogFormat, mergedConfig.Logging.Format)
}
if mergedConfig.Logstash.Servers[0].Host != defaultLogstashURL {
t.Errorf("expected URL to be %v, got %v", defaultLogstashURL, mergedConfig.Logstash.Servers[0].Host)
}
Expand All @@ -105,6 +111,7 @@ func TestMergeWithDefault(t *testing.T) {
},
Logging: LoggingConfig{
Level: "debug",
Format: "json",
},
Logstash: LogstashConfig{
Servers: []*LogstashServer{
Expand All @@ -125,6 +132,10 @@ func TestMergeWithDefault(t *testing.T) {
t.Errorf("expected level to be %v, got %v", "debug", mergedConfig.Logging.Level)
}

if mergedConfig.Logging.Format != "json" {
t.Errorf("expected format to be %v, got %v", "json", mergedConfig.Logging.Format)
}

if mergedConfig.Logstash.Servers[0].Host != "http://localhost:9601" {
t.Errorf("expected URL to be %v, got %v", "http://localhost:9601", mergedConfig.Logstash.Servers[0].Host)
}
Expand Down
7 changes: 0 additions & 7 deletions config/http_config.go

This file was deleted.

13 changes: 4 additions & 9 deletions config/slog_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,17 @@ import (
"strings"
)

var (
LogLevel = getEnvWithDefault("LOG_LEVEL", "info")
LogFormat = getEnvWithDefault("LOG_FORMAT", "text")
var ErrUnknownLogFormat = fmt.Errorf("unknown log format")

ErrUnknownLogFormat = fmt.Errorf("unknown log format")
)

func SetupSlog() (*slog.Logger, error) {
func SetupSlog(logLevel string, logFormat string) (*slog.Logger, error) {
level := slog.LevelInfo
err := level.UnmarshalText([]byte(LogLevel))
err := level.UnmarshalText([]byte(logLevel))
if err != nil {
return nil, err
}

var handler slog.Handler
switch strings.ToLower(LogFormat) {
switch strings.ToLower(logFormat) {
case "text":
handler = slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
Expand Down
25 changes: 15 additions & 10 deletions config/slog_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import (

func TestSetupSlog(t *testing.T) {
t.Run("Wrong type of log level", func(t *testing.T) {
LogLevel = "infox"
logger, err := SetupSlog()
const LogLevel = "infox"
const LogFormat = defaultLogFormat
logger, err := SetupSlog(LogLevel, LogFormat)
if logger != nil {
t.Errorf("Expected logger to be nil, got %s\"", logger)
}
Expand All @@ -17,8 +18,9 @@ func TestSetupSlog(t *testing.T) {
}
})
t.Run("Wrong type of log level", func(t *testing.T) {
LogLevel = "warn"
logger, err := SetupSlog()
const LogLevel = "warn"
const LogFormat = defaultLogFormat
logger, err := SetupSlog(LogLevel, LogFormat)
if err != nil {
t.Errorf("Expected error to be nil, got %s\"", err)
}
Expand All @@ -27,8 +29,9 @@ func TestSetupSlog(t *testing.T) {
}
})
t.Run("Wrong type of log format", func(t *testing.T) {
LogFormat = "test"
logger, err := SetupSlog()
const LogLevel = defaultLogLevel
const LogFormat = "test"
logger, err := SetupSlog(LogLevel, LogFormat)
if logger != nil {
t.Errorf("Expected logger to be nil, got %s\"", logger)
}
Expand All @@ -37,8 +40,9 @@ func TestSetupSlog(t *testing.T) {
}
})
t.Run("Correct type of log format", func(t *testing.T) {
LogFormat = "text"
logger, err := SetupSlog()
const LogLevel = defaultLogLevel
const LogFormat = defaultLogFormat
logger, err := SetupSlog(LogLevel, LogFormat)
if err != nil {
t.Errorf("Expected error to be nil, got %s\"", err)
}
Expand All @@ -47,8 +51,9 @@ func TestSetupSlog(t *testing.T) {
}
})
t.Run("Json type of log format", func(t *testing.T) {
LogFormat = "json"
logger, err := SetupSlog()
const LogLevel = defaultLogLevel
const LogFormat = "json"
logger, err := SetupSlog(LogLevel, LogFormat)
if err != nil {
t.Errorf("Expected error to be nil, got %s\"", err)
}
Expand Down

0 comments on commit 0ef6a94

Please sign in to comment.