feat(cli): rotate file logs for coderd (#15438)

Related to #15309 

As we already are doing for agent logs - this PR is enabling the logs
rotation for coderd logs.

Currently keeping the same logic than we had for agent - with 5MB as the
file size for rotation.
This commit is contained in:
Vincent Vielle
2024-11-12 21:48:32 +01:00
committed by GitHub
parent 56e219b50d
commit d6442db25a
2 changed files with 8 additions and 32 deletions
+8 -7
View File
@@ -4,11 +4,11 @@ import (
"context"
"fmt"
"io"
"os"
"regexp"
"strings"
"golang.org/x/xerrors"
"gopkg.in/natefinch/lumberjack.v2"
"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
@@ -104,7 +104,6 @@ func (b *Builder) Build(inv *serpent.Invocation) (log slog.Logger, closeLog func
addSinkIfProvided := func(sinkFn func(io.Writer) slog.Sink, loc string) error {
switch loc {
case "":
case "/dev/stdout":
sinks = append(sinks, sinkFn(inv.Stdout))
@@ -112,12 +111,14 @@ func (b *Builder) Build(inv *serpent.Invocation) (log slog.Logger, closeLog func
sinks = append(sinks, sinkFn(inv.Stderr))
default:
fi, err := os.OpenFile(loc, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
return xerrors.Errorf("open log file %q: %w", loc, err)
logWriter := &lumberjack.Logger{
Filename: loc,
MaxSize: 5, // MB
// Without this, rotated logs will never be deleted.
MaxBackups: 1,
}
closers = append(closers, fi.Close)
sinks = append(sinks, sinkFn(fi))
closers = append(closers, logWriter.Close)
sinks = append(sinks, sinkFn(logWriter))
}
return nil
}