2014-02-13 01:24:09 +05:30
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2014-02-13 01:24:09 +05:30
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2020-07-06 05:37:07 +05:30
|
|
|
"fmt"
|
2014-05-26 05:41:25 +05:30
|
|
|
"os"
|
2014-07-26 09:54:27 +05:30
|
|
|
"runtime"
|
|
|
|
"strings"
|
2019-09-17 15:09:37 +05:30
|
|
|
"sync"
|
2023-02-04 04:41:48 +05:30
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/process"
|
2014-02-13 01:24:09 +05:30
|
|
|
)
|
|
|
|
|
2019-09-17 15:09:37 +05:30
|
|
|
type loggerMap struct {
|
|
|
|
sync.Map
|
|
|
|
}
|
|
|
|
|
2020-10-31 11:06:46 +05:30
|
|
|
func (m *loggerMap) Load(k string) (*MultiChannelledLogger, bool) {
|
2019-09-17 15:09:37 +05:30
|
|
|
v, ok := m.Map.Load(k)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
2020-10-31 11:06:46 +05:30
|
|
|
l, ok := v.(*MultiChannelledLogger)
|
2019-09-17 15:09:37 +05:30
|
|
|
return l, ok
|
|
|
|
}
|
|
|
|
|
2020-10-31 11:06:46 +05:30
|
|
|
func (m *loggerMap) Store(k string, v *MultiChannelledLogger) {
|
2019-09-17 15:09:37 +05:30
|
|
|
m.Map.Store(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *loggerMap) Delete(k string) {
|
|
|
|
m.Map.Delete(k)
|
|
|
|
}
|
|
|
|
|
2014-03-25 17:17:04 +05:30
|
|
|
var (
|
2019-04-02 13:18:31 +05:30
|
|
|
// DEFAULT is the name of the default logger
|
|
|
|
DEFAULT = "default"
|
|
|
|
// NamedLoggers map of named loggers
|
2019-09-17 15:09:37 +05:30
|
|
|
NamedLoggers loggerMap
|
2019-06-01 20:30:21 +05:30
|
|
|
prefix string
|
2014-03-25 17:17:04 +05:30
|
|
|
)
|
2014-02-13 01:24:09 +05:30
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// NewLogger create a logger for the default logger
|
2020-10-31 11:06:46 +05:30
|
|
|
func NewLogger(bufLen int64, name, provider, config string) *MultiChannelledLogger {
|
2019-04-02 13:18:31 +05:30
|
|
|
err := NewNamedLogger(DEFAULT, bufLen, name, provider, config)
|
|
|
|
if err != nil {
|
|
|
|
CriticalWithSkip(1, "Unable to create default logger: %v", err)
|
|
|
|
panic(err)
|
2014-05-12 00:07:12 +05:30
|
|
|
}
|
2019-09-17 15:09:37 +05:30
|
|
|
l, _ := NamedLoggers.Load(DEFAULT)
|
|
|
|
return l
|
2019-04-02 13:18:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// NewNamedLogger creates a new named logger for a given configuration
|
|
|
|
func NewNamedLogger(name string, bufLen int64, subname, provider, config string) error {
|
2019-09-17 15:09:37 +05:30
|
|
|
logger, ok := NamedLoggers.Load(name)
|
2019-04-02 13:18:31 +05:30
|
|
|
if !ok {
|
|
|
|
logger = newLogger(name, bufLen)
|
2019-09-17 15:09:37 +05:30
|
|
|
NamedLoggers.Store(name, logger)
|
2014-05-12 00:07:12 +05:30
|
|
|
}
|
2019-04-02 13:18:31 +05:30
|
|
|
|
|
|
|
return logger.SetLogger(subname, provider, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DelNamedLogger closes and deletes the named logger
|
|
|
|
func DelNamedLogger(name string) {
|
2019-09-17 15:09:37 +05:30
|
|
|
l, ok := NamedLoggers.Load(name)
|
2019-04-02 13:18:31 +05:30
|
|
|
if ok {
|
2019-09-17 15:09:37 +05:30
|
|
|
NamedLoggers.Delete(name)
|
2019-04-02 13:18:31 +05:30
|
|
|
l.Close()
|
2014-06-20 09:55:23 +05:30
|
|
|
}
|
2014-02-13 01:24:09 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// DelLogger removes the named sublogger from the default logger
|
|
|
|
func DelLogger(name string) error {
|
2019-09-17 15:09:37 +05:30
|
|
|
logger, _ := NamedLoggers.Load(DEFAULT)
|
2019-04-02 13:18:31 +05:30
|
|
|
found, err := logger.DelLogger(name)
|
|
|
|
if !found {
|
|
|
|
Trace("Log %s not found, no need to delete", name)
|
2017-01-17 11:32:35 +05:30
|
|
|
}
|
2019-04-02 13:18:31 +05:30
|
|
|
return err
|
|
|
|
}
|
2017-04-03 07:52:26 +05:30
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// GetLogger returns either a named logger or the default logger
|
2020-10-31 11:06:46 +05:30
|
|
|
func GetLogger(name string) *MultiChannelledLogger {
|
2019-09-17 15:09:37 +05:30
|
|
|
logger, ok := NamedLoggers.Load(name)
|
2019-04-02 13:18:31 +05:30
|
|
|
if ok {
|
|
|
|
return logger
|
|
|
|
}
|
2019-09-17 15:09:37 +05:30
|
|
|
logger, _ = NamedLoggers.Load(DEFAULT)
|
|
|
|
return logger
|
2017-01-17 11:32:35 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// GetLevel returns the minimum logger level
|
|
|
|
func GetLevel() Level {
|
2019-09-17 15:09:37 +05:30
|
|
|
l, _ := NamedLoggers.Load(DEFAULT)
|
|
|
|
return l.GetLevel()
|
2019-04-02 13:18:31 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetStacktraceLevel returns the minimum logger level
|
|
|
|
func GetStacktraceLevel() Level {
|
2019-09-17 15:09:37 +05:30
|
|
|
l, _ := NamedLoggers.Load(DEFAULT)
|
|
|
|
return l.GetStacktraceLevel()
|
2014-06-20 10:44:54 +05:30
|
|
|
}
|
|
|
|
|
2016-11-26 17:23:29 +05:30
|
|
|
// Trace records trace log
|
2014-02-19 04:18:02 +05:30
|
|
|
func Trace(format string, v ...interface{}) {
|
2019-04-02 13:18:31 +05:30
|
|
|
Log(1, TRACE, format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTrace returns true if at least one logger is TRACE
|
|
|
|
func IsTrace() bool {
|
|
|
|
return GetLevel() <= TRACE
|
2014-02-19 04:18:02 +05:30
|
|
|
}
|
|
|
|
|
2016-11-26 17:23:29 +05:30
|
|
|
// Debug records debug log
|
2014-03-23 21:46:17 +05:30
|
|
|
func Debug(format string, v ...interface{}) {
|
2019-04-02 13:18:31 +05:30
|
|
|
Log(1, DEBUG, format, v...)
|
2014-03-23 21:46:17 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// IsDebug returns true if at least one logger is DEBUG
|
|
|
|
func IsDebug() bool {
|
|
|
|
return GetLevel() <= DEBUG
|
2014-02-13 01:24:09 +05:30
|
|
|
}
|
2014-02-19 04:01:16 +05:30
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// Info records info log
|
|
|
|
func Info(format string, v ...interface{}) {
|
|
|
|
Log(1, INFO, format, v...)
|
2014-02-19 04:01:16 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// IsInfo returns true if at least one logger is INFO
|
|
|
|
func IsInfo() bool {
|
|
|
|
return GetLevel() <= INFO
|
2014-02-19 04:01:16 +05:30
|
|
|
}
|
2014-02-19 04:18:02 +05:30
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// Warn records warning log
|
|
|
|
func Warn(format string, v ...interface{}) {
|
|
|
|
Log(1, WARN, format, v...)
|
2014-02-19 04:18:02 +05:30
|
|
|
}
|
2014-05-26 05:41:25 +05:30
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// IsWarn returns true if at least one logger is WARN
|
|
|
|
func IsWarn() bool {
|
|
|
|
return GetLevel() <= WARN
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// Error records error log
|
|
|
|
func Error(format string, v ...interface{}) {
|
|
|
|
Log(1, ERROR, format, v...)
|
2014-09-23 03:00:58 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// ErrorWithSkip records error log from "skip" calls back from this function
|
|
|
|
func ErrorWithSkip(skip int, format string, v ...interface{}) {
|
|
|
|
Log(skip+1, ERROR, format, v...)
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// IsError returns true if at least one logger is ERROR
|
|
|
|
func IsError() bool {
|
|
|
|
return GetLevel() <= ERROR
|
2019-02-06 08:36:41 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// Critical records critical log
|
|
|
|
func Critical(format string, v ...interface{}) {
|
|
|
|
Log(1, CRITICAL, format, v...)
|
2019-02-06 08:36:41 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// CriticalWithSkip records critical log from "skip" calls back from this function
|
|
|
|
func CriticalWithSkip(skip int, format string, v ...interface{}) {
|
|
|
|
Log(skip+1, CRITICAL, format, v...)
|
2019-02-06 08:36:41 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// IsCritical returns true if at least one logger is CRITICAL
|
|
|
|
func IsCritical() bool {
|
|
|
|
return GetLevel() <= CRITICAL
|
2019-02-06 08:36:41 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// Fatal records fatal log and exit process
|
|
|
|
func Fatal(format string, v ...interface{}) {
|
|
|
|
Log(1, FATAL, format, v...)
|
|
|
|
Close()
|
|
|
|
os.Exit(1)
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// FatalWithSkip records fatal log from "skip" calls back from this function
|
|
|
|
func FatalWithSkip(skip int, format string, v ...interface{}) {
|
|
|
|
Log(skip+1, FATAL, format, v...)
|
|
|
|
Close()
|
|
|
|
os.Exit(1)
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// IsFatal returns true if at least one logger is FATAL
|
|
|
|
func IsFatal() bool {
|
|
|
|
return GetLevel() <= FATAL
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2020-07-06 05:37:07 +05:30
|
|
|
// Pause pauses all the loggers
|
|
|
|
func Pause() {
|
|
|
|
NamedLoggers.Range(func(key, value interface{}) bool {
|
2020-10-31 11:06:46 +05:30
|
|
|
logger := value.(*MultiChannelledLogger)
|
2020-07-06 05:37:07 +05:30
|
|
|
logger.Pause()
|
|
|
|
logger.Flush()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resume resumes all the loggers
|
|
|
|
func Resume() {
|
|
|
|
NamedLoggers.Range(func(key, value interface{}) bool {
|
2020-10-31 11:06:46 +05:30
|
|
|
logger := value.(*MultiChannelledLogger)
|
2020-07-06 05:37:07 +05:30
|
|
|
logger.Resume()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReleaseReopen releases and reopens logging files
|
|
|
|
func ReleaseReopen() error {
|
|
|
|
var accumulatedErr error
|
|
|
|
NamedLoggers.Range(func(key, value interface{}) bool {
|
2020-10-31 11:06:46 +05:30
|
|
|
logger := value.(*MultiChannelledLogger)
|
2020-07-06 05:37:07 +05:30
|
|
|
if err := logger.ReleaseReopen(); err != nil {
|
|
|
|
if accumulatedErr == nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
accumulatedErr = fmt.Errorf("Error reopening %s: %w", key.(string), err)
|
2020-07-06 05:37:07 +05:30
|
|
|
} else {
|
2022-10-25 00:59:17 +05:30
|
|
|
accumulatedErr = fmt.Errorf("Error reopening %s: %v & %w", key.(string), err, accumulatedErr)
|
2020-07-06 05:37:07 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
return accumulatedErr
|
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// Close closes all the loggers
|
|
|
|
func Close() {
|
2019-09-17 15:09:37 +05:30
|
|
|
l, ok := NamedLoggers.Load(DEFAULT)
|
2019-04-02 13:18:31 +05:30
|
|
|
if !ok {
|
|
|
|
return
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
2019-09-17 15:09:37 +05:30
|
|
|
NamedLoggers.Delete(DEFAULT)
|
2019-04-02 13:18:31 +05:30
|
|
|
l.Close()
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// Log a message with defined skip and at logging level
|
|
|
|
// A skip of 0 refers to the caller of this command
|
|
|
|
func Log(skip int, level Level, format string, v ...interface{}) {
|
2019-09-17 15:09:37 +05:30
|
|
|
l, ok := NamedLoggers.Load(DEFAULT)
|
2019-04-02 13:18:31 +05:30
|
|
|
if ok {
|
|
|
|
l.Log(skip+1, level, format, v...)
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// LoggerAsWriter is a io.Writer shim around the gitea log
|
|
|
|
type LoggerAsWriter struct {
|
2020-10-31 11:06:46 +05:30
|
|
|
ourLoggers []*MultiChannelledLogger
|
2019-04-02 13:18:31 +05:30
|
|
|
level Level
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// NewLoggerAsWriter creates a Writer representation of the logger with setable log level
|
2020-10-31 11:06:46 +05:30
|
|
|
func NewLoggerAsWriter(level string, ourLoggers ...*MultiChannelledLogger) *LoggerAsWriter {
|
2019-04-02 13:18:31 +05:30
|
|
|
if len(ourLoggers) == 0 {
|
2019-09-17 15:09:37 +05:30
|
|
|
l, _ := NamedLoggers.Load(DEFAULT)
|
2020-10-31 11:06:46 +05:30
|
|
|
ourLoggers = []*MultiChannelledLogger{l}
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
2019-04-02 13:18:31 +05:30
|
|
|
l := &LoggerAsWriter{
|
|
|
|
ourLoggers: ourLoggers,
|
|
|
|
level: FromString(level),
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
2019-04-02 13:18:31 +05:30
|
|
|
return l
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2021-01-29 21:05:30 +05:30
|
|
|
// Write implements the io.Writer interface to allow spoofing of chi
|
2019-04-02 13:18:31 +05:30
|
|
|
func (l *LoggerAsWriter) Write(p []byte) (int, error) {
|
|
|
|
for _, logger := range l.ourLoggers {
|
|
|
|
// Skip = 3 because this presumes that we have been called by log.Println()
|
|
|
|
// If the caller has used log.Output or the like this will be wrong
|
|
|
|
logger.Log(3, l.level, string(p))
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
2019-04-02 13:18:31 +05:30
|
|
|
return len(p), nil
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
// Log takes a given string and logs it at the set log-level
|
|
|
|
func (l *LoggerAsWriter) Log(msg string) {
|
|
|
|
for _, logger := range l.ourLoggers {
|
|
|
|
// Set the skip to reference the call just above this
|
2019-06-13 01:11:28 +05:30
|
|
|
_ = logger.Log(1, l.level, msg)
|
2019-04-02 13:18:31 +05:30
|
|
|
}
|
2014-07-26 09:54:27 +05:30
|
|
|
}
|
|
|
|
|
2019-04-02 13:18:31 +05:30
|
|
|
func init() {
|
2023-02-04 04:41:48 +05:30
|
|
|
process.Trace = func(start bool, pid process.IDType, description string, parentPID process.IDType, typ string) {
|
|
|
|
if start && parentPID != "" {
|
|
|
|
Log(1, TRACE, "Start %s: %s (from %s) (%s)", NewColoredValue(pid, FgHiYellow), description, NewColoredValue(parentPID, FgYellow), NewColoredValue(typ, Reset))
|
|
|
|
} else if start {
|
|
|
|
Log(1, TRACE, "Start %s: %s (%s)", NewColoredValue(pid, FgHiYellow), description, NewColoredValue(typ, Reset))
|
|
|
|
} else {
|
|
|
|
Log(1, TRACE, "Done %s: %s", NewColoredValue(pid, FgHiYellow), NewColoredValue(description, Reset))
|
|
|
|
}
|
|
|
|
}
|
2019-04-02 13:18:31 +05:30
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
|
|
prefix = strings.TrimSuffix(filename, "modules/log/log.go")
|
2022-01-20 17:11:25 +05:30
|
|
|
if prefix == filename {
|
|
|
|
// in case the source code file is moved, we can not trim the suffix, the code above should also be updated.
|
|
|
|
panic("unable to detect correct package prefix, please update file: " + filename)
|
|
|
|
}
|
2014-05-26 05:41:25 +05:30
|
|
|
}
|