cloudflared-mirror/logger/configuration.go

128 lines
2.6 KiB
Go
Raw Normal View History

2020-11-15 01:49:44 +00:00
package logger
import "path/filepath"
2020-11-15 01:49:44 +00:00
var defaultConfig = createDefaultConfig()
// Logging configuration
type Config struct {
ConsoleConfig *ConsoleConfig // If nil, the logger will not log into the console
FileConfig *FileConfig // If nil, the logger will not use an individual log file
RollingConfig *RollingConfig // If nil, the logger will not use a rolling log
MinLevel string // debug | info | error | fatal
}
type ConsoleConfig struct {
noColor bool
}
type FileConfig struct {
Dirname string
Filename string
}
func (fc *FileConfig) Fullpath() string {
return filepath.Join(fc.Dirname, fc.Filename)
2020-11-15 01:49:44 +00:00
}
type RollingConfig struct {
Dirname string
Filename string
2020-11-15 01:49:44 +00:00
maxSize int // megabytes
maxBackups int // files
maxAge int // days
}
func createDefaultConfig() Config {
const minLevel = "info"
2020-11-15 01:49:44 +00:00
const RollingMaxSize = 1 // Mb
const RollingMaxBackups = 5 // files
const RollingMaxAge = 0 // Keep forever
const defaultLogFilename = "cloudflared.log"
2020-11-15 01:49:44 +00:00
return Config{
ConsoleConfig: &ConsoleConfig{
noColor: false,
},
FileConfig: &FileConfig{
Dirname: "",
Filename: defaultLogFilename,
2020-11-15 01:49:44 +00:00
},
RollingConfig: &RollingConfig{
Dirname: "",
Filename: defaultLogFilename,
2020-11-15 01:49:44 +00:00
maxSize: RollingMaxSize,
maxBackups: RollingMaxBackups,
maxAge: RollingMaxAge,
},
MinLevel: minLevel,
}
}
func CreateConfig(
minLevel string,
disableTerminal bool,
rollingLogPath, nonRollingLogFilePath string,
2020-11-15 01:49:44 +00:00
) *Config {
var console *ConsoleConfig
if !disableTerminal {
console = createConsoleConfig()
}
var file *FileConfig
var rolling *RollingConfig
if rollingLogPath != "" {
rolling = createRollingConfig(rollingLogPath)
} else if nonRollingLogFilePath != "" {
file = createFileConfig(nonRollingLogFilePath)
2020-11-15 01:49:44 +00:00
}
if minLevel == "" {
minLevel = defaultConfig.MinLevel
}
return &Config{
ConsoleConfig: console,
FileConfig: file,
RollingConfig: rolling,
MinLevel: minLevel,
}
}
func createConsoleConfig() *ConsoleConfig {
return &ConsoleConfig{
noColor: false,
}
}
func createFileConfig(fullpath string) *FileConfig {
if fullpath == "" {
return defaultConfig.FileConfig
2020-11-15 01:49:44 +00:00
}
dirname, filename := filepath.Split(fullpath)
2020-11-15 01:49:44 +00:00
return &FileConfig{
Dirname: dirname,
Filename: filename,
2020-11-15 01:49:44 +00:00
}
}
func createRollingConfig(directory string) *RollingConfig {
2020-11-15 01:49:44 +00:00
if directory == "" {
directory = defaultConfig.RollingConfig.Dirname
2020-11-15 01:49:44 +00:00
}
return &RollingConfig{
Dirname: directory,
Filename: defaultConfig.RollingConfig.Filename,
2020-11-15 01:49:44 +00:00
maxSize: defaultConfig.RollingConfig.maxSize,
maxBackups: defaultConfig.RollingConfig.maxBackups,
maxAge: defaultConfig.RollingConfig.maxAge,
}
}