From c2ca8173ed0b4392c1f28d30e34e84a3b657eb24 Mon Sep 17 00:00:00 2001 From: Kislay Kishore Date: Sat, 6 Jul 2024 13:58:37 +0530 Subject: [PATCH] Move logging completely to new config --- cfg/defaults.go | 40 ++++++++++++++++++++++++++++++++++ cmd/legacy_main.go | 2 +- internal/logger/logger.go | 16 +++++++------- internal/logger/logger_test.go | 22 +++++++++---------- 4 files changed, 59 insertions(+), 21 deletions(-) create mode 100644 cfg/defaults.go diff --git a/cfg/defaults.go b/cfg/defaults.go new file mode 100644 index 0000000000..85671b626b --- /dev/null +++ b/cfg/defaults.go @@ -0,0 +1,40 @@ +// Copyright 2024 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cfg + +var DefaultConfig = Config{ + Logging: LoggingConfig{ + Severity: "INFO", + LogRotate: LogRotateLoggingConfig{ + BackupFileCount: 10, + Compress: true, + MaxFileSizeMb: 512, + }, + }, + FileCache: FileCacheConfig{ + MaxSizeMb: -1, + }, + MetadataCache: MetadataCacheConfig{ + TtlSecs: 60, + TypeCacheMaxSizeMb: 4, + StatCacheMaxSizeMb: 32, + }, + GcsConnection: GcsConnectionConfig{ + GrpcConnPoolSize: 1, + }, + FileSystem: FileSystemConfig{ + IgnoreInterrupts: true, + }, +} diff --git a/cmd/legacy_main.go b/cmd/legacy_main.go index 2d75f79870..014dc24721 100644 --- a/cmd/legacy_main.go +++ b/cmd/legacy_main.go @@ -276,7 +276,7 @@ func runCLIApp(c *cli.Context) (err error) { } if newConfig.Foreground { - err = logger.InitLogFile(mountConfig.LogConfig, newConfig.Logging) + err = logger.InitLogFile(newConfig.Logging) if err != nil { return fmt.Errorf("init log file: %w", err) } diff --git a/internal/logger/logger.go b/internal/logger/logger.go index 8f6593ce1b..24c6d72e29 100644 --- a/internal/logger/logger.go +++ b/internal/logger/logger.go @@ -52,7 +52,7 @@ var ( // config. // Here, background true means, this InitLogFile has been called for the // background daemon. -func InitLogFile(legacyLogConfig config.LogConfig, newLogConfig cfg.LoggingConfig) error { +func InitLogFile(newLogConfig cfg.LoggingConfig) error { var f *os.File var sysWriter *syslog.Writer var fileWriter *lumberjack.Logger @@ -68,9 +68,9 @@ func InitLogFile(legacyLogConfig config.LogConfig, newLogConfig cfg.LoggingConfi } fileWriter = &lumberjack.Logger{ Filename: f.Name(), - MaxSize: legacyLogConfig.LogRotateConfig.MaxFileSizeMB, - MaxBackups: legacyLogConfig.LogRotateConfig.BackupFileCount, - Compress: legacyLogConfig.LogRotateConfig.Compress, + MaxSize: int(newLogConfig.LogRotate.MaxFileSizeMb), + MaxBackups: int(newLogConfig.LogRotate.BackupFileCount), + Compress: newLogConfig.LogRotate.Compress, } } else { if _, ok := os.LookupEnv(GCSFuseInBackgroundMode); ok { @@ -93,7 +93,7 @@ func InitLogFile(legacyLogConfig config.LogConfig, newLogConfig cfg.LoggingConfi fileWriter: fileWriter, format: newLogConfig.Format, level: string(newLogConfig.Severity), - logRotateConfig: legacyLogConfig.LogRotateConfig, + logRotateConfig: newLogConfig.LogRotate, } defaultLogger = defaultLoggerFactory.newLogger(string(newLogConfig.Severity)) @@ -105,8 +105,8 @@ func init() { defaultLoggerFactory = &loggerFactory{ file: nil, format: defaultFormat, - level: config.INFO, // setting log level to INFO by default - logRotateConfig: config.DefaultLogRotateConfig(), + level: "INFO", // setting log level to INFO by default + logRotateConfig: cfg.DefaultConfig.Logging.LogRotate, } defaultLogger = defaultLoggerFactory.newLogger(config.INFO) } @@ -171,7 +171,7 @@ type loggerFactory struct { sysWriter *syslog.Writer format string level string - logRotateConfig config.LogRotateConfig + logRotateConfig cfg.LogRotateLoggingConfig fileWriter *lumberjack.Logger } diff --git a/internal/logger/logger_test.go b/internal/logger/logger_test.go index d02e91b279..9b78dc036b 100644 --- a/internal/logger/logger_test.go +++ b/internal/logger/logger_test.go @@ -273,29 +273,27 @@ func (t *LoggerTest) TestInitLogFile() { format := "text" filePath, _ := os.UserHomeDir() filePath += "/log.txt" - fileSize := 100 - backupFileCount := 2 - legacyLogConfig := config.LogConfig{ - LogRotateConfig: config.LogRotateConfig{ - MaxFileSizeMB: fileSize, - BackupFileCount: backupFileCount, - Compress: true, - }, - } + fileSize := int64(100) + backupFileCount := int64(2) newLogConfig := cfg.LoggingConfig{ FilePath: cfg.ResolvedPath(filePath), Severity: "DEBUG", Format: format, + LogRotate: cfg.LogRotateLoggingConfig{ + MaxFileSizeMb: fileSize, + BackupFileCount: backupFileCount, + Compress: true, + }, } - err := InitLogFile(legacyLogConfig, newLogConfig) + err := InitLogFile(newLogConfig) assert.NoError(t.T(), err) assert.Equal(t.T(), filePath, defaultLoggerFactory.file.Name()) assert.Nil(t.T(), defaultLoggerFactory.sysWriter) assert.Equal(t.T(), format, defaultLoggerFactory.format) assert.Equal(t.T(), config.DEBUG, defaultLoggerFactory.level) - assert.Equal(t.T(), fileSize, defaultLoggerFactory.logRotateConfig.MaxFileSizeMB) + assert.Equal(t.T(), fileSize, defaultLoggerFactory.logRotateConfig.MaxFileSizeMb) assert.Equal(t.T(), backupFileCount, defaultLoggerFactory.logRotateConfig.BackupFileCount) assert.True(t.T(), defaultLoggerFactory.logRotateConfig.Compress) } @@ -304,7 +302,7 @@ func (t *LoggerTest) TestSetLogFormatToText() { defaultLoggerFactory = &loggerFactory{ file: nil, level: config.INFO, // setting log level to INFO by default - logRotateConfig: config.DefaultLogRotateConfig(), + logRotateConfig: cfg.DefaultConfig.Logging.LogRotate, } testData := []struct {