From 30066c06f54c544af55d65e20dde0ade87cc2049 Mon Sep 17 00:00:00 2001 From: Todd Date: Mon, 22 Jan 2024 08:42:19 -0800 Subject: [PATCH] Add log location to the status output (#4260) --- internal/clientcache/cmd/daemon/logfile_test.go | 6 ++++-- internal/clientcache/cmd/daemon/start.go | 16 +++++----------- internal/clientcache/cmd/daemon/status.go | 3 +++ internal/clientcache/internal/daemon/server.go | 3 ++- .../internal/daemon/status_handler.go | 8 +++++--- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/internal/clientcache/cmd/daemon/logfile_test.go b/internal/clientcache/cmd/daemon/logfile_test.go index 25db51803..7dc94cff6 100644 --- a/internal/clientcache/cmd/daemon/logfile_test.go +++ b/internal/clientcache/cmd/daemon/logfile_test.go @@ -27,8 +27,9 @@ import ( func TestWithEventer(t *testing.T) { ctx := context.Background() dir := t.TempDir() - l, err := logFile(ctx, dir, 1) + l, loc, err := logFile(ctx, dir, 1) require.NoError(t, err) + assert.Equal(t, filepath.Join(dir, logFileName), loc) t.Cleanup(func() { assert.NoError(t, l.Close()) }) @@ -82,8 +83,9 @@ func TestWithEventer(t *testing.T) { func TestRotation(t *testing.T) { ctx := context.Background() dir := t.TempDir() - l, err := logFile(ctx, dir, 1) + l, loc, err := logFile(ctx, dir, 1) require.NoError(t, err) + assert.Equal(t, filepath.Join(dir, logFileName), loc) t.Cleanup(func() { assert.NoError(t, l.Close()) }) diff --git a/internal/clientcache/cmd/daemon/start.go b/internal/clientcache/cmd/daemon/start.go index 117c890ea..d0e76b2c5 100644 --- a/internal/clientcache/cmd/daemon/start.go +++ b/internal/clientcache/cmd/daemon/start.go @@ -8,7 +8,6 @@ import ( stderrors "errors" "fmt" "io" - "net" "os" "os/exec" "path/filepath" @@ -40,12 +39,6 @@ var ( _ cli.CommandAutocomplete = (*StartCommand)(nil) ) -type server interface { - setupLogging(context.Context, io.Writer) error - serve(context.Context, daemon.Commander, net.Listener) error - shutdown() error -} - type StartCommand struct { *base.Command @@ -188,7 +181,7 @@ func (c *StartCommand) Run(args []string) int { // TODO: print something out for the spawner to consume in case they can easily // report if the daemon started or not. - lf, err := logFile(ctx, dotDir, 5) + lf, logFileName, err := logFile(ctx, dotDir, 5) if err != nil { c.PrintCliError(err) return base.CommandCliError @@ -206,6 +199,7 @@ func (c *StartCommand) Run(args []string) int { LogLevel: c.flagLogLevel, LogFormat: c.flagLogFormat, LogWriter: io.MultiWriter(writers...), + LogFileName: logFileName, DotDirectory: dotDir, } @@ -254,14 +248,14 @@ func DefaultDotDirectory(ctx context.Context) (string, error) { // a suffix that matches the time that the rotation happened. Up to 3 log files // are saved as backup. When a new log file is rotated and there is already 3 // backups created, the oldest one is deleted. -func logFile(ctx context.Context, dotDir string, maxSizeMb int) (io.WriteCloser, error) { +func logFile(ctx context.Context, dotDir string, maxSizeMb int) (io.WriteCloser, string, error) { const op = "daemon.logFile" logFilePath := filepath.Join(dotDir, logFileName) { // Ensure the file is created with the desired permissions. logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY, 0o600) if err != nil { - return nil, errors.Wrap(ctx, err, op) + return nil, "", errors.Wrap(ctx, err, op) } logFile.Close() } @@ -272,7 +266,7 @@ func logFile(ctx context.Context, dotDir string, maxSizeMb int) (io.WriteCloser, MaxBackups: 3, Compress: true, } - return logFile, nil + return logFile, logFilePath, nil } func (c *StartCommand) makeBackground(ctx context.Context, dotDir string) (bool, []io.Writer, pidCleanup, error) { diff --git a/internal/clientcache/cmd/daemon/status.go b/internal/clientcache/cmd/daemon/status.go index ed75e32b0..4e50720f5 100644 --- a/internal/clientcache/cmd/daemon/status.go +++ b/internal/clientcache/cmd/daemon/status.go @@ -141,6 +141,9 @@ func printStatusTable(status *daemon.StatusResult) string { if len(status.SocketAddress) > 0 { nonAttributeMap["Domain Socket"] = status.SocketAddress } + if len(status.LogLocation) > 0 { + nonAttributeMap["Log Location"] = status.LogLocation + } if status.Uptime > 0 { nonAttributeMap["Uptime"] = status.Uptime.Round(time.Second) } diff --git a/internal/clientcache/internal/daemon/server.go b/internal/clientcache/internal/daemon/server.go index a180150e1..6149cb186 100644 --- a/internal/clientcache/internal/daemon/server.go +++ b/internal/clientcache/internal/daemon/server.go @@ -79,6 +79,7 @@ type Config struct { LogLevel string LogFormat string LogWriter io.Writer + LogFileName string DotDirectory string // The amount of time since the last refresh that must have passed for a // search query to trigger an inline refresh. @@ -287,7 +288,7 @@ func (s *CacheServer) Serve(ctx context.Context, cmd Commander, opt ...Option) e } mux.Handle("/v1/search", versionEnforcement(searchFn)) - statusFn, err := newStatusHandlerFunc(ctx, repo, l.Addr().String()) + statusFn, err := newStatusHandlerFunc(ctx, repo, l.Addr().String(), s.conf.LogFileName) if err != nil { return errors.Wrap(ctx, err, op) } diff --git a/internal/clientcache/internal/daemon/status_handler.go b/internal/clientcache/internal/daemon/status_handler.go index 052665003..562cd8d8a 100644 --- a/internal/clientcache/internal/daemon/status_handler.go +++ b/internal/clientcache/internal/daemon/status_handler.go @@ -68,10 +68,11 @@ type UserStatus struct { type StatusResult struct { Uptime time.Duration `json:"uptime,omitempty"` SocketAddress string `json:"socket_address,omitempty"` + LogLocation string `json:"log_location,omitempty"` Users []UserStatus `json:"users,omitempty"` } -func newStatusHandlerFunc(ctx context.Context, repo *cache.Repository, socketAddr string) (http.HandlerFunc, error) { +func newStatusHandlerFunc(ctx context.Context, repo *cache.Repository, socketAddr, logLocation string) (http.HandlerFunc, error) { const op = "daemon.newStatusHandlerFunc" switch { case util.IsNil(repo): @@ -102,7 +103,7 @@ func newStatusHandlerFunc(ctx context.Context, repo *cache.Repository, socketAdd return } - apiRes := toApiStatus(res, started, socketAddr) + apiRes := toApiStatus(res, started, socketAddr, logLocation) j, err := json.Marshal(apiRes) if err != nil { writeError(w, err.Error(), http.StatusInternalServerError) @@ -114,7 +115,7 @@ func newStatusHandlerFunc(ctx context.Context, repo *cache.Repository, socketAdd } // toApiStatus converts a domain status result to an api status result -func toApiStatus(in *cache.Status, started time.Time, socketAddr string) *StatusResult { +func toApiStatus(in *cache.Status, started time.Time, socketAddr, logLocation string) *StatusResult { if in == nil { return nil } @@ -122,6 +123,7 @@ func toApiStatus(in *cache.Status, started time.Time, socketAddr string) *Status out := &StatusResult{ Uptime: time.Since(started), SocketAddress: socketAddr, + LogLocation: logLocation, } for _, inU := range in.Users {