From 6634efed32ab399a14f4f01c0f8c932516249a24 Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Wed, 9 Oct 2024 11:02:06 -0300 Subject: [PATCH] logp: don't write to files by default if running in a container environment (#236) * fix: don't write to files by default if running in a container environment * fix gosec lint * systemd should also log to stderr * reuse code between tests --- logp/config.go | 15 +++++++++-- logp/defaults_test.go | 58 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/logp/config.go b/logp/config.go index 87c8e8c..bdc4e83 100644 --- a/logp/config.go +++ b/logp/config.go @@ -70,9 +70,20 @@ const ( // DefaultConfig returns the default config options for a given environment the // Beat is supposed to be run within. func DefaultConfig(environment Environment) Config { + toFiles := true + toStderr := false + + // For container and systemd environments, we don't write to files by default. + switch environment { + case ContainerEnvironment, SystemdEnvironment: + toFiles = false + toStderr = true + } + return Config{ - Level: defaultLevel, - ToFiles: true, + Level: defaultLevel, + ToFiles: toFiles, + ToStderr: toStderr, Files: FileConfig{ MaxSize: 10 * 1024 * 1024, MaxBackups: 7, diff --git a/logp/defaults_test.go b/logp/defaults_test.go index aeec9bc..fa15e6c 100644 --- a/logp/defaults_test.go +++ b/logp/defaults_test.go @@ -19,9 +19,11 @@ package logp_test import ( "bufio" + "bytes" "encoding/json" "fmt" "os" + "os/exec" "path/filepath" "runtime" "sort" @@ -117,6 +119,62 @@ func TestDefaultConfig(t *testing.T) { } } +func TestDefaultConfigContainerLogsToStderr(t *testing.T) { + runTestEnvStderr(t, logp.ContainerEnvironment) +} + +func TestDefaultConfigSystemdLogsToStderr(t *testing.T) { + runTestEnvStderr(t, logp.SystemdEnvironment) +} + +func runTestEnvStderr(t *testing.T, envType logp.Environment) { + switch runtime.GOOS { + case "wasip1", "js", "ios": + t.Skipf("cannot exec subprocess on %s/%s", runtime.GOOS, runtime.GOARCH) + } + + if os.Getenv("TEST_DEFAULT_CONFIG_STDERR") != "1" { + cmd := exec.Command(os.Args[0], fmt.Sprintf("-test.run=^%s$", t.Name()), "-test.v") //nolint:gosec // This is intentionally running a subprocess + cmd.Env = append(cmd.Env, "TEST_DEFAULT_CONFIG_STDERR=1") + + var stderr bytes.Buffer + cmd.Stderr = &stderr + + err := cmd.Run() + data := stderr.Bytes() + assert.NoError(t, err, "command failed with error: %s\nstderr: %s", err, data) + t.Logf("output:\n%s", data) + + logEntry := struct { + LogLevel string `json:"log.level"` + LogOrigin struct { + FileName string `json:"file.name"` + FileLine int `json:"file.line"` + } `json:"log.origin"` + Message string `json:"message"` + }{} + + assert.NoError(t, json.Unmarshal(data, &logEntry), "cannot unmarshal log entry from stderr") + + assert.Equal(t, "info", logEntry.LogLevel) + assert.Equal(t, "foo", logEntry.Message) + + _, fileName, _, _ := runtime.Caller(0) + expectedFileName := filepath.Base(fileName) + gotFileName := filepath.Base(logEntry.LogOrigin.FileName) + assert.Equal(t, expectedFileName, gotFileName) + + return + } + + // This is running in a separate process to make sure we capture stderr. + cfg := logp.DefaultConfig(envType) + assert.NoError(t, logp.Configure(cfg)) + logger := logp.L() + defer logger.Close() + logger.Info("foo") +} + func TestWith(t *testing.T) { tempDir := t.TempDir()