github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/log/logger.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package log 7 8 import "os" 9 10 // Logger is the basic interface for logging 11 type Logger interface { 12 LevelLogger 13 Trace(format string, v ...interface{}) 14 IsTrace() bool 15 Debug(format string, v ...interface{}) 16 IsDebug() bool 17 Info(format string, v ...interface{}) 18 IsInfo() bool 19 Warn(format string, v ...interface{}) 20 IsWarn() bool 21 Error(format string, v ...interface{}) 22 ErrorWithSkip(skip int, format string, v ...interface{}) 23 IsError() bool 24 Critical(format string, v ...interface{}) 25 CriticalWithSkip(skip int, format string, v ...interface{}) 26 IsCritical() bool 27 Fatal(format string, v ...interface{}) 28 FatalWithSkip(skip int, format string, v ...interface{}) 29 IsFatal() bool 30 } 31 32 // LevelLogger is the simplest logging interface 33 type LevelLogger interface { 34 Flush() 35 Close() 36 GetLevel() Level 37 Log(skip int, level Level, format string, v ...interface{}) error 38 } 39 40 // SettableLogger is the interface of loggers which have subloggers 41 type SettableLogger interface { 42 SetLogger(name, provider, config string) error 43 DelLogger(name string) (bool, error) 44 } 45 46 // StacktraceLogger is a logger that can log stacktraces 47 type StacktraceLogger interface { 48 GetStacktraceLevel() Level 49 } 50 51 // LevelLoggerLogger wraps a LevelLogger as a Logger 52 type LevelLoggerLogger struct { 53 LevelLogger 54 } 55 56 // Trace records trace log 57 func (l *LevelLoggerLogger) Trace(format string, v ...interface{}) { 58 l.Log(1, TRACE, format, v...) 59 } 60 61 // IsTrace returns true if the logger is TRACE 62 func (l *LevelLoggerLogger) IsTrace() bool { 63 return l.GetLevel() <= TRACE 64 } 65 66 // Debug records debug log 67 func (l *LevelLoggerLogger) Debug(format string, v ...interface{}) { 68 l.Log(1, DEBUG, format, v...) 69 } 70 71 // IsDebug returns true if the logger is DEBUG 72 func (l *LevelLoggerLogger) IsDebug() bool { 73 return l.GetLevel() <= DEBUG 74 } 75 76 // Info records information log 77 func (l *LevelLoggerLogger) Info(format string, v ...interface{}) { 78 l.Log(1, INFO, format, v...) 79 } 80 81 // IsInfo returns true if the logger is INFO 82 func (l *LevelLoggerLogger) IsInfo() bool { 83 return l.GetLevel() <= INFO 84 } 85 86 // Warn records warning log 87 func (l *LevelLoggerLogger) Warn(format string, v ...interface{}) { 88 l.Log(1, WARN, format, v...) 89 } 90 91 // IsWarn returns true if the logger is WARN 92 func (l *LevelLoggerLogger) IsWarn() bool { 93 return l.GetLevel() <= WARN 94 } 95 96 // Error records error log 97 func (l *LevelLoggerLogger) Error(format string, v ...interface{}) { 98 l.Log(1, ERROR, format, v...) 99 } 100 101 // ErrorWithSkip records error log from "skip" calls back from this function 102 func (l *LevelLoggerLogger) ErrorWithSkip(skip int, format string, v ...interface{}) { 103 l.Log(skip+1, ERROR, format, v...) 104 } 105 106 // IsError returns true if the logger is ERROR 107 func (l *LevelLoggerLogger) IsError() bool { 108 return l.GetLevel() <= ERROR 109 } 110 111 // Critical records critical log 112 func (l *LevelLoggerLogger) Critical(format string, v ...interface{}) { 113 l.Log(1, CRITICAL, format, v...) 114 } 115 116 // CriticalWithSkip records critical log from "skip" calls back from this function 117 func (l *LevelLoggerLogger) CriticalWithSkip(skip int, format string, v ...interface{}) { 118 l.Log(skip+1, CRITICAL, format, v...) 119 } 120 121 // IsCritical returns true if the logger is CRITICAL 122 func (l *LevelLoggerLogger) IsCritical() bool { 123 return l.GetLevel() <= CRITICAL 124 } 125 126 // Fatal records fatal log and exit the process 127 func (l *LevelLoggerLogger) Fatal(format string, v ...interface{}) { 128 l.Log(1, FATAL, format, v...) 129 l.Close() 130 os.Exit(1) 131 } 132 133 // FatalWithSkip records fatal log from "skip" calls back from this function and exits the process 134 func (l *LevelLoggerLogger) FatalWithSkip(skip int, format string, v ...interface{}) { 135 l.Log(skip+1, FATAL, format, v...) 136 l.Close() 137 os.Exit(1) 138 } 139 140 // IsFatal returns true if the logger is FATAL 141 func (l *LevelLoggerLogger) IsFatal() bool { 142 return l.GetLevel() <= FATAL 143 }