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)
}