Skip to content

Commit

Permalink
systemd should also log to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
mauri870 committed Oct 9, 2024
1 parent 3b10333 commit 399ae3f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
12 changes: 8 additions & 4 deletions logp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ const (
// Beat is supposed to be run within.
func DefaultConfig(environment Environment) Config {
toFiles := true
toStderr := false

// If running in a container environment, don't write to files by default.
if environment == ContainerEnvironment {
// 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: toFiles,
Level: defaultLevel,
ToFiles: toFiles,
ToStderr: toStderr,
Files: FileConfig{
MaxSize: 10 * 1024 * 1024,
MaxBackups: 7,
Expand Down
51 changes: 49 additions & 2 deletions logp/defaults_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,62 @@ func TestDefaultConfigContainer(t *testing.T) {
return
}

// This is running in a separate process. By default the
// container environment should be logging to stderr.
// This is running in a separate process.
cfg := logp.DefaultConfig(logp.ContainerEnvironment)
assert.NoError(t, logp.Configure(cfg))
logger := logp.L()
defer logger.Close()
logger.Info("foo")
}

func TestDefaultConfigSystemd(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_SYSTEMD") != "1" {
cmd := exec.Command(os.Args[0], "-test.run=^TestDefaultConfigSystemd$", "-test.v") //nolint:gosec // This is intentionally running a subprocess
cmd.Env = append(cmd.Env, "TEST_DEFAULT_CONFIG_SYSTEMD=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.
cfg := logp.DefaultConfig(logp.SystemdEnvironment)
assert.NoError(t, logp.Configure(cfg))
logger := logp.L()
defer logger.Close()
logger.Info("foo")
}

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

Expand Down

0 comments on commit 399ae3f

Please sign in to comment.