summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/mattermost-server/v6/shared/mlog/global.go
blob: de346123aa71aee9561fe738c1adb17a6610f2fd (plain) (blame)
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

package mlog

import (
	"sync"
)

var (
	globalLogger    *Logger
	muxGlobalLogger sync.RWMutex
)

func InitGlobalLogger(logger *Logger) {
	muxGlobalLogger.Lock()
	defer muxGlobalLogger.Unlock()

	globalLogger = logger
}

func getGlobalLogger() *Logger {
	muxGlobalLogger.RLock()
	defer muxGlobalLogger.RUnlock()

	return globalLogger
}

// IsLevelEnabled returns true only if at least one log target is
// configured to emit the specified log level. Use this check when
// gathering the log info may be expensive.
//
// Note, transformations and serializations done via fields are already
// lazily evaluated and don't require this check beforehand.
func IsLevelEnabled(level Level) bool {
	logger := getGlobalLogger()
	if logger == nil {
		return defaultIsLevelEnabled(level)
	}
	return logger.IsLevelEnabled(level)
}

// Log emits the log record for any targets configured for the specified level.
func Log(level Level, msg string, fields ...Field) {
	logger := getGlobalLogger()
	if logger == nil {
		defaultLog(level, msg, fields...)
		return
	}
	logger.Log(level, msg, fields...)
}

// LogM emits the log record for any targets configured for the specified levels.
// Equivalent to calling `Log` once for each level.
func LogM(levels []Level, msg string, fields ...Field) {
	logger := getGlobalLogger()
	if logger == nil {
		defaultCustomMultiLog(levels, msg, fields...)
		return
	}
	logger.LogM(levels, msg, fields...)
}

// Convenience method equivalent to calling `Log` with the `Trace` level.
func Trace(msg string, fields ...Field) {
	logger := getGlobalLogger()
	if logger == nil {
		defaultLog(LvlTrace, msg, fields...)
		return
	}
	logger.Trace(msg, fields...)
}

// Convenience method equivalent to calling `Log` with the `Debug` level.
func Debug(msg string, fields ...Field) {
	logger := getGlobalLogger()
	if logger == nil {
		defaultLog(LvlDebug, msg, fields...)
		return
	}
	logger.Debug(msg, fields...)
}

// Convenience method equivalent to calling `Log` with the `Info` level.
func Info(msg string, fields ...Field) {
	logger := getGlobalLogger()
	if logger == nil {
		defaultLog(LvlInfo, msg, fields...)
		return
	}
	logger.Info(msg, fields...)
}

// Convenience method equivalent to calling `Log` with the `Warn` level.
func Warn(msg string, fields ...Field) {
	logger := getGlobalLogger()
	if logger == nil {
		defaultLog(LvlWarn, msg, fields...)
		return
	}
	logger.Warn(msg, fields...)
}

// Convenience method equivalent to calling `Log` with the `Error` level.
func Error(msg string, fields ...Field) {
	logger := getGlobalLogger()
	if logger == nil {
		defaultLog(LvlError, msg, fields...)
		return
	}
	logger.Error(msg, fields...)
}

// Convenience method equivalent to calling `Log` with the `Critical` level.
// DEPRECATED: Either use Error or Fatal.
func Critical(msg string, fields ...Field) {
	logger := getGlobalLogger()
	if logger == nil {
		defaultLog(LvlCritical, msg, fields...)
		return
	}
	logger.Critical(msg, fields...)
}

func Fatal(msg string, fields ...Field) {
	logger := getGlobalLogger()
	if logger == nil {
		defaultLog(LvlFatal, msg, fields...)
		return
	}
	logger.Fatal(msg, fields...)
}