78 lines
2.6 KiB
Go
78 lines
2.6 KiB
Go
package servicekit
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/samcharles93/archie-core/internal/buildinfo"
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/telemetry"
|
|
"github.com/samcharles93/archie-core/internal/logging"
|
|
)
|
|
|
|
// Resolve reads the file config. A failure is printed to stderr because the
|
|
// file log destination is itself configuration that has not been read yet.
|
|
func Resolve(log *slog.Logger, cfgPath, overlayPath string) (*configuration.Loader, *configuration.Document, error) {
|
|
loader := configuration.New(log)
|
|
doc, err := loader.Resolve(cfgPath, overlayPath)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err)
|
|
return nil, nil, err
|
|
}
|
|
// Unknown TOML keys are otherwise discarded silently, so a typo would
|
|
// quietly do nothing.
|
|
if len(doc.UnknownKeys) > 0 {
|
|
log.Warn("config file has unrecognised keys; check for typos", "keys", doc.UnknownKeys)
|
|
}
|
|
return loader, doc, nil
|
|
}
|
|
|
|
// Logs is a service's configured log and the task log registry beside it.
|
|
type Logs struct {
|
|
Log *slog.Logger
|
|
Feed *logging.Feed
|
|
TaskLogs *logging.TaskRegistry
|
|
Closer io.Closer
|
|
}
|
|
|
|
// Logging opens the configured log for component and starts trace export. A
|
|
// file log that cannot be opened is reported and logging continues on stderr;
|
|
// a trace exporter that cannot be built fails startup. Closing flushes the
|
|
// exporter, then closes the log, so shutdown records are still written.
|
|
func Logging(ctx context.Context, cfg config.Config, component string) (Logs, error) {
|
|
stopTracing, err := telemetry.Start(ctx, cfg.Telemetry, component, buildinfo.Version)
|
|
if err != nil {
|
|
return Logs{}, err
|
|
}
|
|
feed := logging.NewFeed(1000)
|
|
taskLogs := logging.NewTaskRegistry(filepath.Join(cfg.StateDir, "logs", "tasks"), feed, logging.TaskSinkOptions{})
|
|
fileLog, fileCloser, err := logging.New(logging.Options{
|
|
File: cfg.Log.File,
|
|
MaxSizeMB: cfg.Log.MaxSizeMB,
|
|
Keep: cfg.Log.Keep,
|
|
Level: cfg.Log.Level,
|
|
Stderr: !cfg.Log.Quiet,
|
|
Feed: feed,
|
|
})
|
|
log := fileLog.With("component", component)
|
|
if err != nil {
|
|
log.ErrorContext(ctx, "file logging disabled", "err", err)
|
|
} else if cfg.Log.File != "" {
|
|
log.InfoContext(ctx, "logging to file", "path", cfg.Log.File)
|
|
}
|
|
closer := closerFunc(func() error {
|
|
stopTracing()
|
|
return fileCloser.Close()
|
|
})
|
|
return Logs{Log: log, Feed: feed, TaskLogs: taskLogs, Closer: closer}, nil
|
|
}
|
|
|
|
type closerFunc func() error
|
|
|
|
func (f closerFunc) Close() error { return f() }
|