Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(logging): Implement early logging #15629

Merged
merged 11 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions agent/accumulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package agent
import (
"bytes"
"errors"
"log"
"os"
"testing"
"time"
Expand Down Expand Up @@ -49,8 +48,8 @@ func TestAddFields(t *testing.T) {

func TestAccAddError(t *testing.T) {
errBuf := bytes.NewBuffer(nil)
log.SetOutput(errBuf)
defer log.SetOutput(os.Stderr)
logger.RedirectLogging(errBuf)
defer logger.RedirectLogging(os.Stderr)

metrics := make(chan telegraf.Metric, 10)
defer close(metrics)
Expand Down Expand Up @@ -157,5 +156,5 @@ func (tm *TestMetricMaker) MakeMetric(metric telegraf.Metric) telegraf.Metric {
}

func (tm *TestMetricMaker) Log() telegraf.Logger {
return logger.NewLogger("TestPlugin", "test", "")
return logger.New("TestPlugin", "test", "")
}
5 changes: 0 additions & 5 deletions cmd/telegraf/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,6 @@ func runApp(args []string, outputBuffer io.Writer, pprof Server, c TelegrafConfi
return fmt.Errorf("unknown command %q", cCtx.Args().First())
}

err := logger.SetupLogging(&logger.Config{})
if err != nil {
return err
}

// Deprecated: Use execd instead
// Load external plugins, if requested.
if cCtx.String("plugin-directory") != "" {
Expand Down
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,11 @@ func (c *Config) LoadConfig(path string) error {

data, _, err := LoadConfigFileWithRetries(path, c.Agent.ConfigURLRetryAttempts)
if err != nil {
return fmt.Errorf("error loading config file %s: %w", path, err)
return fmt.Errorf("loading config file %s failed: %w", path, err)
}

if err = c.LoadConfigData(data); err != nil {
return fmt.Errorf("error loading config file %s: %w", path, err)
return fmt.Errorf("loading config file %s failed: %w", path, err)
}

return nil
Expand Down Expand Up @@ -904,7 +904,7 @@ func (c *Config) addSecretStore(name string, table *ast.Table) error {
return err
}

logger := logging.NewLogger("secretstores", name, "")
logger := logging.New("secretstores", name, "")
models.SetLoggerOnPlugin(store, logger)

if err := store.Init(); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ func TestConfig_BadOrdering(t *testing.T) {
require.Error(t, err, "bad ordering")
require.Equal(
t,
"error loading config file ./testdata/non_slice_slice.toml: error parsing http array, line 4: cannot unmarshal TOML array into string (need slice)",
"loading config file ./testdata/non_slice_slice.toml failed: error parsing http array, line 4: cannot unmarshal TOML array into string (need slice)",
err.Error(),
)
}
Expand Down Expand Up @@ -541,11 +541,11 @@ func TestConfig_URLLikeFileName(t *testing.T) {
// The error file not found error message is different on Windows
require.Equal(
t,
"error loading config file http:##www.example.com.conf: open http:##www.example.com.conf: The system cannot find the file specified.",
"loading config file http:##www.example.com.conf failed: open http:##www.example.com.conf: The system cannot find the file specified.",
err.Error(),
)
} else {
require.Equal(t, "error loading config file http:##www.example.com.conf: open http:##www.example.com.conf: no such file or directory", err.Error())
require.Equal(t, "loading config file http:##www.example.com.conf failed: open http:##www.example.com.conf: no such file or directory", err.Error())
}
}

Expand Down Expand Up @@ -639,7 +639,7 @@ func TestConfig_SerializerInterfaceNewFormat(t *testing.T) {
formatCfg := &cfg
formatCfg.DataFormat = format

logger := logging.NewLogger("serializers", format, "test")
logger := logging.New("serializers", format, "test")

var serializer telegraf.Serializer
if creator, found := serializers.Serializers[format]; found {
Expand Down Expand Up @@ -731,7 +731,7 @@ func TestConfig_SerializerInterfaceOldFormat(t *testing.T) {
formatCfg := &cfg
formatCfg.DataFormat = format

logger := logging.NewLogger("serializers", format, "test")
logger := logging.New("serializers", format, "test")

var serializer serializers.Serializer
if creator, found := serializers.Serializers[format]; found {
Expand Down Expand Up @@ -837,7 +837,7 @@ func TestConfig_ParserInterface(t *testing.T) {

expected := make([]telegraf.Parser, 0, len(formats))
for _, format := range formats {
logger := logging.NewLogger("parsers", format, "parser_test_new")
logger := logging.New("parsers", format, "parser_test_new")

creator, found := parsers.Parsers[format]
require.Truef(t, found, "No parser for format %q", format)
Expand Down Expand Up @@ -1043,7 +1043,7 @@ func TestConfig_ProcessorsWithParsers(t *testing.T) {

expected := make([]telegraf.Parser, 0, len(formats))
for _, format := range formats {
logger := logging.NewLogger("parsers", format, "processors_with_parsers")
logger := logging.New("parsers", format, "processors_with_parsers")

creator, found := parsers.Parsers[format]
require.Truef(t, found, "No parser for format %q", format)
Expand Down
2 changes: 1 addition & 1 deletion config/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func TestURLRetries3Fails(t *testing.T) {
}))
defer ts.Close()

expected := fmt.Sprintf("error loading config file %s: failed to fetch HTTP config: 404 Not Found", ts.URL)
expected := fmt.Sprintf("loading config file %s failed: failed to fetch HTTP config: 404 Not Found", ts.URL)

c := NewConfig()
err := c.LoadConfig(ts.URL)
Expand Down
1 change: 0 additions & 1 deletion docs/LICENSE_OF_DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ following works:
- github.com/influxdata/line-protocol [MIT License](https://github.com/influxdata/line-protocol/blob/v2/LICENSE)
- github.com/influxdata/tail [MIT License](https://github.com/influxdata/tail/blob/master/LICENSE.txt)
- github.com/influxdata/toml [MIT License](https://github.com/influxdata/toml/blob/master/LICENSE)
- github.com/influxdata/wlog [MIT License](https://github.com/influxdata/wlog/blob/master/LICENSE)
- github.com/intel/iaevents [Apache License 2.0](https://github.com/intel/iaevents/blob/main/LICENSE)
- github.com/intel/powertelemetry [Apache License 2.0](https://github.com/intel/powertelemetry/blob/main/LICENSE)
- github.com/jackc/chunkreader [MIT License](https://github.com/jackc/chunkreader/blob/master/LICENSE)
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ require (
github.com/influxdata/line-protocol/v2 v2.2.1
github.com/influxdata/tail v1.0.1-0.20221130111531-19b97bffd978
github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65
github.com/influxdata/wlog v0.0.0-20160411224016-7c63b0a71ef8
github.com/intel/iaevents v1.1.0
github.com/intel/powertelemetry v1.0.1
github.com/jackc/pgconn v1.14.3
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1621,8 +1621,6 @@ github.com/influxdata/tail v1.0.1-0.20221130111531-19b97bffd978 h1:SaKzm7QUHJcHO
github.com/influxdata/tail v1.0.1-0.20221130111531-19b97bffd978/go.mod h1:VeiWgI3qaGdJWust2fP27a6J+koITo/1c/UhxeOxgaM=
github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65 h1:vvyMtD5LTJc1W9sQKjDkAWdcg0478CszSdzlHtiAXCY=
github.com/influxdata/toml v0.0.0-20190415235208-270119a8ce65/go.mod h1:zApaNFpP/bTpQItGZNNUMISDMDAnTXu9UqJ4yT3ocz8=
github.com/influxdata/wlog v0.0.0-20160411224016-7c63b0a71ef8 h1:W2IgzRCb0L9VzMujq/QuTaZUKcH8096jWwP519mHN6Q=
github.com/influxdata/wlog v0.0.0-20160411224016-7c63b0a71ef8/go.mod h1:/2NMgWB1DHM1ti/gqhOlg+LJeBVk6FqR5aVGYY0hlwI=
github.com/intel/iaevents v1.1.0 h1:FzxMBfXk/apG2EUXUCfaq3gUQ+q+TgZ1HNMjjUILUGE=
github.com/intel/iaevents v1.1.0/go.mod h1:CyUUzXw0lHRCsmyyF7Pwco9Y7NiTNQUUlcJ7RJAazKs=
github.com/intel/powertelemetry v1.0.1 h1:a35pZbqOnJlEYGEPXM+YKtetu6D6dJD4Jb4GS4Zetxs=
Expand Down
45 changes: 30 additions & 15 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ package telegraf
// LogLevel denotes the level for logging
type LogLevel int

const (
// None means nothing is logged
None LogLevel = iota
// Error will log error messages
Error
// Warn will log error messages and warnings
Warn
// Info will log error messages, warnings and information messages
Info
// Debug will log all of the above and debugging messages issued by plugins
Debug
)

func (e LogLevel) String() string {
switch e {
case Error:
Expand All @@ -17,27 +30,29 @@ func (e LogLevel) String() string {
return "NONE"
}

const (
// None means nothing is logged
None LogLevel = iota
// Error will log error messages
Error
// Warn will log error messages and warnings
Warn
// Info will log error messages, warnings and information messages
Info
// Debug will log all of the above and debugging messages issued by plugins
Debug
)
func (e LogLevel) Indicator() string {
switch e {
case Error:
return "E!"
case Warn:
return "W!"
case Info:
return "I!"
case Debug:
return "D!"
}
return "U!"
}

func (e LogLevel) Includes(level LogLevel) bool {
return e >= level
}

// Logger defines an plugin-related interface for logging.
type Logger interface {
// Level returns the configured log-level of the logger
Level() LogLevel

// RegisterErrorCallback registers a callback triggered when logging errors
RegisterErrorCallback(func())

// Errorf logs an error message, patterned after log.Printf.
Errorf(format string, args ...interface{})
// Error logs an error message, patterned after log.Print.
Expand Down
Loading
Loading