summaryrefslogtreecommitdiffstats
path: root/modules/log/logger_global.go
blob: 994acfedbb3a16da90b17f512f85c2d69574415c (plain)
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
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package log

import (
	"fmt"
	"os"
)

// FallbackErrorf is the last chance to show an error if the logger has internal errors
func FallbackErrorf(format string, args ...any) {
	_, _ = fmt.Fprintf(os.Stderr, format+"\n", args...)
}

func GetLevel() Level {
	return GetLogger(DEFAULT).GetLevel()
}

func Log(skip int, level Level, format string, v ...any) {
	GetLogger(DEFAULT).Log(skip+1, level, format, v...)
}

func Trace(format string, v ...any) {
	Log(1, TRACE, format, v...)
}

func IsTrace() bool {
	return GetLevel() <= TRACE
}

func Debug(format string, v ...any) {
	Log(1, DEBUG, format, v...)
}

func IsDebug() bool {
	return GetLevel() <= DEBUG
}

func Info(format string, v ...any) {
	Log(1, INFO, format, v...)
}

func Warn(format string, v ...any) {
	Log(1, WARN, format, v...)
}

func Error(format string, v ...any) {
	Log(1, ERROR, format, v...)
}

func ErrorWithSkip(skip int, format string, v ...any) {
	Log(skip+1, ERROR, format, v...)
}

func Critical(format string, v ...any) {
	Log(1, ERROR, format, v...)
}

// Fatal records fatal log and exit process
func Fatal(format string, v ...any) {
	Log(1, FATAL, format, v...)
	GetManager().Close()
	os.Exit(1)
}

func GetLogger(name string) Logger {
	return GetManager().GetLogger(name)
}

func IsLoggerEnabled(name string) bool {
	return GetManager().GetLogger(name).IsEnabled()
}

func SetConsoleLogger(loggerName, writerName string, level Level) {
	writer := NewEventWriterConsole(writerName, WriterMode{
		Level:        level,
		Flags:        FlagsFromBits(LstdFlags),
		Colorize:     CanColorStdout,
		WriterOption: WriterConsoleOption{},
	})
	GetManager().GetLogger(loggerName).ReplaceAllWriters(writer)
}