mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 19:09:58 +00:00
TUN-3561: Unified logger configuration
This commit is contained in:

committed by
Arég Harutyunyan

parent
78cb60b85f
commit
cad58b9b57
118
logger/configuration.go
Normal file
118
logger/configuration.go
Normal file
@@ -0,0 +1,118 @@
|
||||
package logger
|
||||
|
||||
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 {
|
||||
Filepath string
|
||||
}
|
||||
|
||||
type RollingConfig struct {
|
||||
Directory string
|
||||
Filename string
|
||||
|
||||
maxSize int // megabytes
|
||||
maxBackups int // files
|
||||
maxAge int // days
|
||||
}
|
||||
|
||||
func createDefaultConfig() Config {
|
||||
const minLevel = "fatal"
|
||||
|
||||
const RollingMaxSize = 1 // Mb
|
||||
const RollingMaxBackups = 5 // files
|
||||
const RollingMaxAge = 0 // Keep forever
|
||||
const rollingLogFilename = "cloudflared.log"
|
||||
|
||||
return Config{
|
||||
ConsoleConfig: &ConsoleConfig{
|
||||
noColor: false,
|
||||
},
|
||||
FileConfig: &FileConfig{
|
||||
Filepath: "",
|
||||
},
|
||||
RollingConfig: &RollingConfig{
|
||||
Directory: "",
|
||||
Filename: rollingLogFilename,
|
||||
maxSize: RollingMaxSize,
|
||||
maxBackups: RollingMaxBackups,
|
||||
maxAge: RollingMaxAge,
|
||||
},
|
||||
MinLevel: minLevel,
|
||||
}
|
||||
}
|
||||
|
||||
func CreateConfig(
|
||||
minLevel string,
|
||||
disableTerminal bool,
|
||||
rollingLogPath, nonRollingLogFilePath string,
|
||||
) *Config {
|
||||
var console *ConsoleConfig
|
||||
if !disableTerminal {
|
||||
console = createConsoleConfig()
|
||||
}
|
||||
|
||||
var file *FileConfig
|
||||
if nonRollingLogFilePath != "" {
|
||||
file = createFileConfig(nonRollingLogFilePath)
|
||||
}
|
||||
|
||||
var rolling *RollingConfig
|
||||
if rollingLogPath != "" {
|
||||
rolling = createRollingConfig(rollingLogPath)
|
||||
}
|
||||
|
||||
if minLevel == "" {
|
||||
minLevel = defaultConfig.MinLevel
|
||||
}
|
||||
|
||||
return &Config{
|
||||
ConsoleConfig: console,
|
||||
FileConfig: file,
|
||||
RollingConfig: rolling,
|
||||
|
||||
MinLevel: minLevel,
|
||||
}
|
||||
}
|
||||
|
||||
func createConsoleConfig() *ConsoleConfig {
|
||||
return &ConsoleConfig{
|
||||
noColor: false,
|
||||
}
|
||||
}
|
||||
|
||||
func createFileConfig(filepath string) *FileConfig {
|
||||
if filepath == "" {
|
||||
filepath = defaultConfig.FileConfig.Filepath
|
||||
}
|
||||
|
||||
return &FileConfig{
|
||||
Filepath: filepath,
|
||||
}
|
||||
}
|
||||
|
||||
func createRollingConfig(directory string) *RollingConfig {
|
||||
if directory == "" {
|
||||
directory = defaultConfig.RollingConfig.Directory
|
||||
}
|
||||
|
||||
return &RollingConfig{
|
||||
Directory: directory,
|
||||
Filename: defaultConfig.RollingConfig.Filename,
|
||||
maxSize: defaultConfig.RollingConfig.maxSize,
|
||||
maxBackups: defaultConfig.RollingConfig.maxBackups,
|
||||
maxAge: defaultConfig.RollingConfig.maxAge,
|
||||
}
|
||||
}
|
@@ -8,6 +8,20 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/alecthomas/units"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
const (
|
||||
EnableTerminalLog = false
|
||||
DisableTerminalLog = true
|
||||
|
||||
LogLevelFlag = "loglevel"
|
||||
LogFileFlag = "logfile"
|
||||
LogDirectoryFlag = "log-directory"
|
||||
LogTransportLevelFlag = "transport-loglevel"
|
||||
|
||||
LogSSHDirectoryFlag = "log-directory"
|
||||
LogSSHLevelFlag = "log-level"
|
||||
)
|
||||
|
||||
// Option is to encaspulate actions that will be called by Parse and run later to build an Options struct
|
||||
@@ -127,6 +141,63 @@ func New(opts ...Option) (*OutputWriter, error) {
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func NewInHouse(loggerConfig *Config) (*OutputWriter, error) {
|
||||
var loggerOpts []Option
|
||||
|
||||
var logPath string
|
||||
if loggerConfig.FileConfig != nil {
|
||||
logPath = loggerConfig.FileConfig.Filepath
|
||||
}
|
||||
if logPath == "" && loggerConfig.RollingConfig != nil {
|
||||
logPath = loggerConfig.RollingConfig.Directory
|
||||
}
|
||||
|
||||
if logPath != "" {
|
||||
loggerOpts = append(loggerOpts, DefaultFile(logPath))
|
||||
}
|
||||
|
||||
loggerOpts = append(loggerOpts, LogLevelString(loggerConfig.MinLevel))
|
||||
|
||||
if loggerConfig.ConsoleConfig == nil {
|
||||
disableOption := DisableTerminal(true)
|
||||
loggerOpts = append(loggerOpts, disableOption)
|
||||
}
|
||||
|
||||
l, err := New(loggerOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func CreateTransportLoggerFromContext(c *cli.Context, disableTerminal bool) (*OutputWriter, error) {
|
||||
return createFromContext(c, LogTransportLevelFlag, LogDirectoryFlag, disableTerminal)
|
||||
}
|
||||
|
||||
func CreateLoggerFromContext(c *cli.Context, disableTerminal bool) (*OutputWriter, error) {
|
||||
return createFromContext(c, LogLevelFlag, LogDirectoryFlag, disableTerminal)
|
||||
}
|
||||
|
||||
func CreateSSHLoggerFromContext(c *cli.Context, disableTerminal bool) (*OutputWriter, error) {
|
||||
return createFromContext(c, LogSSHLevelFlag, LogSSHDirectoryFlag, disableTerminal)
|
||||
}
|
||||
|
||||
func createFromContext(
|
||||
c *cli.Context,
|
||||
logLevelFlagName,
|
||||
logDirectoryFlagName string,
|
||||
disableTerminal bool,
|
||||
) (*OutputWriter, error) {
|
||||
logLevel := c.String(logLevelFlagName)
|
||||
logFile := c.String(LogFileFlag)
|
||||
logDirectory := c.String(logDirectoryFlagName)
|
||||
|
||||
loggerConfig := CreateConfig(logLevel, disableTerminal, logDirectory, logFile)
|
||||
|
||||
return NewInHouse(loggerConfig)
|
||||
}
|
||||
|
||||
// ParseLevelString returns the expected log levels based on the cmd flag
|
||||
func ParseLevelString(lvl string) ([]Level, error) {
|
||||
switch strings.ToLower(lvl) {
|
||||
|
Reference in New Issue
Block a user