2017-02-09 19:01:20 +08:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2020-11-05 19:33:32 +08:00
|
|
|
"context"
|
2017-02-09 19:01:20 +08:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2017-06-27 05:46:45 +08:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-07-26 08:54:09 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-02-09 19:01:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// LogLevel describes rclone's logs. These are a subset of the syslog log levels.
|
|
|
|
type LogLevel byte
|
|
|
|
|
2017-02-10 21:28:06 +08:00
|
|
|
// Log levels. These are the syslog levels of which we only use a
|
|
|
|
// subset.
|
|
|
|
//
|
|
|
|
// LOG_EMERG system is unusable
|
|
|
|
// LOG_ALERT action must be taken immediately
|
|
|
|
// LOG_CRIT critical conditions
|
|
|
|
// LOG_ERR error conditions
|
|
|
|
// LOG_WARNING warning conditions
|
|
|
|
// LOG_NOTICE normal, but significant, condition
|
|
|
|
// LOG_INFO informational message
|
|
|
|
// LOG_DEBUG debug-level message
|
2017-02-09 19:01:20 +08:00
|
|
|
const (
|
2017-02-10 05:22:46 +08:00
|
|
|
LogLevelEmergency LogLevel = iota
|
2017-02-09 19:01:20 +08:00
|
|
|
LogLevelAlert
|
|
|
|
LogLevelCritical
|
|
|
|
LogLevelError // Error - can't be suppressed
|
|
|
|
LogLevelWarning
|
|
|
|
LogLevelNotice // Normal logging, -q suppresses
|
|
|
|
LogLevelInfo // Transfers, needs -v
|
|
|
|
LogLevelDebug // Debug level, needs -vv
|
|
|
|
)
|
|
|
|
|
2017-02-10 21:28:06 +08:00
|
|
|
var logLevelToString = []string{
|
|
|
|
LogLevelEmergency: "EMERGENCY",
|
|
|
|
LogLevelAlert: "ALERT",
|
|
|
|
LogLevelCritical: "CRITICAL",
|
|
|
|
LogLevelError: "ERROR",
|
|
|
|
LogLevelWarning: "WARNING",
|
|
|
|
LogLevelNotice: "NOTICE",
|
|
|
|
LogLevelInfo: "INFO",
|
|
|
|
LogLevelDebug: "DEBUG",
|
|
|
|
}
|
2017-02-09 19:01:20 +08:00
|
|
|
|
2017-02-10 21:28:06 +08:00
|
|
|
// String turns a LogLevel into a string
|
|
|
|
func (l LogLevel) String() string {
|
|
|
|
if l >= LogLevel(len(logLevelToString)) {
|
|
|
|
return fmt.Sprintf("LogLevel(%d)", l)
|
|
|
|
}
|
|
|
|
return logLevelToString[l]
|
|
|
|
}
|
2017-02-09 19:01:20 +08:00
|
|
|
|
2017-06-27 05:46:45 +08:00
|
|
|
// Set a LogLevel
|
|
|
|
func (l *LogLevel) Set(s string) error {
|
|
|
|
for n, name := range logLevelToString {
|
|
|
|
if s != "" && name == s {
|
|
|
|
*l = LogLevel(n)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.Errorf("Unknown log level %q", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type of the value
|
|
|
|
func (l *LogLevel) Type() string {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
2020-12-12 01:48:09 +08:00
|
|
|
// UnmarshalJSON makes sure the value can be parsed as a string or integer in JSON
|
|
|
|
func (l *LogLevel) UnmarshalJSON(in []byte) error {
|
|
|
|
return UnmarshalJSONFlag(in, l, func(i int64) error {
|
|
|
|
if i < 0 || i >= int64(LogLevel(len(logLevelToString))) {
|
|
|
|
return errors.Errorf("Unknown log level %d", i)
|
|
|
|
}
|
|
|
|
*l = (LogLevel)(i)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
// LogPrint sends the text to the logger of level
|
|
|
|
var LogPrint = func(level LogLevel, text string) {
|
2020-12-17 19:55:27 +08:00
|
|
|
text = fmt.Sprintf("%-6s: %s", level, text)
|
2018-09-03 01:11:09 +08:00
|
|
|
_ = log.Output(4, text)
|
2017-02-10 21:28:06 +08:00
|
|
|
}
|
|
|
|
|
2020-04-12 01:02:50 +08:00
|
|
|
// LogValueItem describes keyed item for a JSON log entry
|
|
|
|
type LogValueItem struct {
|
2020-12-30 21:06:43 +08:00
|
|
|
key string
|
|
|
|
value interface{}
|
|
|
|
render bool
|
2020-04-12 01:02:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// LogValue should be used as an argument to any logging calls to
|
|
|
|
// augment the JSON output with more structured information.
|
|
|
|
//
|
|
|
|
// key is the dictionary parameter used to store value.
|
|
|
|
func LogValue(key string, value interface{}) LogValueItem {
|
2020-12-30 21:06:43 +08:00
|
|
|
return LogValueItem{key: key, value: value, render: true}
|
2020-04-12 01:02:50 +08:00
|
|
|
}
|
|
|
|
|
2020-12-30 21:06:43 +08:00
|
|
|
// LogValueHide should be used as an argument to any logging calls to
|
|
|
|
// augment the JSON output with more structured information.
|
|
|
|
//
|
|
|
|
// key is the dictionary parameter used to store value.
|
|
|
|
//
|
|
|
|
// String() will return a blank string - this is useful to put items
|
|
|
|
// in which don't print into the log.
|
|
|
|
func LogValueHide(key string, value interface{}) LogValueItem {
|
|
|
|
return LogValueItem{key: key, value: value, render: false}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the representation of value. If render is fals this
|
|
|
|
// is an empty string so LogValueItem entries won't show in the
|
|
|
|
// textual representation of logs.
|
2020-04-12 01:02:50 +08:00
|
|
|
func (j LogValueItem) String() string {
|
2020-12-30 21:06:43 +08:00
|
|
|
if !j.render {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if do, ok := j.value.(fmt.Stringer); ok {
|
|
|
|
return do.String()
|
|
|
|
}
|
|
|
|
return fmt.Sprint(j.value)
|
2020-04-12 01:02:50 +08:00
|
|
|
}
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
// LogPrintf produces a log string from the arguments passed in
|
|
|
|
func LogPrintf(level LogLevel, o interface{}, text string, args ...interface{}) {
|
2017-02-09 19:01:20 +08:00
|
|
|
out := fmt.Sprintf(text, args...)
|
2019-08-02 22:57:09 +08:00
|
|
|
|
2020-11-05 19:33:32 +08:00
|
|
|
if GetConfig(context.TODO()).UseJSONLog {
|
2019-08-02 22:57:09 +08:00
|
|
|
fields := logrus.Fields{}
|
|
|
|
if o != nil {
|
|
|
|
fields = logrus.Fields{
|
|
|
|
"object": fmt.Sprintf("%+v", o),
|
|
|
|
"objectType": fmt.Sprintf("%T", o),
|
|
|
|
}
|
|
|
|
}
|
2020-04-12 01:02:50 +08:00
|
|
|
for _, arg := range args {
|
|
|
|
if item, ok := arg.(LogValueItem); ok {
|
|
|
|
fields[item.key] = item.value
|
|
|
|
}
|
2020-02-16 05:21:01 +08:00
|
|
|
}
|
2019-07-26 08:54:09 +08:00
|
|
|
switch level {
|
|
|
|
case LogLevelDebug:
|
2019-08-02 22:57:09 +08:00
|
|
|
logrus.WithFields(fields).Debug(out)
|
2019-07-26 08:54:09 +08:00
|
|
|
case LogLevelInfo:
|
2019-08-02 22:57:09 +08:00
|
|
|
logrus.WithFields(fields).Info(out)
|
2019-07-26 08:54:09 +08:00
|
|
|
case LogLevelNotice, LogLevelWarning:
|
2019-08-02 22:57:09 +08:00
|
|
|
logrus.WithFields(fields).Warn(out)
|
2019-07-26 08:54:09 +08:00
|
|
|
case LogLevelError:
|
2019-08-02 22:57:09 +08:00
|
|
|
logrus.WithFields(fields).Error(out)
|
2019-07-26 08:54:09 +08:00
|
|
|
case LogLevelCritical:
|
2019-08-02 22:57:09 +08:00
|
|
|
logrus.WithFields(fields).Fatal(out)
|
2019-07-26 08:54:09 +08:00
|
|
|
case LogLevelEmergency, LogLevelAlert:
|
2019-08-02 22:57:09 +08:00
|
|
|
logrus.WithFields(fields).Panic(out)
|
2019-07-26 08:54:09 +08:00
|
|
|
}
|
|
|
|
} else {
|
2019-08-02 22:57:09 +08:00
|
|
|
if o != nil {
|
|
|
|
out = fmt.Sprintf("%v: %s", o, out)
|
|
|
|
}
|
2019-07-26 08:54:09 +08:00
|
|
|
LogPrint(level, out)
|
|
|
|
}
|
2017-02-09 19:01:20 +08:00
|
|
|
}
|
|
|
|
|
2017-06-27 05:46:45 +08:00
|
|
|
// LogLevelPrintf writes logs at the given level
|
|
|
|
func LogLevelPrintf(level LogLevel, o interface{}, text string, args ...interface{}) {
|
2020-11-05 19:33:32 +08:00
|
|
|
if GetConfig(context.TODO()).LogLevel >= level {
|
2018-01-13 00:30:54 +08:00
|
|
|
LogPrintf(level, o, text, args...)
|
2017-06-27 05:46:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 19:01:20 +08:00
|
|
|
// Errorf writes error log output for this Object or Fs. It
|
2017-02-10 21:28:06 +08:00
|
|
|
// should always be seen by the user.
|
2017-02-09 19:01:20 +08:00
|
|
|
func Errorf(o interface{}, text string, args ...interface{}) {
|
2020-11-05 19:33:32 +08:00
|
|
|
if GetConfig(context.TODO()).LogLevel >= LogLevelError {
|
2018-01-13 00:30:54 +08:00
|
|
|
LogPrintf(LogLevelError, o, text, args...)
|
2017-02-10 05:22:46 +08:00
|
|
|
}
|
2017-02-09 19:01:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Logf writes log output for this Object or Fs. This should be
|
2017-02-10 05:22:46 +08:00
|
|
|
// considered to be Info level logging. It is the default level. By
|
|
|
|
// default rclone should not log very much so only use this for
|
|
|
|
// important things the user should see. The user can filter these
|
|
|
|
// out with the -q flag.
|
2017-02-09 19:01:20 +08:00
|
|
|
func Logf(o interface{}, text string, args ...interface{}) {
|
2020-11-05 19:33:32 +08:00
|
|
|
if GetConfig(context.TODO()).LogLevel >= LogLevelNotice {
|
2018-01-13 00:30:54 +08:00
|
|
|
LogPrintf(LogLevelNotice, o, text, args...)
|
2017-02-09 19:01:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 05:22:46 +08:00
|
|
|
// Infof writes info on transfers for this Object or Fs. Use this
|
|
|
|
// level for logging transfers, deletions and things which should
|
|
|
|
// appear with the -v flag.
|
2017-02-09 19:01:20 +08:00
|
|
|
func Infof(o interface{}, text string, args ...interface{}) {
|
2020-11-05 19:33:32 +08:00
|
|
|
if GetConfig(context.TODO()).LogLevel >= LogLevelInfo {
|
2018-01-13 00:30:54 +08:00
|
|
|
LogPrintf(LogLevelInfo, o, text, args...)
|
2017-02-09 19:01:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 05:22:46 +08:00
|
|
|
// Debugf writes debugging output for this Object or Fs. Use this for
|
|
|
|
// debug only. The user must have to specify -vv to see this.
|
2017-02-09 19:01:20 +08:00
|
|
|
func Debugf(o interface{}, text string, args ...interface{}) {
|
2020-11-05 19:33:32 +08:00
|
|
|
if GetConfig(context.TODO()).LogLevel >= LogLevelDebug {
|
2018-01-13 00:30:54 +08:00
|
|
|
LogPrintf(LogLevelDebug, o, text, args...)
|
2017-05-09 18:38:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 00:30:54 +08:00
|
|
|
// LogDirName returns an object for the logger, logging a root
|
|
|
|
// directory which would normally be "" as the Fs
|
|
|
|
func LogDirName(f Fs, dir string) interface{} {
|
|
|
|
if dir != "" {
|
|
|
|
return dir
|
2017-02-09 19:01:20 +08:00
|
|
|
}
|
2018-01-13 00:30:54 +08:00
|
|
|
return f
|
2017-02-09 19:01:20 +08:00
|
|
|
}
|