Skip to content

Commit af06886

Browse files
committed
feat: add cleanup parameters in async logging
feat: add cleanup parameters in async logging feat: add cleanup parameters in async logging
1 parent 7d6b5bd commit af06886

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

cmd/base/options/log.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ type LogsOptions struct {
2929
SupportAsyncLogging bool
3030
LogDir string
3131
LogBufferSizeMB int
32+
LogFileMaxAge int
33+
LogFileMaxBackups int
3234
}
3335

3436
func NewLogsOptions() *LogsOptions {
3537
return &LogsOptions{
3638
LogPackageLevel: general.LoggingPKGFull,
3739
LogFileMaxSizeInMB: 1800,
38-
LogDir: "/opt/tiger/toutiao/log/app",
3940
LogBufferSizeMB: 10000,
41+
LogFileMaxAge: 7,
42+
LogFileMaxBackups: 10,
4043
}
4144
}
4245

@@ -47,6 +50,8 @@ func (o *LogsOptions) AddFlags(fs *pflag.FlagSet) {
4750
fs.BoolVar(&o.SupportAsyncLogging, "support-async-logging", o.SupportAsyncLogging, "whether to support async logging")
4851
fs.StringVar(&o.LogDir, "log-dir", o.LogDir, "directory of log file")
4952
fs.IntVar(&o.LogBufferSizeMB, "log-buffer-size", o.LogBufferSizeMB, "size of the ring buffer to store async logs")
53+
fs.IntVar(&o.LogFileMaxAge, "log-file-max-age", o.LogFileMaxAge, "max age of klog log file in days")
54+
fs.IntVar(&o.LogFileMaxBackups, "log-file-max-backups", o.LogFileMaxBackups, "max number of klog log file backups")
5055
}
5156

5257
func (o *LogsOptions) ApplyTo(c *generic.LogConfiguration) error {
@@ -56,5 +61,7 @@ func (o *LogsOptions) ApplyTo(c *generic.LogConfiguration) error {
5661
c.LogDir = o.LogDir
5762
c.LogFileMaxSize = int(o.LogFileMaxSizeInMB)
5863
c.LogBufferSizeMB = o.LogBufferSizeMB
64+
c.LogFileMaxAge = o.LogFileMaxAge
65+
c.LogFileMaxBackups = o.LogFileMaxBackups
5966
return nil
6067
}

cmd/katalyst-agent/app/agent.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func Run(
6464
}
6565

6666
if conf.SupportAsyncLogging {
67-
asyncLogger := logging.NewAsyncLogger(genericCtx, conf.LogFileMaxSize, conf.LogBufferSizeMB)
67+
asyncLogger := logging.NewAsyncLogger(genericCtx, conf.LogDir, conf.LogFileMaxSize, conf.LogFileMaxAge, conf.LogFileMaxBackups, conf.LogBufferSizeMB)
6868
defer asyncLogger.Shutdown()
6969
}
7070

pkg/config/generic/log.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type LogConfiguration struct {
2121
LogDir string
2222
LogFileMaxSize int
2323
LogBufferSizeMB int
24+
LogFileMaxAge int
25+
LogFileMaxBackups int
2426
}
2527

2628
func NewLogConfiguration() *LogConfiguration {

pkg/util/logging/logger.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package logging
1818

1919
import (
20+
"path"
2021
"time"
2122

2223
"github.com/rs/zerolog/diode"
@@ -44,10 +45,10 @@ const (
4445
)
4546

4647
const (
47-
defaultInfoLogFileName = "/opt/tiger/toutiao/log/app/agent.info.log"
48-
defaultWarningLogFileName = "/opt/tiger/toutiao/log/app/agent.warning.log"
49-
defaultErrorLogFileName = "/opt/tiger/toutiao/log/app/agent.error.log"
50-
defaultFatalLogFileName = "/opt/tiger/toutiao/log/app/agent.fatal.log"
48+
defaultInfoLogFileName = "agent.info.log"
49+
defaultWarningLogFileName = "agent.warning.log"
50+
defaultErrorLogFileName = "agent.error.log"
51+
defaultFatalLogFileName = "agent.fatal.log"
5152
)
5253

5354
type logInfo struct {
@@ -64,20 +65,23 @@ var logInfoMap = map[SeverityName]*logInfo{
6465

6566
type AsyncLogger struct {
6667
diodeWriters []diode.Writer
67-
logFile string
6868
}
6969

7070
// NewAsyncLogger creates an async logger that produces an async writer for each of the severity levels.
7171
// The async writer spins up a goroutine that periodically flushes the buffered logs to disk.
72-
func NewAsyncLogger(agentCtx *agent.GenericContext, maxSizeMB int, bufferSizeMB int) *AsyncLogger {
72+
func NewAsyncLogger(
73+
agentCtx *agent.GenericContext, logDir string, maxSizeMB, maxAge, maxBackups, bufferSizeMB int,
74+
) *AsyncLogger {
7375
wrappedEmitter := agentCtx.EmitterPool.GetDefaultMetricsEmitter()
7476

7577
asyncLogger := &AsyncLogger{}
7678
for severity, logInfo := range logInfoMap {
7779
// lumberjackLogger is a logger that rotates log files
7880
lumberjackLogger := &lumberjack.Logger{
79-
Filename: logInfo.fileName,
80-
MaxSize: maxSizeMB,
81+
Filename: path.Join(logDir, logInfo.fileName),
82+
MaxSize: maxSizeMB,
83+
MaxAge: maxAge,
84+
MaxBackups: maxBackups,
8185
}
8286

8387
// diodeWriter is a writer that stores logs in a ring buffer and asynchronously flushes them

pkg/util/logging/logger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestNewAsyncLogger(t *testing.T) {
3333
EmitterPool: metrics_pool.DummyMetricsEmitterPool{},
3434
},
3535
}
36-
asyncLogger := NewAsyncLogger(agentCtx, 100, 100)
36+
asyncLogger := NewAsyncLogger(agentCtx, "testDir", 100, 100, 100, 100)
3737
assert.NotNil(t, asyncLogger)
3838

3939
assert.Equal(t, len(asyncLogger.diodeWriters), 4)

0 commit comments

Comments
 (0)