MM-34787: Add colored output for non JSON console logs (#17388)

* MM-34787: Add colored output for non JSON console logs

https://mattermost.atlassian.net/browse/MM-34787

```release-note
A new field EnableColor is added to LogSettings and NotificationLogSettings.
Non-JSON console logs will now be colored if that field is set to true.
```

* Trigger CI
Этот коммит содержится в:
Agniva De Sarker
2021-04-13 23:45:56 +05:30
коммит произвёл GitHub
родитель 6f87eb991f
Коммит fa16ecf98a
4 изменённых файлов: 20 добавлений и 4 удалений

Просмотреть файл

@@ -1201,6 +1201,7 @@ type LogSettings struct {
EnableConsole *bool `access:"environment_logging,write_restrictable,cloud_restrictable"` EnableConsole *bool `access:"environment_logging,write_restrictable,cloud_restrictable"`
ConsoleLevel *string `access:"environment_logging,write_restrictable,cloud_restrictable"` ConsoleLevel *string `access:"environment_logging,write_restrictable,cloud_restrictable"`
ConsoleJson *bool `access:"environment_logging,write_restrictable,cloud_restrictable"` ConsoleJson *bool `access:"environment_logging,write_restrictable,cloud_restrictable"`
EnableColor *bool `access:"environment_logging,write_restrictable,cloud_restrictable"` // telemetry: none
EnableFile *bool `access:"environment_logging,write_restrictable,cloud_restrictable"` EnableFile *bool `access:"environment_logging,write_restrictable,cloud_restrictable"`
FileLevel *string `access:"environment_logging,write_restrictable,cloud_restrictable"` FileLevel *string `access:"environment_logging,write_restrictable,cloud_restrictable"`
FileJson *bool `access:"environment_logging,write_restrictable,cloud_restrictable"` FileJson *bool `access:"environment_logging,write_restrictable,cloud_restrictable"`
@@ -1220,6 +1221,10 @@ func (s *LogSettings) SetDefaults() {
s.ConsoleLevel = NewString("DEBUG") s.ConsoleLevel = NewString("DEBUG")
} }
if s.EnableColor == nil {
s.EnableColor = NewBool(false)
}
if s.EnableFile == nil { if s.EnableFile == nil {
s.EnableFile = NewBool(true) s.EnableFile = NewBool(true)
} }
@@ -1306,6 +1311,7 @@ type NotificationLogSettings struct {
EnableConsole *bool `access:"write_restrictable,cloud_restrictable"` EnableConsole *bool `access:"write_restrictable,cloud_restrictable"`
ConsoleLevel *string `access:"write_restrictable,cloud_restrictable"` ConsoleLevel *string `access:"write_restrictable,cloud_restrictable"`
ConsoleJson *bool `access:"write_restrictable,cloud_restrictable"` ConsoleJson *bool `access:"write_restrictable,cloud_restrictable"`
EnableColor *bool `access:"write_restrictable,cloud_restrictable"` // telemetry: none
EnableFile *bool `access:"write_restrictable,cloud_restrictable"` EnableFile *bool `access:"write_restrictable,cloud_restrictable"`
FileLevel *string `access:"write_restrictable,cloud_restrictable"` FileLevel *string `access:"write_restrictable,cloud_restrictable"`
FileJson *bool `access:"write_restrictable,cloud_restrictable"` FileJson *bool `access:"write_restrictable,cloud_restrictable"`
@@ -1338,6 +1344,10 @@ func (s *NotificationLogSettings) SetDefaults() {
s.ConsoleJson = NewBool(true) s.ConsoleJson = NewBool(true)
} }
if s.EnableColor == nil {
s.EnableColor = NewBool(false)
}
if s.FileJson == nil { if s.FileJson == nil {
s.FileJson = NewBool(true) s.FileJson = NewBool(true)
} }

Просмотреть файл

@@ -73,6 +73,7 @@ type TargetInfo logr.TargetInfo
type LoggerConfiguration struct { type LoggerConfiguration struct {
EnableConsole bool EnableConsole bool
ConsoleJson bool ConsoleJson bool
EnableColor bool
ConsoleLevel string ConsoleLevel string
EnableFile bool EnableFile bool
FileJson bool FileJson bool
@@ -103,12 +104,15 @@ func getZapLevel(level string) zapcore.Level {
} }
} }
func makeEncoder(json bool) zapcore.Encoder { func makeEncoder(json, color bool) zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig() encoderConfig := zap.NewProductionEncoderConfig()
if json { if json {
return zapcore.NewJSONEncoder(encoderConfig) return zapcore.NewJSONEncoder(encoderConfig)
} }
if color {
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
return zapcore.NewConsoleEncoder(encoderConfig) return zapcore.NewConsoleEncoder(encoderConfig)
} }
@@ -124,7 +128,7 @@ func NewLogger(config *LoggerConfiguration) *Logger {
if config.EnableConsole { if config.EnableConsole {
writer := zapcore.Lock(os.Stderr) writer := zapcore.Lock(os.Stderr)
core := zapcore.NewCore(makeEncoder(config.ConsoleJson), writer, logger.consoleLevel) core := zapcore.NewCore(makeEncoder(config.ConsoleJson, config.EnableColor), writer, logger.consoleLevel)
cores = append(cores, core) cores = append(cores, core)
} }
@@ -153,7 +157,7 @@ func NewLogger(config *LoggerConfiguration) *Logger {
Compress: true, Compress: true,
}) })
core := zapcore.NewCore(makeEncoder(config.FileJson), writer, logger.fileLevel) core := zapcore.NewCore(makeEncoder(config.FileJson, false), writer, logger.fileLevel)
cores = append(cores, core) cores = append(cores, core)
} }
} }

Просмотреть файл

@@ -37,7 +37,7 @@ func NewTestingLogger(tb testing.TB, writer io.Writer) *Logger {
mutex: &sync.RWMutex{}, mutex: &sync.RWMutex{},
} }
logWriterCore := zapcore.NewCore(makeEncoder(true), zapcore.Lock(logWriterSync), testingLogger.consoleLevel) logWriterCore := zapcore.NewCore(makeEncoder(true, false), zapcore.Lock(logWriterSync), testingLogger.consoleLevel)
testingLogger.zap = zap.New(logWriterCore, testingLogger.zap = zap.New(logWriterCore,
zap.AddCaller(), zap.AddCaller(),

Просмотреть файл

@@ -29,6 +29,7 @@ func MloggerConfigFromLoggerConfig(s *model.LogSettings, getFileFunc fileLocatio
FileJson: *s.FileJson, FileJson: *s.FileJson,
FileLevel: strings.ToLower(*s.FileLevel), FileLevel: strings.ToLower(*s.FileLevel),
FileLocation: getFileFunc(*s.FileLocation), FileLocation: getFileFunc(*s.FileLocation),
EnableColor: *s.EnableColor,
} }
} }
@@ -58,6 +59,7 @@ func GetLogSettingsFromNotificationsLogSettings(notificationLogSettings *model.N
FileLevel: notificationLogSettings.FileLevel, FileLevel: notificationLogSettings.FileLevel,
FileLocation: notificationLogSettings.FileLocation, FileLocation: notificationLogSettings.FileLocation,
AdvancedLoggingConfig: notificationLogSettings.AdvancedLoggingConfig, AdvancedLoggingConfig: notificationLogSettings.AdvancedLoggingConfig,
EnableColor: notificationLogSettings.EnableColor,
} }
} }