github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/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/mlog"
    11  	"github.com/mattermost/mattermost-server/v5/model"
    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  	}
    33  }
    34  
    35  func GetLogFileLocation(fileLocation string) string {
    36  	if fileLocation == "" {
    37  		fileLocation, _ = fileutils.FindDir("logs")
    38  	}
    39  
    40  	return filepath.Join(fileLocation, LogFilename)
    41  }
    42  
    43  func GetNotificationsLogFileLocation(fileLocation string) string {
    44  	if fileLocation == "" {
    45  		fileLocation, _ = fileutils.FindDir("logs")
    46  	}
    47  
    48  	return filepath.Join(fileLocation, LogNotificationFilename)
    49  }
    50  
    51  func GetLogSettingsFromNotificationsLogSettings(notificationLogSettings *model.NotificationLogSettings) *model.LogSettings {
    52  	return &model.LogSettings{
    53  		ConsoleJson:           notificationLogSettings.ConsoleJson,
    54  		ConsoleLevel:          notificationLogSettings.ConsoleLevel,
    55  		EnableConsole:         notificationLogSettings.EnableConsole,
    56  		EnableFile:            notificationLogSettings.EnableFile,
    57  		FileJson:              notificationLogSettings.FileJson,
    58  		FileLevel:             notificationLogSettings.FileLevel,
    59  		FileLocation:          notificationLogSettings.FileLocation,
    60  		AdvancedLoggingConfig: notificationLogSettings.AdvancedLoggingConfig,
    61  	}
    62  }
    63  
    64  // DON'T USE THIS Modify the level on the app logger
    65  func DisableDebugLogForTest() {
    66  	mlog.GloballyDisableDebugLogForTest()
    67  }
    68  
    69  // DON'T USE THIS Modify the level on the app logger
    70  func EnableDebugLogForTest() {
    71  	mlog.GloballyEnableDebugLogForTest()
    72  }