Skip to content

Commit

Permalink
Move logging completely to new config
Browse files Browse the repository at this point in the history
  • Loading branch information
kislaykishore committed Jul 6, 2024
1 parent f2107c6 commit c2ca817
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
40 changes: 40 additions & 0 deletions cfg/defaults.go
Original file line number Diff line number Diff line change
@@ -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,
},
}
2 changes: 1 addition & 1 deletion cmd/legacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
16 changes: 8 additions & 8 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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))

Expand All @@ -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)
}
Expand Down Expand Up @@ -171,7 +171,7 @@ type loggerFactory struct {
sysWriter *syslog.Writer
format string
level string
logRotateConfig config.LogRotateConfig
logRotateConfig cfg.LogRotateLoggingConfig
fileWriter *lumberjack.Logger
}

Expand Down
22 changes: 10 additions & 12 deletions internal/logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit c2ca817

Please sign in to comment.