mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Our store upgrade methods used mlog.Critical and then slept for a second and triggered os.Exit. First of all, with the new mlog refactor Critical wasn't a standard log level so it was never to be logged. And secondly, this completely missed the fact that there already existed an API method Fatal which did exactly this. We mark the Critical API as deprecated, and use Fatal for all occurences. The choice should be between either to use Error or Fatal. While here, we also remove the non-standard exit codes which just confused things further while debugging. https://mattermost.atlassian.net/browse/MM-38712 ```release-note NONE ```
133 lines
3.2 KiB
Go
133 lines
3.2 KiB
Go
// 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...)
|
|
}
|