|
| 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