Skip to content

Commit 10cf1fc

Browse files
authored
[logp] Default logging config logs to file (#208)
The default log configuration must log to files. A test is added to ensure the default logging configuration logs to files as well as some other default fields are correctly set.
1 parent 212a3be commit 10cf1fc

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

logp/config.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ const (
7171
// Beat is supposed to be run within.
7272
func DefaultConfig(environment Environment) Config {
7373
return Config{
74-
Level: defaultLevel,
74+
Level: defaultLevel,
75+
ToFiles: true,
7576
Files: FileConfig{
7677
MaxSize: 10 * 1024 * 1024,
7778
MaxBackups: 7,

logp/defaults_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package logp_test
19+
20+
import (
21+
"encoding/json"
22+
"fmt"
23+
"os"
24+
"path/filepath"
25+
"runtime"
26+
"testing"
27+
28+
"github.com/elastic/elastic-agent-libs/logp"
29+
)
30+
31+
// TestDefaultConfig tests the default config ensuring the default
32+
// behaviour is to log to files
33+
func TestDefaultConfig(t *testing.T) {
34+
cfg := logp.DefaultConfig(logp.DefaultEnvironment)
35+
36+
// Set cfg.Beat to avoid a log file like '-202405029.ndjson'
37+
cfg.Beat = t.Name()
38+
39+
// Set Files.Path to an empty folder so we can assert the
40+
// creation of the log file without being affected by other
41+
// test runs.
42+
cfg.Files.Path = t.TempDir()
43+
44+
if err := logp.Configure(cfg); err != nil {
45+
t.Fatalf("did not expect an error calling logp.Configure: %s", err)
46+
}
47+
48+
// Get the logger and log anything
49+
logger := logp.L()
50+
defer logger.Close()
51+
52+
logger.Info("foo")
53+
_, fileName, lineNum, _ := runtime.Caller(0)
54+
lineNum-- // We want the line number from the log
55+
fileName = filepath.Base(fileName)
56+
57+
// Assert the log file was created
58+
glob := fmt.Sprintf("%s-*.ndjson", t.Name())
59+
glob = filepath.Join(cfg.Files.Path, glob)
60+
logFiles, err := filepath.Glob(glob)
61+
if err != nil {
62+
t.Fatalf("could not list files for glob '%s', err: %s", glob, err)
63+
}
64+
65+
if len(logFiles) < 1 {
66+
t.Fatalf("did not find any log file")
67+
}
68+
69+
data, err := os.ReadFile(logFiles[0])
70+
if err != nil {
71+
t.Fatalf("cannot open file '%s', err: %s", logFiles[0], err)
72+
}
73+
74+
logEntry := struct {
75+
LogLevel string `json:"log.level"`
76+
LogOrigin struct {
77+
FileName string `json:"file.name"`
78+
FileLine int `json:"file.line"`
79+
} `json:"log.origin"`
80+
Message string `json:"message"`
81+
ServiceName string `json:"service.name"`
82+
}{}
83+
if err := json.Unmarshal(data, &logEntry); err != nil {
84+
t.Fatalf("cannot unmarshal log entry: %s", err)
85+
}
86+
87+
if got, expect := logEntry.Message, "foo"; got != expect {
88+
t.Errorf("expecting message '%s', got '%s'", expect, got)
89+
}
90+
91+
if got, expect := logEntry.LogLevel, "info"; got != expect {
92+
t.Errorf("expecting level '%s', got '%s'", expect, got)
93+
}
94+
95+
if got, expect := logEntry.ServiceName, t.Name(); got != expect {
96+
t.Errorf("expecting service name '%s', got '%s'", expect, got)
97+
}
98+
99+
if got, expect := filepath.Base(logEntry.LogOrigin.FileName), fileName; got != expect {
100+
t.Errorf("expecting log.origin.file.name '%s', got '%s'", expect, got)
101+
}
102+
103+
if got, expect := logEntry.LogOrigin.FileLine, lineNum; got != expect {
104+
t.Errorf("expecting log.origin.file.line '%d', got '%d'", expect, got)
105+
}
106+
107+
if t.Failed() {
108+
t.Log("Original log entry:")
109+
t.Log(string(data))
110+
}
111+
}

0 commit comments

Comments
 (0)