v0.3.7 dependency update refactor

This commit is contained in:
2025-07-21 02:40:37 -04:00
parent 7c221b426b
commit 5aa732b096
5 changed files with 29 additions and 29 deletions

View File

@ -122,8 +122,8 @@ func configureFileLogging(logCfg *log.Config, cfg *config.Config) {
if cfg.Logging.File != nil {
logCfg.Directory = cfg.Logging.File.Directory
logCfg.Name = cfg.Logging.File.Name
logCfg.MaxSizeMB = cfg.Logging.File.MaxSizeMB
logCfg.MaxTotalSizeMB = cfg.Logging.File.MaxTotalSizeMB
logCfg.MaxSizeKB = cfg.Logging.File.MaxSizeMB * 1000
logCfg.MaxTotalSizeKB = cfg.Logging.File.MaxTotalSizeMB * 1000
if cfg.Logging.File.RetentionHours > 0 {
logCfg.RetentionPeriodHrs = cfg.Logging.File.RetentionHours
}

View File

@ -2,6 +2,7 @@
package config
import (
"errors"
"fmt"
"os"
"path/filepath"
@ -74,9 +75,9 @@ func Load(args []string) (*Config, error) {
cfg, err := lconfig.NewBuilder().
WithDefaults(defaults()).
WithEnvPrefix("LOGWISP_").
WithFile(configPath).
WithArgs(args).
WithEnvTransform(customEnvTransform).
WithArgs(args).
WithFile(configPath).
WithSources(
lconfig.SourceCLI,
lconfig.SourceEnv,
@ -86,23 +87,24 @@ func Load(args []string) (*Config, error) {
Build()
if err != nil {
// Config file load errors
if strings.Contains(err.Error(), "not found") {
// Handle file not found errors - maintain existing behavior
if errors.Is(err, lconfig.ErrConfigNotFound) {
if isExplicit {
return nil, fmt.Errorf("config file not found: %s", configPath)
}
// If the default config file is not found, it's not an error.
// If the default config file is not found, it's not an error
} else {
return nil, fmt.Errorf("failed to load config: %w", err)
}
}
// Scan into final config struct
// Scan into final config struct - using new interface
finalConfig := &Config{}
if err := cfg.Scan("", finalConfig); err != nil {
if err := cfg.Scan(finalConfig); err != nil {
return nil, fmt.Errorf("failed to scan config: %w", err)
}
// Set config file path if it exists
if _, err := os.Stat(configPath); err == nil {
finalConfig.ConfigFile = configPath
}
@ -160,7 +162,7 @@ func resolveConfigPath(args []string) (path string, isExplicit bool) {
func customEnvTransform(path string) string {
env := strings.ReplaceAll(path, ".", "_")
env = strings.ToUpper(env)
env = "LOGWISP_" + env
// env = "LOGWISP_" + env // already added by WithEnvPrefix
return env
}

View File

@ -40,35 +40,33 @@ func NewFileSink(options map[string]any, logger *log.Logger, formatter format.Fo
}
// Create configuration for the internal log writer
var configArgs []string
configArgs = append(configArgs,
fmt.Sprintf("directory=%s", directory),
fmt.Sprintf("name=%s", name),
"enable_stdout=false", // File only
"show_timestamp=false", // We already have timestamps in entries
"show_level=false", // We already have levels in entries
)
writerConfig := log.DefaultConfig()
writerConfig.Directory = directory
writerConfig.Name = name
writerConfig.EnableStdout = false // File only
writerConfig.ShowTimestamp = false // We already have timestamps in entries
writerConfig.ShowLevel = false // We already have levels in entries
// Add optional configurations
if maxSize, ok := options["max_size_mb"].(int64); ok && maxSize > 0 {
configArgs = append(configArgs, fmt.Sprintf("max_size_mb=%d", maxSize))
writerConfig.MaxSizeKB = maxSize * 1000
}
if maxTotalSize, ok := options["max_total_size_mb"].(int64); ok && maxTotalSize >= 0 {
configArgs = append(configArgs, fmt.Sprintf("max_total_size_mb=%d", maxTotalSize))
writerConfig.MaxTotalSizeKB = maxTotalSize * 1000
}
if retention, ok := options["retention_hours"].(int64); ok && retention > 0 {
configArgs = append(configArgs, fmt.Sprintf("retention_period_hrs=%.1f", retention))
writerConfig.RetentionPeriodHrs = float64(retention)
}
if minDiskFree, ok := options["min_disk_free_mb"].(int64); ok && minDiskFree > 0 {
configArgs = append(configArgs, fmt.Sprintf("min_disk_free_mb=%d", minDiskFree))
writerConfig.MinDiskFreeKB = minDiskFree * 1000
}
// Create internal logger for file writing
writer := log.NewLogger()
if err := writer.ApplyOverride(configArgs...); err != nil {
if err := writer.ApplyConfig(writerConfig); err != nil {
return nil, fmt.Errorf("failed to initialize file writer: %w", err)
}