Add log location to the status output (#4260)

This commit is contained in:
Todd authored and GitHub committed 2024-01-22 08:42:19 -08:00
1 parent fff5f9506f
commit 30066c06f5
5 files changed
+19 -17

No files matched your search

@@ -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())
})
+5 -11
View File
@@ -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) {
@@ -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)
}
@@ -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)
}
@@ -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 {