Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename log rotate config #1520

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions internal/config/mount_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ package config

const (
// Default log rotation config values.
defaultMaxFileSizeMB = 512
defaultFileCount = 10
defaultCompress = false
defaultMaxFileSizeMB = 512
defaultBackupFileCount = 10
defaultCompress = false
)

type WriteConfig struct {
Expand Down Expand Up @@ -46,16 +46,16 @@ type MountConfig struct {
// 3. compress: indicates whether the rotated log files should be compressed
// using gzip. The default value is False.
type LogRotateConfig struct {
MaxFileSizeMB int `yaml:"max-file-size-mb"`
FileCount int `yaml:"file-count"`
Compress bool `yaml:"compress"`
MaxFileSizeMB int `yaml:"max-file-size-mb"`
BackupFileCount int `yaml:"backup-file-count"`
ashmeenkaur marked this conversation as resolved.
Show resolved Hide resolved
Compress bool `yaml:"compress"`
}

func DefaultLogRotateConfig() LogRotateConfig {
return LogRotateConfig{
MaxFileSizeMB: defaultMaxFileSizeMB,
FileCount: defaultFileCount,
Compress: defaultCompress,
MaxFileSizeMB: defaultMaxFileSizeMB,
BackupFileCount: defaultBackupFileCount,
Compress: defaultCompress,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ logging:
severity: error
log-rotate:
max-file-size-mb: -1
file-count: -1
backup-file-count: -1
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ write:
logging:
severity: error
log-rotate:
file-count: -1
backup-file-count: -1
2 changes: 1 addition & 1 deletion internal/config/testdata/valid_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ logging:
severity: error
log-rotate:
max-file-size-mb: 100
file-count: 5
backup-file-count: 5
compress: false


Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
write:
create-empty-file: true
logging:
file-path: /tmp/logfile.json
format: text
severity: error
log-rotate:
max-file-size-mb: 100
backup-file-count: 0 # retain all backup files
compress: false


4 changes: 2 additions & 2 deletions internal/config/yaml_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func IsValidLogRotateConfig(config LogRotateConfig) error {
if config.MaxFileSizeMB <= 0 {
return fmt.Errorf("max-file-size-mb should be atleast 1")
}
if config.FileCount <= 0 {
return fmt.Errorf("file-count should be atleast 1")
if config.BackupFileCount < 0 {
return fmt.Errorf("backup-file-count should be 0 (to retain all backup files) or a positive value")
}
return nil
}
Expand Down
20 changes: 17 additions & 3 deletions internal/config/yaml_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func validateDefaultConfig(mountConfig *MountConfig) {
ExpectEq("", mountConfig.LogConfig.Format)
ExpectEq("", mountConfig.LogConfig.FilePath)
ExpectEq(512, mountConfig.LogConfig.LogRotateConfig.MaxFileSizeMB)
ExpectEq(10, mountConfig.LogConfig.LogRotateConfig.FileCount)
ExpectEq(10, mountConfig.LogConfig.LogRotateConfig.BackupFileCount)
ExpectEq(false, mountConfig.LogConfig.LogRotateConfig.Compress)
}

Expand Down Expand Up @@ -68,6 +68,20 @@ func (t *YamlParserTest) TestReadConfigFile_InvalidConfig() {
AssertTrue(strings.Contains(err.Error(), "error parsing config file: yaml: unmarshal errors:"))
}

func (t *YamlParserTest) TestReadConfigFile_ValidConfigWith0BackupFileCount() {
mountConfig, err := ParseConfigFile("testdata/valid_config_with_0_backup-file-count.yaml")

AssertEq(nil, err)
AssertNe(nil, mountConfig)
ExpectEq(true, mountConfig.WriteConfig.CreateEmptyFile)
ExpectEq(ERROR, mountConfig.LogConfig.Severity)
ExpectEq("/tmp/logfile.json", mountConfig.LogConfig.FilePath)
ExpectEq("text", mountConfig.LogConfig.Format)
ExpectEq(100, mountConfig.LogConfig.LogRotateConfig.MaxFileSizeMB)
ExpectEq(0, mountConfig.LogConfig.LogRotateConfig.BackupFileCount)
ExpectEq(false, mountConfig.LogConfig.LogRotateConfig.Compress)
}

func (t *YamlParserTest) TestReadConfigFile_Invalid_UnexpectedField_Config() {
_, err := ParseConfigFile("testdata/invalid_unexpectedfield_config.yaml")

Expand All @@ -86,7 +100,7 @@ func (t *YamlParserTest) TestReadConfigFile_ValidConfig() {
ExpectEq("/tmp/logfile.json", mountConfig.LogConfig.FilePath)
ExpectEq("text", mountConfig.LogConfig.Format)
ExpectEq(100, mountConfig.LogConfig.LogRotateConfig.MaxFileSizeMB)
ExpectEq(5, mountConfig.LogConfig.LogRotateConfig.FileCount)
ExpectEq(5, mountConfig.LogConfig.LogRotateConfig.BackupFileCount)
ExpectEq(false, mountConfig.LogConfig.LogRotateConfig.Compress)
}

Expand All @@ -111,5 +125,5 @@ func (t *YamlParserTest) TestReadConfigFile_InvalidLogRotateConfig2() {

AssertNe(nil, err)
AssertTrue(strings.Contains(err.Error(),
fmt.Sprintf(parseConfigFileErrMsgFormat, "file-count should be atleast 1")))
fmt.Sprintf(parseConfigFileErrMsgFormat, "backup-file-count should be 0 (to retain all backup files) or a positive value")))
}
2 changes: 1 addition & 1 deletion internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func InitLogFile(logConfig config.LogConfig) error {
fileWriter = &lumberjack.Logger{
Filename: f.Name(),
MaxSize: logConfig.LogRotateConfig.MaxFileSizeMB,
MaxBackups: logConfig.LogRotateConfig.FileCount - 1,
MaxBackups: logConfig.LogRotateConfig.BackupFileCount,
Compress: logConfig.LogRotateConfig.Compress,
}
} else {
Expand Down
10 changes: 5 additions & 5 deletions internal/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,15 @@ func (t *LoggerTest) TestInitLogFile() {
filePath, _ := os.UserHomeDir()
filePath += "/log.txt"
fileSize := 100
fileCount := 2
backupFileCount := 2
logConfig := config.LogConfig{
Severity: config.DEBUG,
Format: format,
FilePath: filePath,
LogRotateConfig: config.LogRotateConfig{
MaxFileSizeMB: fileSize,
FileCount: fileCount,
Compress: true,
MaxFileSizeMB: fileSize,
BackupFileCount: backupFileCount,
Compress: true,
},
}

Expand All @@ -288,6 +288,6 @@ func (t *LoggerTest) TestInitLogFile() {
ExpectEq(format, defaultLoggerFactory.format)
ExpectEq(config.DEBUG, defaultLoggerFactory.level)
ExpectEq(fileSize, defaultLoggerFactory.logRotateConfig.MaxFileSizeMB)
ExpectEq(fileCount, defaultLoggerFactory.logRotateConfig.FileCount)
ExpectEq(backupFileCount, defaultLoggerFactory.logRotateConfig.BackupFileCount)
ExpectEq(true, defaultLoggerFactory.logRotateConfig.Compress)
}
12 changes: 6 additions & 6 deletions tools/integration_tests/log_rotation/log_rotation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ const (
var logDirPath string
var logFilePath string

func getMountConfigForLogRotation(maxFileSizeMB, fileCount int, compress bool,
func getMountConfigForLogRotation(maxFileSizeMB, backupFileCount int, compress bool,
logFilePath string) config.MountConfig {
mountConfig := config.MountConfig{
LogConfig: config.LogConfig{
Severity: config.TRACE,
FilePath: logFilePath,
LogRotateConfig: config.LogRotateConfig{
MaxFileSizeMB: maxFileSizeMB,
FileCount: fileCount,
Compress: compress,
MaxFileSizeMB: maxFileSizeMB,
BackupFileCount: backupFileCount,
Compress: compress,
},
},
}
Expand Down Expand Up @@ -80,10 +80,10 @@ func TestMain(m *testing.M) {

// Set up config files.
configFile1 := setup.YAMLConfigFile(
getMountConfigForLogRotation(maxFileSizeMB, logFileCount, true, logFilePath),
getMountConfigForLogRotation(maxFileSizeMB, backupLogFileCount, true, logFilePath),
"config1.yaml")
configFile2 := setup.YAMLConfigFile(
getMountConfigForLogRotation(maxFileSizeMB, logFileCount, false, logFilePath),
getMountConfigForLogRotation(maxFileSizeMB, backupLogFileCount, false, logFilePath),
"config2.yaml")

// Set up flags to run tests on.
Expand Down
4 changes: 2 additions & 2 deletions tools/integration_tests/operations/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ func TestMain(m *testing.M) {
LogConfig: config.LogConfig{
Severity: config.TRACE,
LogRotateConfig: config.LogRotateConfig{
MaxFileSizeMB: 10,
FileCount: 1,
MaxFileSizeMB: 10,
BackupFileCount: 0, // to retain all backup log files.
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion tools/integration_tests/run_tests_mounted_directory.sh
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ echo "logging:
severity: trace
log-rotate:
max-file-size-mb: 2
file-count: 3
backup-file-count: 3
compress: true
" > /tmp/gcsfuse_config.yaml
gcsfuse --config-file=/tmp/gcsfuse_config.yaml $TEST_BUCKET_NAME $MOUNT_DIR
Expand Down
Loading