github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/utils/logger.go (about) 1 // Copyright (c) 2015-present Xenia, 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/xzl8028/xenia-server/mlog" 11 "github.com/xzl8028/xenia-server/model" 12 "github.com/xzl8028/xenia-server/utils/fileutils" 13 ) 14 15 const ( 16 LOG_ROTATE_SIZE = 10000 17 LOG_FILENAME = "xenia.log" 18 LOG_NOTIFICATION_FILENAME = "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, LOG_FILENAME) 41 } 42 43 func GetNotificationsLogFileLocation(fileLocation string) string { 44 if fileLocation == "" { 45 fileLocation, _ = fileutils.FindDir("logs") 46 } 47 48 return filepath.Join(fileLocation, LOG_NOTIFICATION_FILENAME) 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 } 61 } 62 63 // DON'T USE THIS Modify the level on the app logger 64 func DisableDebugLogForTest() { 65 mlog.GloballyDisableDebugLogForTest() 66 } 67 68 // DON'T USE THIS Modify the level on the app logger 69 func EnableDebugLogForTest() { 70 mlog.GloballyEnableDebugLogForTest() 71 }