704da08fdc
* Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
245 lines
6 KiB
Go
245 lines
6 KiB
Go
// 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 (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
// DEFAULT is the name of the default logger
|
|
DEFAULT = "default"
|
|
// NamedLoggers map of named loggers
|
|
NamedLoggers = make(map[string]*Logger)
|
|
// GitLogger logger for git
|
|
GitLogger *Logger
|
|
prefix string
|
|
)
|
|
|
|
// 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)
|
|
}
|
|
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
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// 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]
|
|
}
|
|
|
|
// NewGitLogger create a logger for git
|
|
// FIXME: use same log level as other loggers.
|
|
func NewGitLogger(logPath string) {
|
|
path := path.Dir(logPath)
|
|
|
|
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
|
Fatal("Failed to create dir %s: %v", path, err)
|
|
}
|
|
|
|
GitLogger = newLogger("git", 0)
|
|
GitLogger.SetLogger("file", "file", fmt.Sprintf(`{"level":"TRACE","filename":"%s","rotate":false}`, logPath))
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
|
|
// Trace records trace log
|
|
func Trace(format string, v ...interface{}) {
|
|
Log(1, TRACE, format, v...)
|
|
}
|
|
|
|
// IsTrace returns true if at least one logger is TRACE
|
|
func IsTrace() bool {
|
|
return GetLevel() <= TRACE
|
|
}
|
|
|
|
// Debug records debug log
|
|
func Debug(format string, v ...interface{}) {
|
|
Log(1, DEBUG, format, v...)
|
|
}
|
|
|
|
// IsDebug returns true if at least one logger is DEBUG
|
|
func IsDebug() bool {
|
|
return GetLevel() <= DEBUG
|
|
}
|
|
|
|
// Info records info log
|
|
func Info(format string, v ...interface{}) {
|
|
Log(1, INFO, format, v...)
|
|
}
|
|
|
|
// IsInfo returns true if at least one logger is INFO
|
|
func IsInfo() bool {
|
|
return GetLevel() <= INFO
|
|
}
|
|
|
|
// Warn records warning log
|
|
func Warn(format string, v ...interface{}) {
|
|
Log(1, WARN, format, v...)
|
|
}
|
|
|
|
// IsWarn returns true if at least one logger is WARN
|
|
func IsWarn() bool {
|
|
return GetLevel() <= WARN
|
|
}
|
|
|
|
// Error records error log
|
|
func Error(format string, v ...interface{}) {
|
|
Log(1, ERROR, format, v...)
|
|
}
|
|
|
|
// 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...)
|
|
}
|
|
|
|
// IsError returns true if at least one logger is ERROR
|
|
func IsError() bool {
|
|
return GetLevel() <= ERROR
|
|
}
|
|
|
|
// Critical records critical log
|
|
func Critical(format string, v ...interface{}) {
|
|
Log(1, CRITICAL, format, v...)
|
|
}
|
|
|
|
// 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...)
|
|
}
|
|
|
|
// IsCritical returns true if at least one logger is CRITICAL
|
|
func IsCritical() bool {
|
|
return GetLevel() <= CRITICAL
|
|
}
|
|
|
|
// Fatal records fatal log and exit process
|
|
func Fatal(format string, v ...interface{}) {
|
|
Log(1, FATAL, format, v...)
|
|
Close()
|
|
os.Exit(1)
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
// IsFatal returns true if at least one logger is FATAL
|
|
func IsFatal() bool {
|
|
return GetLevel() <= FATAL
|
|
}
|
|
|
|
// Close closes all the loggers
|
|
func Close() {
|
|
l, ok := NamedLoggers[DEFAULT]
|
|
if !ok {
|
|
return
|
|
}
|
|
delete(NamedLoggers, DEFAULT)
|
|
l.Close()
|
|
}
|
|
|
|
// 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...)
|
|
}
|
|
}
|
|
|
|
// LoggerAsWriter is a io.Writer shim around the gitea log
|
|
type LoggerAsWriter struct {
|
|
ourLoggers []*Logger
|
|
level Level
|
|
}
|
|
|
|
// 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]}
|
|
}
|
|
l := &LoggerAsWriter{
|
|
ourLoggers: ourLoggers,
|
|
level: FromString(level),
|
|
}
|
|
return l
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
return len(p), nil
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
_, filename, _, _ := runtime.Caller(0)
|
|
prefix = strings.TrimSuffix(filename, "modules/log/log.go")
|
|
}
|