github.com/unidoc/unidoc@v2.2.0+incompatible/common/logging.go (about) 1 /* 2 * This file is subject to the terms and conditions defined in 3 * file 'LICENSE.md', which is part of this source code package. 4 */ 5 6 package common 7 8 import ( 9 "fmt" 10 "os" 11 "path/filepath" 12 "runtime" 13 ) 14 15 type Logger interface { 16 Error(format string, args ...interface{}) 17 Warning(format string, args ...interface{}) 18 Notice(format string, args ...interface{}) 19 Info(format string, args ...interface{}) 20 Debug(format string, args ...interface{}) 21 Trace(format string, args ...interface{}) 22 } 23 24 // Dummy Logger does nothing. 25 type DummyLogger struct{} 26 27 func (this DummyLogger) Error(format string, args ...interface{}) { 28 } 29 30 func (this DummyLogger) Warning(format string, args ...interface{}) { 31 } 32 33 func (this DummyLogger) Notice(format string, args ...interface{}) { 34 } 35 36 func (this DummyLogger) Info(format string, args ...interface{}) { 37 } 38 39 func (this DummyLogger) Debug(format string, args ...interface{}) { 40 } 41 42 func (this DummyLogger) Trace(format string, args ...interface{}) { 43 } 44 45 // Simple Console Logger that the tests use. 46 type LogLevel int 47 48 const ( 49 LogLevelTrace LogLevel = 5 50 LogLevelDebug LogLevel = 4 51 LogLevelInfo LogLevel = 3 52 LogLevelNotice LogLevel = 2 53 LogLevelWarning LogLevel = 1 54 LogLevelError LogLevel = 0 55 ) 56 57 type ConsoleLogger struct { 58 LogLevel LogLevel 59 } 60 61 func NewConsoleLogger(logLevel LogLevel) *ConsoleLogger { 62 logger := ConsoleLogger{} 63 logger.LogLevel = logLevel 64 return &logger 65 } 66 67 func (this ConsoleLogger) Error(format string, args ...interface{}) { 68 if this.LogLevel >= LogLevelError { 69 prefix := "[ERROR] " 70 this.output(os.Stdout, prefix, format, args...) 71 } 72 } 73 74 func (this ConsoleLogger) Warning(format string, args ...interface{}) { 75 if this.LogLevel >= LogLevelWarning { 76 prefix := "[WARNING] " 77 this.output(os.Stdout, prefix, format, args...) 78 } 79 } 80 81 func (this ConsoleLogger) Notice(format string, args ...interface{}) { 82 if this.LogLevel >= LogLevelNotice { 83 prefix := "[NOTICE] " 84 this.output(os.Stdout, prefix, format, args...) 85 } 86 } 87 88 func (this ConsoleLogger) Info(format string, args ...interface{}) { 89 if this.LogLevel >= LogLevelInfo { 90 prefix := "[INFO] " 91 this.output(os.Stdout, prefix, format, args...) 92 } 93 } 94 95 func (this ConsoleLogger) Debug(format string, args ...interface{}) { 96 if this.LogLevel >= LogLevelDebug { 97 prefix := "[DEBUG] " 98 this.output(os.Stdout, prefix, format, args...) 99 } 100 } 101 102 func (this ConsoleLogger) Trace(format string, args ...interface{}) { 103 if this.LogLevel >= LogLevelTrace { 104 prefix := "[TRACE] " 105 this.output(os.Stdout, prefix, format, args...) 106 } 107 } 108 109 var Log Logger = DummyLogger{} 110 111 func SetLogger(logger Logger) { 112 Log = logger 113 } 114 115 // output writes `format`, `args` log message prefixed by the source file name, line and `prefix` 116 func (this ConsoleLogger) output(f *os.File, prefix string, format string, args ...interface{}) { 117 _, file, line, ok := runtime.Caller(3) 118 if !ok { 119 file = "???" 120 line = 0 121 } else { 122 file = filepath.Base(file) 123 } 124 125 src := fmt.Sprintf("%s %s:%d ", prefix, file, line) + format + "\n" 126 fmt.Fprintf(f, src, args...) 127 }