2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2022-01-06 21:07:09 +00:00
|
|
|
// LogFilePath is the path to write the logs to the user specified file.
|
2018-08-29 20:56:58 +00:00
|
|
|
LogFilePath string
|
|
|
|
|
2022-01-06 21:07:09 +00:00
|
|
|
// LogRotateDuration is the user specified time to rotate logs
|
2018-08-29 20:56:58 +00:00
|
|
|
LogRotateDuration time.Duration
|
|
|
|
|
2022-01-06 21:07:09 +00:00
|
|
|
// LogRotateBytes is the user specified byte limit to rotate logs
|
2018-08-29 20:56:58 +00:00
|
|
|
LogRotateBytes int
|
2019-07-19 21:36:34 +00:00
|
|
|
|
2022-01-06 21:07:09 +00:00
|
|
|
// LogRotateMaxFiles is the maximum number of past archived log files to keep
|
2019-07-19 21:36:34 +00:00
|
|
|
LogRotateMaxFiles int
|
2016-11-04 04:14:56 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 18:04:54 +00:00
|
|
|
// defaultRotateDuration is the default time taken by the agent to rotate logs
|
|
|
|
const defaultRotateDuration = 24 * time.Hour
|
2018-08-29 20:56:58 +00:00
|
|
|
|
2020-06-10 20:47:35 +00:00
|
|
|
type LogSetupErrorFn func(string)
|
|
|
|
|
2022-01-06 21:07:09 +00:00
|
|
|
// noErrorWriter is a wrapper to suppress errors when writing to w.
|
|
|
|
type noErrorWriter struct {
|
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w noErrorWriter) Write(p []byte) (n int, err error) {
|
|
|
|
_, _ = w.w.Write(p)
|
|
|
|
// We purposely return n == len(p) as if write was successful
|
|
|
|
return len(p), nil
|
|
|
|
}
|
|
|
|
|
2020-08-19 16:09:35 +00:00
|
|
|
// Setup logging from Config, and return an hclog Logger.
|
2016-11-04 04:14:56 +00:00
|
|
|
//
|
2020-08-19 16:09:35 +00:00
|
|
|
// Logs may be written to out, and optionally to syslog, and a file.
|
|
|
|
func Setup(config Config, out io.Writer) (hclog.InterceptLogger, error) {
|
2020-01-28 23:50:41 +00:00
|
|
|
if !ValidateLogLevel(config.LogLevel) {
|
2020-07-29 20:18:54 +00:00
|
|
|
return nil, fmt.Errorf("Invalid log level: %s. Valid log levels are: %v",
|
2020-06-10 20:47:35 +00:00
|
|
|
config.LogLevel,
|
|
|
|
allowedLogLevels)
|
2016-11-04 04:14:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 21:07:09 +00:00
|
|
|
// If out is os.Stdout and Consul is being run as a Windows Service, writes will
|
|
|
|
// fail silently, which may inadvertently prevent writes to other writers.
|
|
|
|
// noErrorWriter is used as a wrapper to suppress any errors when writing to out.
|
|
|
|
writers := []io.Writer{noErrorWriter{w: out}}
|
2020-08-19 16:09:35 +00:00
|
|
|
|
2016-11-04 04:14:56 +00:00
|
|
|
if config.EnableSyslog {
|
|
|
|
retries := 12
|
|
|
|
delay := 5 * time.Second
|
|
|
|
for i := 0; i <= retries; i++ {
|
2020-08-19 16:09:35 +00:00
|
|
|
syslog, err := gsyslog.NewLogger(gsyslog.LOG_NOTICE, config.SyslogFacility, "consul")
|
2017-04-21 01:59:42 +00:00
|
|
|
if err == nil {
|
2022-04-14 22:51:00 +00:00
|
|
|
writers = append(writers, &SyslogWrapper{l: syslog})
|
2016-11-04 04:14:56 +00:00
|
|
|
break
|
|
|
|
}
|
2017-04-21 01:59:42 +00:00
|
|
|
|
|
|
|
if i == retries {
|
|
|
|
timeout := time.Duration(retries) * delay
|
2020-07-29 20:18:54 +00:00
|
|
|
return nil, fmt.Errorf("Syslog setup did not succeed within timeout (%s).", timeout.String())
|
2017-04-21 01:59:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(delay)
|
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 fileName == "" {
|
|
|
|
fileName = "consul.log"
|
|
|
|
}
|
2020-10-22 18:04:54 +00:00
|
|
|
if config.LogRotateDuration == 0 {
|
|
|
|
config.LogRotateDuration = defaultRotateDuration
|
2018-08-29 20:56:58 +00:00
|
|
|
}
|
2019-07-19 21:36:34 +00:00
|
|
|
logFile := &LogFile{
|
2020-01-28 23:50:41 +00:00
|
|
|
fileName: fileName,
|
|
|
|
logPath: dir,
|
2020-10-22 18:04:54 +00:00
|
|
|
duration: config.LogRotateDuration,
|
|
|
|
MaxBytes: config.LogRotateBytes,
|
2020-01-28 23:50:41 +00:00
|
|
|
MaxFiles: config.LogRotateMaxFiles,
|
2019-07-19 21:36:34 +00:00
|
|
|
}
|
2020-10-22 18:31:51 +00:00
|
|
|
if err := logFile.pruneFiles(); err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to prune log files: %w", err)
|
|
|
|
}
|
2020-04-19 09:14:54 +00:00
|
|
|
if err := logFile.openNew(); err != nil {
|
2020-07-29 20:18:54 +00:00
|
|
|
return nil, fmt.Errorf("Failed to setup logging: %w", err)
|
2020-04-19 09:14:54 +00:00
|
|
|
}
|
2018-08-29 20:56:58 +00:00
|
|
|
writers = append(writers, logFile)
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:50:41 +00:00
|
|
|
logger := hclog.NewInterceptLogger(&hclog.LoggerOptions{
|
|
|
|
Level: LevelFromString(config.LogLevel),
|
|
|
|
Name: config.Name,
|
2020-07-29 20:18:54 +00:00
|
|
|
Output: io.MultiWriter(writers...),
|
2020-01-28 23:50:41 +00:00
|
|
|
JSONFormat: config.LogJSON,
|
|
|
|
})
|
2020-07-29 20:18:54 +00:00
|
|
|
return logger, nil
|
2016-11-04 04:14:56 +00:00
|
|
|
}
|