Skip to content

Commit

Permalink
Correct plugin logging (#3185)
Browse files Browse the repository at this point in the history
* The plugin logger should use verbosity level

The log level is meant to be used only for log files.

Signed-off-by: Kim Christensen <kimworking@gmail.com>

* Correctly detect log level from plugin logs

The constants used before was numbers, causing the writing to always
fall back to writing as debug

Signed-off-by: Kim Christensen <kimworking@gmail.com>

* Correct the documentation

Signed-off-by: Kim Christensen <kimworking@gmail.com>

* Do not log information messages from the plugin infrastructure

Signed-off-by: Kim Christensen <kimworking@gmail.com>

---------

Signed-off-by: Kim Christensen <kimworking@gmail.com>
Co-authored-by: schristoff <28318173+schristoff@users.noreply.github.com>
  • Loading branch information
kichristensen and schristoff authored Aug 4, 2024
1 parent 6292541 commit ddf7d68
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/content/docs/configuration/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ The following log settings are available:
| ---------------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| logs.log-to-file | PORTER_LOGS_LOG_TO_FILE | Specifies if a logfile should be written for each command. |
| logs.structured | PORTER_LOGS_STRUCTURED | Specifies if the logs printed to the console should include a timestamp and log level |
| logs.level | PORTER_LOGS_LEVEL | Filters the logs to the specified level and higher. The log level controls both the logs written to file, and the logs output to the console when porter is run. Allowed values are: debug, info, warn, error. |
| logs.level | PORTER_LOGS_LEVEL | Filters the logs to the specified level and higher. The log level controls the logs written to file when porter is run. Allowed values are: debug, info, warn, error. |

#### Telemetry

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ The following log settings are available:
| ---------------- | ----------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| logs.log-to-file | PORTER_LOGS_LOG_TO_FILE | Specifies if a logfile should be written for each command. |
| logs.structured | PORTER_LOGS_STRUCTURED | Specifies if the logs printed to the console should include a timestamp and log level |
| logs.level | PORTER_LOGS_LEVEL | Filters the logs to the specified level and higher. The log level controls both the logs written to file, and the logs output to the console when porter is run. Allowed values are: debug, info, warn, error. |
| logs.level | PORTER_LOGS_LEVEL | Filters the logs to the specified level and higher. The log level controls the logs written to file when porter is run. Allowed values are: debug, info, warn, error. |

#### Telemetry

Expand Down
8 changes: 4 additions & 4 deletions pkg/plugins/pluggable/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (c *PluginConnection) Start(ctx context.Context, pluginCfg io.Reader) error
logger := hclog.New(&hclog.LoggerOptions{
Name: "porter",
Output: c.logsWriter,
Level: hclog.Debug,
Level: hclog.Warn,
JSONFormat: true,
})
c.client = plugin.NewClient(&plugin.ClientConfig{
Expand Down Expand Up @@ -338,11 +338,11 @@ func (c *PluginConnection) collectPluginLogs(ctx context.Context) {
}

switch pluginLog["@level"] {
case hclog.Error:
case "error":
_ = span.Error(fmt.Errorf(msg))
case hclog.Warn:
case "warn":
span.Warn(msg)
case hclog.Info:
case "info":
span.Infof(msg)
default:
span.Debug(msg)
Expand Down
68 changes: 68 additions & 0 deletions pkg/portercontext/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"runtime"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -63,3 +64,70 @@ func TestContext_LogToFile(t *testing.T) {
c.CompareGoldenFile("testdata/expected-output.txt", c.GetAllLogs())
}
}

func TestContext_PluginVerbosityLevel(t *testing.T) {
testcases := []struct {
name string
verbosityLevel zapcore.Level
wantNumberOfLogLines int
}{
{"debug level", zapcore.DebugLevel, 4},
{"info level", zapcore.InfoLevel, 3},
{"warn level", zapcore.WarnLevel, 2},
{"error level", zapcore.ErrorLevel, 1},
}

for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
c := NewTestContext(t)
c.IsInternalPlugin = true
c.ConfigureLogging(context.Background(), LogConfiguration{
Verbosity: tc.verbosityLevel,
})

_, log := c.StartRootSpan(context.Background(), t.Name())
log.Debug("debug log")
log.Info("info log")
log.Warn("warning log")
//throwing away error here because it is a test
// we do not return it
_ = log.Error(errors.New("error log"))

log.EndSpan()
c.Close()

lines := strings.Split(c.captureLogs.String(), "\n")
lines = lines[:len(lines)-1] // Remove last line as it will be empty
require.Len(t, lines, tc.wantNumberOfLogLines)
})
}
}

func TestContext_PluginLogCollectorLevel(t *testing.T) {
c := NewTestContext(t)
c.IsInternalPlugin = true
c.ConfigureLogging(context.Background(), LogConfiguration{
Verbosity: zapcore.DebugLevel,
})

_, log := c.StartRootSpan(context.Background(), t.Name())
log.Debug("debug log")
log.Info("info log")
log.Warn("warning log")
//throwing away error here because it is a test
// we do not return it
_ = log.Error(errors.New("error log"))

log.EndSpan()
c.Close()

lines := strings.Split(c.captureLogs.String(), "\n")
require.Contains(t, lines[0], "\"@level\":\"debug\"")
require.Contains(t, lines[0], "\"@message\":\"debug log\"")
require.Contains(t, lines[1], "\"@level\":\"info\"")
require.Contains(t, lines[1], "\"@message\":\"info log\"")
require.Contains(t, lines[2], "\"@level\":\"warn\"")
require.Contains(t, lines[2], "\"@message\":\"warning log\"")
require.Contains(t, lines[3], "\"@level\":\"error\"")
require.Contains(t, lines[3], "\"@message\":\"error log\"")
}
2 changes: 1 addition & 1 deletion pkg/portercontext/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (c *Context) makePluginLogger(pluginKey string, cfg LogConfiguration) zapco

enc := zap.NewProductionEncoderConfig()
jsonEncoder := zapcore.NewJSONEncoder(enc)
return zapcore.NewCore(jsonEncoder, zapcore.AddSync(pluginLogger), cfg.LogLevel)
return zapcore.NewCore(jsonEncoder, zapcore.AddSync(pluginLogger), cfg.Verbosity)
}

// Accepts zap log commands and translates them to a format that hclog understands
Expand Down

0 comments on commit ddf7d68

Please sign in to comment.