github.com/ncw/rclone@v1.48.1-0.20190724201158-a35aa1360e3e/fs/log.go (about) 1 package fs 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/pkg/errors" 8 ) 9 10 // LogLevel describes rclone's logs. These are a subset of the syslog log levels. 11 type LogLevel byte 12 13 // Log levels. These are the syslog levels of which we only use a 14 // subset. 15 // 16 // LOG_EMERG system is unusable 17 // LOG_ALERT action must be taken immediately 18 // LOG_CRIT critical conditions 19 // LOG_ERR error conditions 20 // LOG_WARNING warning conditions 21 // LOG_NOTICE normal, but significant, condition 22 // LOG_INFO informational message 23 // LOG_DEBUG debug-level message 24 const ( 25 LogLevelEmergency LogLevel = iota 26 LogLevelAlert 27 LogLevelCritical 28 LogLevelError // Error - can't be suppressed 29 LogLevelWarning 30 LogLevelNotice // Normal logging, -q suppresses 31 LogLevelInfo // Transfers, needs -v 32 LogLevelDebug // Debug level, needs -vv 33 ) 34 35 var logLevelToString = []string{ 36 LogLevelEmergency: "EMERGENCY", 37 LogLevelAlert: "ALERT", 38 LogLevelCritical: "CRITICAL", 39 LogLevelError: "ERROR", 40 LogLevelWarning: "WARNING", 41 LogLevelNotice: "NOTICE", 42 LogLevelInfo: "INFO", 43 LogLevelDebug: "DEBUG", 44 } 45 46 // String turns a LogLevel into a string 47 func (l LogLevel) String() string { 48 if l >= LogLevel(len(logLevelToString)) { 49 return fmt.Sprintf("LogLevel(%d)", l) 50 } 51 return logLevelToString[l] 52 } 53 54 // Set a LogLevel 55 func (l *LogLevel) Set(s string) error { 56 for n, name := range logLevelToString { 57 if s != "" && name == s { 58 *l = LogLevel(n) 59 return nil 60 } 61 } 62 return errors.Errorf("Unknown log level %q", s) 63 } 64 65 // Type of the value 66 func (l *LogLevel) Type() string { 67 return "string" 68 } 69 70 // LogPrint sends the text to the logger of level 71 var LogPrint = func(level LogLevel, text string) { 72 text = fmt.Sprintf("%-6s: %s", level, text) 73 _ = log.Output(4, text) 74 } 75 76 // LogPrintf produces a log string from the arguments passed in 77 func LogPrintf(level LogLevel, o interface{}, text string, args ...interface{}) { 78 out := fmt.Sprintf(text, args...) 79 if o != nil { 80 out = fmt.Sprintf("%v: %s", o, out) 81 } 82 LogPrint(level, out) 83 } 84 85 // LogLevelPrintf writes logs at the given level 86 func LogLevelPrintf(level LogLevel, o interface{}, text string, args ...interface{}) { 87 if Config.LogLevel >= level { 88 LogPrintf(level, o, text, args...) 89 } 90 } 91 92 // Errorf writes error log output for this Object or Fs. It 93 // should always be seen by the user. 94 func Errorf(o interface{}, text string, args ...interface{}) { 95 if Config.LogLevel >= LogLevelError { 96 LogPrintf(LogLevelError, o, text, args...) 97 } 98 } 99 100 // Logf writes log output for this Object or Fs. This should be 101 // considered to be Info level logging. It is the default level. By 102 // default rclone should not log very much so only use this for 103 // important things the user should see. The user can filter these 104 // out with the -q flag. 105 func Logf(o interface{}, text string, args ...interface{}) { 106 if Config.LogLevel >= LogLevelNotice { 107 LogPrintf(LogLevelNotice, o, text, args...) 108 } 109 } 110 111 // Infof writes info on transfers for this Object or Fs. Use this 112 // level for logging transfers, deletions and things which should 113 // appear with the -v flag. 114 func Infof(o interface{}, text string, args ...interface{}) { 115 if Config.LogLevel >= LogLevelInfo { 116 LogPrintf(LogLevelInfo, o, text, args...) 117 } 118 } 119 120 // Debugf writes debugging output for this Object or Fs. Use this for 121 // debug only. The user must have to specify -vv to see this. 122 func Debugf(o interface{}, text string, args ...interface{}) { 123 if Config.LogLevel >= LogLevelDebug { 124 LogPrintf(LogLevelDebug, o, text, args...) 125 } 126 } 127 128 // LogDirName returns an object for the logger, logging a root 129 // directory which would normally be "" as the Fs 130 func LogDirName(f Fs, dir string) interface{} { 131 if dir != "" { 132 return dir 133 } 134 return f 135 }