94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package logger
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"git.zhangdeman.cn/zhangdeman/ginframework/diagnostic"
|
|
"git.zhangdeman.cn/zhangdeman/ginframework/fault"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestOutputLifecycleAndSafeFields(t *testing.T) {
|
|
directory := t.TempDir()
|
|
instance, err := ConfigureOutput(&OutputConfig{Name: "default", Path: directory, UseJSON: true})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if instance != Instance("default") {
|
|
t.Fatal("logger not shared")
|
|
}
|
|
if _, err := ConfigureOutput(&OutputConfig{Name: "default", Path: directory}); err == nil {
|
|
t.Fatal("replaced active logger")
|
|
}
|
|
source := map[string]any{"token": "private-value", "count": int64(9007199254740993)}
|
|
instance.Info("Write example data", Fields(source))
|
|
if err := CloseOutputs(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
files, err := filepath.Glob(filepath.Join(directory, "*"))
|
|
if err != nil || len(files) == 0 {
|
|
t.Fatal("log file missing")
|
|
}
|
|
body, err := os.ReadFile(files[0])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(body), "private-value") || !strings.Contains(string(body), "9007199254740993") {
|
|
t.Fatal("log values unsafe or imprecise")
|
|
}
|
|
if source["token"] != "private-value" {
|
|
t.Fatal("logging mutated source")
|
|
}
|
|
if err := CloseOutputs(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestOutputCreationPreservesCause(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "file")
|
|
if err := os.WriteFile(path, []byte("file"), 0600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, err := ConfigureOutput(&OutputConfig{Name: "invalid", Path: filepath.Join(path, "child")})
|
|
var (
|
|
output *OutputError
|
|
)
|
|
if !errors.As(err, &output) || errors.Unwrap(err) == nil {
|
|
t.Fatal("output failure lost cause")
|
|
}
|
|
}
|
|
|
|
func TestErrorContextUsesTrustedSnapshotWithoutCauseText(t *testing.T) {
|
|
directory := t.TempDir()
|
|
if _, err := ConfigureOutput(&OutputConfig{Name: "default", Path: directory, UseJSON: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ctx := diagnostic.With(context.Background(), diagnostic.Snapshot{TraceID: "trusted-trace", RequestID: "trusted-request", URI: "/safe"})
|
|
RecordErrorContext(ctx, "Read outbound response failed", fault.New(900709, "Read outbound response failed", errors.New("private-password")))
|
|
if err := CloseOutputs(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
files, err := filepath.Glob(filepath.Join(directory, "*"))
|
|
if err != nil || len(files) != 1 {
|
|
t.Fatal("log file missing")
|
|
}
|
|
raw, err := os.ReadFile(files[0])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var (
|
|
entry map[string]any
|
|
)
|
|
if err = json.Unmarshal(bytes.TrimSpace(raw), &entry); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if entry["trace_id"] != "trusted-trace" || entry["request_id"] != "trusted-request" || entry["uri"] != "/safe" || strings.Contains(string(raw), "private-password") {
|
|
t.Fatalf("unsafe or missing diagnostics: %s", raw)
|
|
}
|
|
}
|