github.com/mattermost/mattermost-server/v5@v5.39.3/utils/logger.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"path/filepath"
     8  	"strings"
     9  
    10  	"github.com/mattermost/mattermost-server/v5/model"
    11  	"github.com/mattermost/mattermost-server/v5/shared/mlog"
    12  	"github.com/mattermost/mattermost-server/v5/utils/fileutils"
    13  )
    14  
    15  const (
    16  	LogRotateSize           = 10000
    17  	LogFilename             = "mattermost.log"
    18  	LogNotificationFilename = "notifications.log"
    19  )
    20  
    21  type fileLocationFunc func(string) string
    22  
    23  func MloggerConfigFromLoggerConfig(s *model.LogSettings, getFileFunc fileLocationFunc) *mlog.LoggerConfiguration {
    24  	return &mlog.LoggerConfiguration{
    25  		EnableConsole: *s.EnableConsole,
    26  		ConsoleJson:   *s.ConsoleJson,
    27  		ConsoleLevel:  strings.ToLower(*s.ConsoleLevel),
    28  		EnableFile:    *s.EnableFile,
    29  		FileJson:      *s.FileJson,
    30  		FileLevel:     strings.ToLower(*s.FileLevel),
    31  		FileLocation:  getFileFunc(*s.FileLocation),
    32  		EnableColor:   *s.EnableColor,
    33  	}
    34  }
    35  
    36  func GetLogFileLocation(fileLocation string) string {
    37  	if fileLocation == "" {
    38  		fileLocation, _ = fileutils.FindDir("logs")
    39  	}
    40  
    41  	return filepath.Join(fileLocation, LogFilename)
    42  }
    43  
    44  func GetNotificationsLogFileLocation(fileLocation string) string {
    45  	if fileLocation == "" {
    46  		fileLocation, _ = fileutils.FindDir("logs")
    47  	}
    48  
    49  	return filepath.Join(fileLocation, LogNotificationFilename)
    50  }
    51  
    52  func GetLogSettingsFromNotificationsLogSettings(notificationLogSettings *model.NotificationLogSettings) *model.LogSettings {
    53  	return &model.LogSettings{
    54  		ConsoleJson:           notificationLogSettings.ConsoleJson,
    55  		ConsoleLevel:          notificationLogSettings.ConsoleLevel,
    56  		EnableConsole:         notificationLogSettings.EnableConsole,
    57  		EnableFile:            notificationLogSettings.EnableFile,
    58  		FileJson:              notificationLogSettings.FileJson,
    59  		FileLevel:             notificationLogSettings.FileLevel,
    60  		FileLocation:          notificationLogSettings.FileLocation,
    61  		AdvancedLoggingConfig: notificationLogSettings.AdvancedLoggingConfig,
    62  		EnableColor:           notificationLogSettings.EnableColor,
    63  	}
    64  }
    65  
    66  // DON'T USE THIS Modify the level on the app logger
    67  func DisableDebugLogForTest() {
    68  	mlog.GloballyDisableDebugLogForTest()
    69  }
    70  
    71  // DON'T USE THIS Modify the level on the app logger
    72  func EnableDebugLogForTest() {
    73  	mlog.GloballyEnableDebugLogForTest()
    74  }