2020-11-15 01:49:44 +00:00
|
|
|
package logger
|
|
|
|
|
2021-01-13 23:13:23 +00:00
|
|
|
import (
|
|
|
|
"path/filepath"
|
|
|
|
)
|
2020-12-08 18:16:25 +00:00
|
|
|
|
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 {
|
2020-12-08 18:16:25 +00:00
|
|
|
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 {
|
2020-12-08 18:16:25 +00:00
|
|
|
Dirname string
|
|
|
|
Filename string
|
2020-11-15 01:49:44 +00:00
|
|
|
|
|
|
|
maxSize int // megabytes
|
|
|
|
maxBackups int // files
|
|
|
|
maxAge int // days
|
|
|
|
}
|
|
|
|
|
|
|
|
func createDefaultConfig() Config {
|
2020-11-25 06:55:13 +00:00
|
|
|
const minLevel = "info"
|
2020-11-15 01:49:44 +00:00
|
|
|
|
|
|
|
const RollingMaxSize = 1 // Mb
|
|
|
|
const RollingMaxBackups = 5 // files
|
|
|
|
const RollingMaxAge = 0 // Keep forever
|
2020-12-08 18:16:25 +00:00
|
|
|
const defaultLogFilename = "cloudflared.log"
|
2020-11-15 01:49:44 +00:00
|
|
|
|
|
|
|
return Config{
|
|
|
|
ConsoleConfig: &ConsoleConfig{
|
|
|
|
noColor: false,
|
|
|
|
},
|
|
|
|
FileConfig: &FileConfig{
|
2020-12-08 18:16:25 +00:00
|
|
|
Dirname: "",
|
|
|
|
Filename: defaultLogFilename,
|
2020-11-15 01:49:44 +00:00
|
|
|
},
|
|
|
|
RollingConfig: &RollingConfig{
|
2020-12-08 18:16:25 +00:00
|
|
|
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,
|
2020-12-08 18:16:25 +00:00
|
|
|
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 != "" {
|
2020-12-08 18:16:25 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 18:16:25 +00:00
|
|
|
func createFileConfig(fullpath string) *FileConfig {
|
|
|
|
if fullpath == "" {
|
|
|
|
return defaultConfig.FileConfig
|
2020-11-15 01:49:44 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 18:16:25 +00:00
|
|
|
dirname, filename := filepath.Split(fullpath)
|
|
|
|
|
2020-11-15 01:49:44 +00:00
|
|
|
return &FileConfig{
|
2020-12-08 18:16:25 +00:00
|
|
|
Dirname: dirname,
|
|
|
|
Filename: filename,
|
2020-11-15 01:49:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 18:16:25 +00:00
|
|
|
func createRollingConfig(directory string) *RollingConfig {
|
2020-11-15 01:49:44 +00:00
|
|
|
if directory == "" {
|
2020-12-08 18:16:25 +00:00
|
|
|
directory = defaultConfig.RollingConfig.Dirname
|
2020-11-15 01:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &RollingConfig{
|
2020-12-08 18:16:25 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|