-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[logp] Default logging config logs to file
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.
- Loading branch information
Showing
2 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package logp_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/elastic/elastic-agent-libs/logp" | ||
) | ||
|
||
// TestDefaultConfig tests the default config ensuring the default | ||
// behaviour is to log to files | ||
func TestDefaultConfig(t *testing.T) { | ||
cfg := logp.DefaultConfig(logp.DefaultEnvironment) | ||
|
||
// Set cfg.Beat to avoid a log file like '-202405029.ndjson' | ||
cfg.Beat = t.Name() | ||
|
||
// Set Files.Path to an empty folder so we can assert the | ||
// creation of the log file without being affected by other | ||
// test runs. | ||
cfg.Files.Path = t.TempDir() | ||
|
||
if err := logp.Configure(cfg); err != nil { | ||
t.Fatalf("did not expect an error calling logp.Configure: %s", err) | ||
} | ||
|
||
// Get the logger and log anything | ||
logger := logp.L() | ||
logger.Info("foo") | ||
_, fileName, lineNum, _ := runtime.Caller(0) | ||
lineNum-- // We want the line number from the log | ||
fileName = filepath.Base(fileName) | ||
|
||
// Assert the log file was created | ||
glob := fmt.Sprintf("%s-*.ndjson", t.Name()) | ||
glob = filepath.Join(cfg.Files.Path, glob) | ||
logFiles, err := filepath.Glob(glob) | ||
if err != nil { | ||
t.Fatalf("could not list files for glob '%s', err: %s", glob, err) | ||
} | ||
|
||
if len(logFiles) < 1 { | ||
t.Fatalf("did not find any log file") | ||
} | ||
|
||
data, err := os.ReadFile(logFiles[0]) | ||
if err != nil { | ||
t.Fatalf("cannot open file '%s', err: %s", logFiles[0], err) | ||
} | ||
|
||
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"` | ||
ServiceName string `json:"service.name"` | ||
}{} | ||
if err := json.Unmarshal(data, &logEntry); err != nil { | ||
t.Fatalf("cannot unmarshal log entry: %s", err) | ||
} | ||
|
||
if got, expect := logEntry.Message, "foo"; got != expect { | ||
t.Errorf("expecting message '%s', got '%s'", expect, got) | ||
} | ||
|
||
if got, expect := logEntry.LogLevel, "info"; got != expect { | ||
t.Errorf("expecting level '%s', got '%s'", expect, got) | ||
} | ||
|
||
if got, expect := logEntry.ServiceName, t.Name(); got != expect { | ||
t.Errorf("expecting service name '%s', got '%s'", expect, got) | ||
} | ||
|
||
if got, expect := filepath.Base(logEntry.LogOrigin.FileName), fileName; got != expect { | ||
t.Errorf("expecting log.origin.file.name '%s', got '%s'", expect, got) | ||
} | ||
|
||
if got, expect := logEntry.LogOrigin.FileLine, lineNum; got != expect { | ||
t.Errorf("expecting log.origin.file.line '%d', got '%d'", expect, got) | ||
} | ||
|
||
if t.Failed() { | ||
t.Log("Original log entry:") | ||
t.Log(string(data)) | ||
} | ||
} |