2014-02-12 20:54:09 +01:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2014-06-20 07:14:54 +02:00
|
|
|
"fmt"
|
2014-05-26 02:11:25 +02:00
|
|
|
"os"
|
2014-06-20 07:14:54 +02:00
|
|
|
"path"
|
2014-07-26 06:24:27 +02:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
2014-02-12 20:54:09 +01:00
|
|
|
)
|
|
|
|
|
2014-03-25 12:47:04 +01:00
|
|
|
var (
|
2019-04-02 09:48:31 +02:00
|
|
|
// DEFAULT is the name of the default logger
|
|
|
|
DEFAULT = "default"
|
|
|
|
// NamedLoggers map of named loggers
|
|
|
|
NamedLoggers = make(map[string]*Logger)
|
2016-11-26 12:53:29 +01:00
|
|
|
// GitLogger logger for git
|
2014-07-26 06:24:27 +02:00
|
|
|
GitLogger *Logger
|
2019-04-02 09:48:31 +02:00
|
|
|
prefix string
|
2014-03-25 12:47:04 +01:00
|
|
|
)
|
2014-02-12 20:54:09 +01:00
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// NewLogger create a logger for the default logger
|
|
|
|
func NewLogger(bufLen int64, name, provider, config string) *Logger {
|
|
|
|
err := NewNamedLogger(DEFAULT, bufLen, name, provider, config)
|
|
|
|
if err != nil {
|
|
|
|
CriticalWithSkip(1, "Unable to create default logger: %v", err)
|
|
|
|
panic(err)
|
2014-05-11 20:37:12 +02:00
|
|
|
}
|
2019-04-02 09:48:31 +02:00
|
|
|
return NamedLoggers[DEFAULT]
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNamedLogger creates a new named logger for a given configuration
|
|
|
|
func NewNamedLogger(name string, bufLen int64, subname, provider, config string) error {
|
|
|
|
logger, ok := NamedLoggers[name]
|
|
|
|
if !ok {
|
|
|
|
logger = newLogger(name, bufLen)
|
|
|
|
|
|
|
|
NamedLoggers[name] = logger
|
2014-05-11 20:37:12 +02:00
|
|
|
}
|
2019-04-02 09:48:31 +02:00
|
|
|
|
|
|
|
return logger.SetLogger(subname, provider, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DelNamedLogger closes and deletes the named logger
|
|
|
|
func DelNamedLogger(name string) {
|
|
|
|
l, ok := NamedLoggers[name]
|
|
|
|
if ok {
|
|
|
|
delete(NamedLoggers, name)
|
|
|
|
l.Close()
|
2014-06-20 06:25:23 +02:00
|
|
|
}
|
2014-02-12 20:54:09 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// DelLogger removes the named sublogger from the default logger
|
|
|
|
func DelLogger(name string) error {
|
|
|
|
logger := NamedLoggers[DEFAULT]
|
|
|
|
found, err := logger.DelLogger(name)
|
|
|
|
if !found {
|
|
|
|
Trace("Log %s not found, no need to delete", name)
|
2017-01-17 07:02:35 +01:00
|
|
|
}
|
2019-04-02 09:48:31 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-04-03 04:22:26 +02:00
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// GetLogger returns either a named logger or the default logger
|
|
|
|
func GetLogger(name string) *Logger {
|
|
|
|
logger, ok := NamedLoggers[name]
|
|
|
|
if ok {
|
|
|
|
return logger
|
|
|
|
}
|
|
|
|
return NamedLoggers[DEFAULT]
|
2017-01-17 07:02:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-26 12:53:29 +01:00
|
|
|
// NewGitLogger create a logger for git
|
2016-02-16 05:11:22 +01:00
|
|
|
// FIXME: use same log level as other loggers.
|
2014-06-20 07:14:54 +02:00
|
|
|
func NewGitLogger(logPath string) {
|
2016-12-01 00:56:15 +01:00
|
|
|
path := path.Dir(logPath)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
2019-04-02 09:48:31 +02:00
|
|
|
Fatal("Failed to create dir %s: %v", path, err)
|
2016-12-01 00:56:15 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
GitLogger = newLogger("git", 0)
|
2019-05-14 02:08:34 +02:00
|
|
|
GitLogger.SetLogger("file", "file", fmt.Sprintf(`{"level":"TRACE","filename":"%s","rotate":true,"maxsize":%d,"daily":true,"maxdays":7,"compress":true,"compressionLevel":-1, "stacktraceLevel":"NONE"}`, logPath, 1<<28))
|
2019-04-02 09:48:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLevel returns the minimum logger level
|
|
|
|
func GetLevel() Level {
|
|
|
|
return NamedLoggers[DEFAULT].GetLevel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStacktraceLevel returns the minimum logger level
|
|
|
|
func GetStacktraceLevel() Level {
|
|
|
|
return NamedLoggers[DEFAULT].GetStacktraceLevel()
|
2014-06-20 07:14:54 +02:00
|
|
|
}
|
|
|
|
|
2016-11-26 12:53:29 +01:00
|
|
|
// Trace records trace log
|
2014-02-18 23:48:02 +01:00
|
|
|
func Trace(format string, v ...interface{}) {
|
2019-04-02 09:48:31 +02:00
|
|
|
Log(1, TRACE, format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsTrace returns true if at least one logger is TRACE
|
|
|
|
func IsTrace() bool {
|
|
|
|
return GetLevel() <= TRACE
|
2014-02-18 23:48:02 +01:00
|
|
|
}
|
|
|
|
|
2016-11-26 12:53:29 +01:00
|
|
|
// Debug records debug log
|
2014-03-23 17:16:17 +01:00
|
|
|
func Debug(format string, v ...interface{}) {
|
2019-04-02 09:48:31 +02:00
|
|
|
Log(1, DEBUG, format, v...)
|
2014-03-23 17:16:17 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// IsDebug returns true if at least one logger is DEBUG
|
|
|
|
func IsDebug() bool {
|
|
|
|
return GetLevel() <= DEBUG
|
2014-02-12 20:54:09 +01:00
|
|
|
}
|
2014-02-18 23:31:16 +01:00
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// Info records info log
|
|
|
|
func Info(format string, v ...interface{}) {
|
|
|
|
Log(1, INFO, format, v...)
|
2014-02-18 23:31:16 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// IsInfo returns true if at least one logger is INFO
|
|
|
|
func IsInfo() bool {
|
|
|
|
return GetLevel() <= INFO
|
2014-02-18 23:31:16 +01:00
|
|
|
}
|
2014-02-18 23:48:02 +01:00
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// Warn records warning log
|
|
|
|
func Warn(format string, v ...interface{}) {
|
|
|
|
Log(1, WARN, format, v...)
|
2014-02-18 23:48:02 +01:00
|
|
|
}
|
2014-05-26 02:11:25 +02:00
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// IsWarn returns true if at least one logger is WARN
|
|
|
|
func IsWarn() bool {
|
|
|
|
return GetLevel() <= WARN
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// Error records error log
|
|
|
|
func Error(format string, v ...interface{}) {
|
|
|
|
Log(1, ERROR, format, v...)
|
2014-09-22 23:30:58 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// 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 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// IsError returns true if at least one logger is ERROR
|
|
|
|
func IsError() bool {
|
|
|
|
return GetLevel() <= ERROR
|
2019-02-06 04:06:41 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// Critical records critical log
|
|
|
|
func Critical(format string, v ...interface{}) {
|
|
|
|
Log(1, CRITICAL, format, v...)
|
2019-02-06 04:06:41 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// 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 04:06:41 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// IsCritical returns true if at least one logger is CRITICAL
|
|
|
|
func IsCritical() bool {
|
|
|
|
return GetLevel() <= CRITICAL
|
2019-02-06 04:06:41 +01:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// 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 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// 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 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// IsFatal returns true if at least one logger is FATAL
|
|
|
|
func IsFatal() bool {
|
|
|
|
return GetLevel() <= FATAL
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// Close closes all the loggers
|
|
|
|
func Close() {
|
|
|
|
l, ok := NamedLoggers[DEFAULT]
|
|
|
|
if !ok {
|
|
|
|
return
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
2019-04-02 09:48:31 +02:00
|
|
|
delete(NamedLoggers, DEFAULT)
|
|
|
|
l.Close()
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// 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{}) {
|
|
|
|
l, ok := NamedLoggers[DEFAULT]
|
|
|
|
if ok {
|
|
|
|
l.Log(skip+1, level, format, v...)
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// LoggerAsWriter is a io.Writer shim around the gitea log
|
|
|
|
type LoggerAsWriter struct {
|
|
|
|
ourLoggers []*Logger
|
|
|
|
level Level
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// NewLoggerAsWriter creates a Writer representation of the logger with setable log level
|
|
|
|
func NewLoggerAsWriter(level string, ourLoggers ...*Logger) *LoggerAsWriter {
|
|
|
|
if len(ourLoggers) == 0 {
|
|
|
|
ourLoggers = []*Logger{NamedLoggers[DEFAULT]}
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
2019-04-02 09:48:31 +02:00
|
|
|
l := &LoggerAsWriter{
|
|
|
|
ourLoggers: ourLoggers,
|
|
|
|
level: FromString(level),
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
2019-04-02 09:48:31 +02:00
|
|
|
return l
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// Write implements the io.Writer interface to allow spoofing of macaron
|
|
|
|
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 06:24:27 +02:00
|
|
|
}
|
2019-04-02 09:48:31 +02:00
|
|
|
return len(p), nil
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
// 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
|
|
|
|
logger.Log(1, l.level, msg)
|
|
|
|
}
|
2014-07-26 06:24:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-02 09:48:31 +02:00
|
|
|
func init() {
|
|
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
|
|
prefix = strings.TrimSuffix(filename, "modules/log/log.go")
|
2014-05-26 02:11:25 +02:00
|
|
|
}
|