~bigbes/sr-ht-dolt

ref: 0638c16f7ac54e5ab2e398ac748c28a1475ebb04 sr-ht-dolt/cmd/doltsrht-migrate/logging.go -rw-r--r-- 2.9 KiB
0638c16f — Eugene Blikh migrate: install the instance's masking before brant can log a DSN 18 hours ago
                                                                                
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
package main

import (
	"log/slog"
	"os"

	"github.com/vaughan0/go-ini"

	"go.bigb.es/auxilia/scribe"

	"sourcecraft.dev/bigbes/sr-ht-ecore/logging"
)

// installLogger installs the process-wide slog handler, and it is the daemon's:
// cmd/doltsrht/logging.go builds the same scribe handler over the same
// sr-ht-ecore policy, so a migration and the daemon that starts after it are
// read the same way in the same journal. The file is named after that one for
// the same reason.
//
// Installing it at all is the point rather than the tidiness. brant reports a
// migration directory it cannot open with
// `slog.Error("failed to create provider", "datasource", a.DataSourceName, ...)`
// — the connection string, password and all, at ERR level, on the ordinary path
// an installed package takes before its first migration ships. Without this call
// that record goes to Go's built-in stderr handler, which knows no mask list, so
// the redaction sr-ht-ecore maintains never gets a chance to run. It was the
// install that was missing here, not the list: the daemon next door had been
// masking a DSN correctly for as long as this binary had been printing one.
//
// One thing no mask list can fix: brant's own cli.initLogger runs inside
// ProviderFromArgs, ahead of that record, and under --json it calls
// slog.SetDefault with a plain JSON handler of its own — which throws this one
// away and prints the DSN again. Nothing here passes --json and nothing should
// start; the default branch only redirects the std log package and leaves the
// installed handler alone, which is why the masking holds.
//
// The verbosity comes from DefaultsWithoutDebugFlag rather than the daemon's
// Defaults, and that is the one place the two differ: ecore reads a standalone
// -d out of os.Args as SourceHut's debug flag, while here -d is brant's
// --dialect and takes a value, so `doltsrht-migrate -d postgres up` would
// silently ask for debug logging as well. $LOG_LEVEL and [dolt.sr.ht]log-level
// still resolve, which is what an operator debugging an upgrade hook reaches
// for.
func installLogger(conf ini.File) *slog.Logger {
	opts := logging.DefaultsWithoutDebugFlag(conf, serviceName)

	return logging.Install(scribe.NewTintHandler(
		scribe.WithWriter(os.Stderr),
		scribe.WithLevel(opts.Level),
		scribe.WithSource(opts.AddSource),
		scribe.WithTimeFormat(opts.TimeFormat),
		// Colour is for a terminal; under systemd or a container's log
		// collector the escapes are noise in the journal.
		scribe.WithNoColor(!opts.Color),
		// The masks are keyed on the attribute path, not on the message, so
		// they cost nothing in prose and cannot be defeated by a sentence that
		// happens to contain the word "token". This binary holds exactly one
		// credential — the connection string — and brant is what prints it.
		scribe.WithMaskKeys(opts.MaskKeys...),
		scribe.WithMask(opts.MaskPattern, opts.MaskReplacement),
	))
}