Skip to content

Commit

Permalink
fix: don't write to files by default if running in a container enviro…
Browse files Browse the repository at this point in the history
…nment
  • Loading branch information
mauri870 committed Oct 7, 2024
1 parent 196dace commit ce685aa
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
9 changes: 8 additions & 1 deletion logp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,16 @@ 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

// If running in a container environment, don't write to files by default.
if environment == ContainerEnvironment {
toFiles = false
}

return Config{
Level: defaultLevel,
ToFiles: true,
ToFiles: toFiles,
Files: FileConfig{
MaxSize: 10 * 1024 * 1024,
MaxBackups: 7,
Expand Down
55 changes: 55 additions & 0 deletions logp/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package logp_test

import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
Expand Down Expand Up @@ -117,6 +119,59 @@ func TestDefaultConfig(t *testing.T) {
}
}

func TestDefaultConfigContainer(t *testing.T) {
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_CONTAINER") != "1" {
cmd := exec.Command(os.Args[0], "-test.run=^TestDefaultConfigContainer$", "-test.v")

Check failure on line 129 in logp/defaults_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

G204: Subprocess launched with a potential tainted input or cmd arguments (gosec)

Check failure on line 129 in logp/defaults_test.go

View workflow job for this annotation

GitHub Actions / lint (linux)

G204: Subprocess launched with a potential tainted input or cmd arguments (gosec)

Check failure on line 129 in logp/defaults_test.go

View workflow job for this annotation

GitHub Actions / lint (darwin)

G204: Subprocess launched with a potential tainted input or cmd arguments (gosec)
cmd.Env = append(cmd.Env, "TEST_DEFAULT_CONFIG_CONTAINER=1")

var stderr bytes.Buffer
cmd.Stderr = &stderr

err := cmd.Run()
data := stderr.Bytes()
if err != nil {
t.Fatalf("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. By default the
// container environment should be logging to stderr.
cfg := logp.DefaultConfig(logp.ContainerEnvironment)
if err := logp.Configure(cfg); err != nil {
t.Errorf("logp.Configure failed: %s", err.Error())
}
logger := logp.L()
defer logger.Close()
logger.Info("foo")
}

func TestWith(t *testing.T) {
tempDir := t.TempDir()

Expand Down

0 comments on commit ce685aa

Please sign in to comment.