1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
package dlog
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
)
type Severity int32
type globals struct {
sync.Mutex
logLevel Severity
useSyslog *bool
truncateLogFile *bool
appName string
syslogFacility string
systemLogger *systemLogger
fileName *string
outFd *os.File
lastMessage string
lastOccurrence time.Time
occurrences uint64
}
var _globals = globals{
logLevel: SeverityLast,
appName: "-",
lastMessage: "",
lastOccurrence: time.Now(),
occurrences: 0,
}
const (
SeverityDebug Severity = iota
SeverityInfo
SeverityNotice
SeverityWarning
SeverityError
SeverityCritical
SeverityFatal
SeverityLast
)
const (
floodDelay = 5 * time.Second
floodMinRepeats = 3
)
var SeverityName = []string{
SeverityDebug: "DEBUG",
SeverityInfo: "INFO",
SeverityNotice: "NOTICE",
SeverityWarning: "WARNING",
SeverityError: "ERROR",
SeverityCritical: "CRITICAL",
SeverityFatal: "FATAL",
}
func Debugf(format string, args ...interface{}) {
logf(SeverityDebug, format, args...)
}
func Infof(format string, args ...interface{}) {
logf(SeverityInfo, format, args...)
}
func Noticef(format string, args ...interface{}) {
logf(SeverityNotice, format, args...)
}
func Warnf(format string, args ...interface{}) {
logf(SeverityWarning, format, args...)
}
func Errorf(format string, args ...interface{}) {
logf(SeverityError, format, args...)
}
func Criticalf(format string, args ...interface{}) {
logf(SeverityCritical, format, args...)
}
func Fatalf(format string, args ...interface{}) {
logf(SeverityFatal, format, args...)
}
func Debug(message interface{}) {
log(SeverityDebug, message)
}
func Info(message interface{}) {
log(SeverityInfo, message)
}
func Notice(message interface{}) {
log(SeverityNotice, message)
}
func Warn(message interface{}) {
log(SeverityWarning, message)
}
func Error(message interface{}) {
log(SeverityError, message)
}
func Critical(message interface{}) {
log(SeverityCritical, message)
}
func Fatal(message interface{}) {
log(SeverityFatal, message)
}
func (s *Severity) get() Severity {
return Severity(atomic.LoadInt32((*int32)(s)))
}
func (s *Severity) set(val Severity) {
atomic.StoreInt32((*int32)(s), int32(val))
}
func (s *Severity) String() string {
return strconv.FormatInt(int64(*s), 10)
}
func (s *Severity) Get() interface{} {
return s.get()
}
func (s *Severity) Set(strVal string) error {
val, _ := strconv.Atoi(strVal)
s.set(Severity(val))
return nil
}
func Init(appName string, logLevel Severity, syslogFacility string) error {
_globals.logLevel.set(logLevel)
if len(syslogFacility) == 0 {
syslogFacility = "DAEMON"
}
_globals.appName = appName
_globals.syslogFacility = syslogFacility
_globals.useSyslog = flag.Bool("syslog", false, "Send application logs to the local system logger (Eventlog on Windows, syslog on Unix)")
_globals.fileName = flag.String("logfile", "", "Write application logs to file")
_globals.truncateLogFile = flag.Bool("logfile-truncate", false, "Truncate the application log file; keep only data from the most recent application launch")
flag.Var(&_globals.logLevel, "loglevel", fmt.Sprintf("Application log level (%d-%d)", SeverityDebug, SeverityFatal))
return nil
}
func LogLevel() Severity {
_globals.Lock()
logLevel := _globals.logLevel.get()
_globals.Unlock()
return logLevel
}
func SetLogLevel(logLevel Severity) {
_globals.Lock()
_globals.logLevel.set(logLevel)
_globals.Unlock()
}
func UseSyslog(value bool) {
_globals.Lock()
_globals.useSyslog = &value
_globals.Unlock()
}
func TruncateLogFile(value bool) {
_globals.Lock()
_globals.truncateLogFile = &value
_globals.Unlock()
}
func UseLogFile(fileName string) {
_globals.Lock()
_globals.fileName = &fileName
_globals.Unlock()
}
func GetFileDescriptor() *os.File {
_globals.Lock()
createFileDescriptor()
_globals.Unlock()
return _globals.outFd
}
func SetFileDescriptor(fd *os.File) {
_globals.Lock()
_globals.outFd = fd
_globals.Unlock()
}
func createFileDescriptor() {
if _globals.fileName != nil && len(*_globals.fileName) > 0 && _globals.outFd == nil {
mode := os.O_WRONLY | os.O_CREATE
if _globals.truncateLogFile != nil && *_globals.truncateLogFile {
mode |= os.O_TRUNC
} else {
mode |= os.O_APPEND
}
outFd, err := os.OpenFile(*_globals.fileName, mode, 0o644)
if err == nil {
_globals.outFd = outFd
}
}
}
func logf(severity Severity, format string, args ...interface{}) {
if severity < _globals.logLevel.get() {
return
}
now := time.Now().Local()
year, month, day := now.Date()
hour, minute, second := now.Clock()
message := fmt.Sprintf(format, args...)
message = strings.TrimSpace(strings.TrimSuffix(message, "\n"))
if len(message) <= 0 {
return
}
_globals.Lock()
defer _globals.Unlock()
if _globals.lastMessage == message {
if time.Since(_globals.lastOccurrence) < floodDelay {
_globals.occurrences++
if _globals.occurrences > floodMinRepeats {
return
}
}
} else {
_globals.occurrences = 0
_globals.lastMessage = message
}
_globals.lastOccurrence = now
if *_globals.useSyslog && _globals.systemLogger == nil {
systemLogger, err := newSystemLogger(_globals.appName, _globals.syslogFacility)
if err == nil {
_globals.systemLogger = systemLogger
}
}
createFileDescriptor()
if _globals.systemLogger != nil {
(*_globals.systemLogger).writeString(severity, message)
} else {
line := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d] [%s] %s\n", year, int(month), day, hour, minute, second, SeverityName[severity], message)
if _globals.outFd != nil {
_globals.outFd.WriteString(line)
_globals.outFd.Sync()
} else {
os.Stderr.WriteString(line)
}
}
if severity >= SeverityFatal {
os.Exit(255)
}
}
func log(severity Severity, args interface{}) {
logf(severity, "%v", args)
}
|