summaryrefslogtreecommitdiff
path: root/internal/io/dlog/loggers/fout.go
blob: 0d3fde503af58ff86d095d404936e2e33b1b9414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package loggers

import (
	"context"
	"sync"
	"time"

	"github.com/mimecast/dtail/internal/config"
)

// fout logs to both a file and stdout. It is the default client logger.
//
// The two things a client emits are deliberately split at this seam:
//   - Diagnostics (connection INFO/WARN/ERROR/etc.) arrive via Log/LogWithColors
//     and are ALWAYS written to both stdout and the file — they are the small,
//     useful audit trail the daily log file is meant to keep.
//   - Retrieved payload (the bulk dcat/dgrep/dtail output) arrives via
//     Raw/RawWithColors. It always reaches stdout/terminal, but it is teed to
//     the file only when logPayload is set (opt-in via --log-payload /
//     Client.LogPayload). By default the file receives no payload, so a bulk
//     dcat no longer silently grows the daily log file by the full payload size.
type fout struct {
	file       Logger
	stdout     Logger
	logPayload bool
}

// newFout builds the default client logger. Whether retrieved payload is teed
// to the file is decided once at construction from the client config.
func newFout(strategy Strategy) *fout {
	return newFoutWithSinks(newFile(strategy), newStdout(), clientLogPayloadEnabled())
}

// newFoutWithSinks builds a fout over injectable sinks and an explicit payload
// switch. Production uses newFout (concrete file+stdout, config-driven switch);
// tests inject fakes to assert that diagnostics always reach the file while
// payload reaches it only when opted in.
func newFoutWithSinks(file, stdout Logger, logPayload bool) *fout {
	return &fout{file: file, stdout: stdout, logPayload: logPayload}
}

// clientLogPayloadEnabled reports whether the client has opted in to teeing the
// full retrieved payload into the daily log file. Default (false) keeps only
// diagnostics in the file. config.Client is nil-guarded because a logger can be
// constructed in early/unit contexts before config.Setup has populated it.
func clientLogPayloadEnabled() bool {
	return config.Client != nil && config.Client.LogPayload
}

func (f *fout) Start(ctx context.Context, wg *sync.WaitGroup) {
	go func() {
		defer wg.Done()

		var wg2 sync.WaitGroup
		wg2.Add(2)
		f.file.Start(ctx, &wg2)
		f.stdout.Start(ctx, &wg2)
		wg2.Wait()
	}()
}

func (f *fout) Log(now time.Time, message string) {
	f.stdout.Log(now, message)
	f.file.Log(now, message)
}

func (f *fout) LogWithColors(now time.Time, message, coloredMessage string) {
	f.stdout.LogWithColors(now, "", coloredMessage)
	// The file logger does not support colors, so write the plain message via
	// Log (its LogWithColors would route to RawWithColors, which panics).
	f.file.Log(now, message)
}

// Raw writes retrieved payload. It always reaches stdout/terminal; it is teed
// to the file sink only when the client opted in via --log-payload /
// Client.LogPayload. By default the file is left payload-free.
func (f *fout) Raw(now time.Time, message string) {
	f.stdout.Raw(now, message)
	if f.logPayload {
		f.file.Raw(now, message)
	}
}

func (f *fout) RawWithColors(now time.Time, message, coloredMessage string) {
	f.stdout.RawWithColors(now, "", coloredMessage)
	// Same opt-in gate as Raw; the file gets the plain (uncolored) payload.
	if f.logPayload {
		f.file.Raw(now, message)
	}
}

// RawFileOnly tees retrieved payload into the daily log FILE sink only, never to
// stdout, honoring the same --log-payload / Client.LogPayload opt-in as Raw.
//
// It exists for the serverless direct-output path: that path writes
// payload straight to its own stdout sink and bypasses Raw entirely, so without
// this hook --log-payload would silently no longer tee payload to the file in
// serverless mode. The caller (the serverless output writer) already emits the
// payload bytes to stdout itself, so this method deliberately writes ONLY to the
// file to keep stdout byte-identical whether or not --log-payload is set.
func (f *fout) RawFileOnly(now time.Time, message string) {
	if f.logPayload {
		f.file.Raw(now, message)
	}
}

func (f *fout) Flush()  { f.stdout.Flush(); f.file.Flush() }
func (f *fout) Pause()  { f.stdout.Pause(); f.file.Pause() }
func (f *fout) Resume() { f.stdout.Resume(); f.file.Resume() }
func (f *fout) Rotate() { f.file.Rotate() }

func (fout) SupportsColors() bool { return true }