2020-01-28 23:50:41 +00:00
|
|
|
package logging
|
2016-11-04 04:14:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2018-08-29 20:56:58 +00:00
|
|
|
"path/filepath"
|
2016-11-04 04:14:56 +00:00
|
|
|
"time"
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
gsyslog "github.com/hashicorp/go-syslog"
|
2016-11-04 04:14:56 +00:00
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is used to set up logging.
|
|
|
|
type Config struct {
|
|
|
|
// LogLevel is the minimum level to be logged.
|
|
|
|
LogLevel string
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
// LogJSON controls outputing logs in a JSON format.
|
|
|
|
LogJSON bool
|
|
|
|
|
|
|
|
// Name is the name the returned logger will use to prefix log lines.
|
|
|
|
Name string
|
|
|
|
|
2016-11-04 04:14:56 +00:00
|
|
|
// EnableSyslog controls forwarding to syslog.
|
|
|
|
EnableSyslog bool
|
|
|
|
|
|
|
|
// SyslogFacility is the destination for syslog forwarding.
|
|
|
|
SyslogFacility string
|
2018-08-29 20:56:58 +00:00
|
|
|
|
|
|
|
//LogFilePath is the path to write the logs to the user specified file.
|
|
|
|
LogFilePath string
|
|
|
|
|
|
|
|
//LogRotateDuration is the user specified time to rotate logs
|
|
|
|
LogRotateDuration time.Duration
|
|
|
|
|
|
|
|
//LogRotateBytes is the user specified byte limit to rotate logs
|
|
|
|
LogRotateBytes int
|
2019-07-19 21:36:34 +00:00
|
|
|
|
|
|
|
//LogRotateMaxFiles is the maximum number of past archived log files to keep
|
|
|
|
LogRotateMaxFiles int
|
2016-11-04 04:14:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 20:56:58 +00:00
|
|
|
const (
|
|
|
|
// defaultRotateDuration is the default time taken by the agent to rotate logs
|
|
|
|
defaultRotateDuration = 24 * time.Hour
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
logRotateDuration time.Duration
|
|
|
|
logRotateBytes int
|
|
|
|
)
|
|
|
|
|
2016-11-04 04:14:56 +00:00
|
|
|
// Setup is used to perform setup of several logging objects:
|
|
|
|
//
|
2020-01-28 23:50:41 +00:00
|
|
|
// * A hclog.Logger is used to perform filtering by log level and write to io.Writer.
|
2016-11-04 04:14:56 +00:00
|
|
|
// * A GatedWriter is used to buffer logs until startup UI operations are
|
|
|
|
// complete. After this is flushed then logs flow directly to output
|
|
|
|
// destinations.
|
|
|
|
// * An io.Writer is provided as the sink for all logs to flow to.
|
|
|
|
//
|
|
|
|
// The provided ui object will get any log messages related to setting up
|
|
|
|
// logging itself, and will also be hooked up to the gated logger. The final bool
|
|
|
|
// parameter indicates if logging was set up successfully.
|
2020-01-28 23:50:41 +00:00
|
|
|
func Setup(config *Config, ui cli.Ui) (hclog.InterceptLogger, *GatedWriter, io.Writer, bool) {
|
2016-11-04 04:14:56 +00:00
|
|
|
// The gated writer buffers logs at startup and holds until it's flushed.
|
|
|
|
logGate := &GatedWriter{
|
2017-03-23 23:05:35 +00:00
|
|
|
Writer: &cli.UiWriter{Ui: ui},
|
2016-11-04 04:14:56 +00:00
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
if !ValidateLogLevel(config.LogLevel) {
|
2016-11-04 04:14:56 +00:00
|
|
|
ui.Error(fmt.Sprintf(
|
|
|
|
"Invalid log level: %s. Valid log levels are: %v",
|
2020-01-28 23:50:41 +00:00
|
|
|
config.LogLevel, allowedLogLevels))
|
|
|
|
return nil, nil, nil, false
|
2016-11-04 04:14:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up syslog if it's enabled.
|
|
|
|
var syslog io.Writer
|
|
|
|
if config.EnableSyslog {
|
|
|
|
retries := 12
|
|
|
|
delay := 5 * time.Second
|
|
|
|
for i := 0; i <= retries; i++ {
|
|
|
|
l, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, config.SyslogFacility, "consul")
|
2017-04-21 01:59:42 +00:00
|
|
|
if err == nil {
|
2020-01-28 23:50:41 +00:00
|
|
|
syslog = &SyslogWrapper{l}
|
2016-11-04 04:14:56 +00:00
|
|
|
break
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
|
|
|
|
ui.Error(fmt.Sprintf("Syslog setup error: %v", err))
|
|
|
|
if i == retries {
|
|
|
|
timeout := time.Duration(retries) * delay
|
|
|
|
ui.Error(fmt.Sprintf("Syslog setup did not succeed within timeout (%s).", timeout.String()))
|
2020-01-28 23:50:41 +00:00
|
|
|
return nil, nil, nil, false
|
2017-04-21 01:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ui.Error(fmt.Sprintf("Retrying syslog setup in %s...", delay.String()))
|
|
|
|
time.Sleep(delay)
|
2016-11-04 04:14:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-28 23:50:41 +00:00
|
|
|
writers := []io.Writer{logGate}
|
2018-08-29 20:56:58 +00:00
|
|
|
|
2016-11-04 04:14:56 +00:00
|
|
|
var logOutput io.Writer
|
|
|
|
if syslog != nil {
|
2018-08-29 20:56:58 +00:00
|
|
|
writers = append(writers, syslog)
|
2016-11-04 04:14:56 +00:00
|
|
|
}
|
2018-08-29 20:56:58 +00:00
|
|
|
|
|
|
|
// Create a file logger if the user has specified the path to the log file
|
|
|
|
if config.LogFilePath != "" {
|
|
|
|
dir, fileName := filepath.Split(config.LogFilePath)
|
|
|
|
// If a path is provided but has no fileName a default is provided.
|
|
|
|
if fileName == "" {
|
|
|
|
fileName = "consul.log"
|
|
|
|
}
|
|
|
|
// Try to enter the user specified log rotation duration first
|
|
|
|
if config.LogRotateDuration != 0 {
|
|
|
|
logRotateDuration = config.LogRotateDuration
|
|
|
|
} else {
|
|
|
|
// Default to 24 hrs if no rotation period is specified
|
|
|
|
logRotateDuration = defaultRotateDuration
|
|
|
|
}
|
|
|
|
// User specified byte limit for log rotation if one is provided
|
|
|
|
if config.LogRotateBytes != 0 {
|
|
|
|
logRotateBytes = config.LogRotateBytes
|
|
|
|
}
|
2019-07-19 21:36:34 +00:00
|
|
|
logFile := &LogFile{
|
2020-01-28 23:50:41 +00:00
|
|
|
fileName: fileName,
|
|
|
|
logPath: dir,
|
|
|
|
duration: logRotateDuration,
|
|
|
|
MaxBytes: logRotateBytes,
|
|
|
|
MaxFiles: config.LogRotateMaxFiles,
|
2019-07-19 21:36:34 +00:00
|
|
|
}
|
2020-04-19 09:14:54 +00:00
|
|
|
if err := logFile.openNew(); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Failed to setup logging: %v", err))
|
|
|
|
return nil, nil, nil, false
|
|
|
|
}
|
2018-08-29 20:56:58 +00:00
|
|
|
writers = append(writers, logFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
logOutput = io.MultiWriter(writers...)
|
2020-01-28 23:50:41 +00:00
|
|
|
|
|
|
|
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
|
|
|
|
Level: LevelFromString(config.LogLevel),
|
|
|
|
Name: config.Name,
|
|
|
|
Output: logOutput,
|
|
|
|
JSONFormat: config.LogJSON,
|
|
|
|
})
|
|
|
|
|
|
|
|
return logger, logGate, logOutput, true
|
2016-11-04 04:14:56 +00:00
|
|
|
}
|