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
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
6f87eb991f
Коммит
fa16ecf98a
@@ -1201,6 +1201,7 @@ type LogSettings struct {
|
||||
EnableConsole *bool `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"`
|
||||
EnableColor *bool `access:"environment_logging,write_restrictable,cloud_restrictable"` // telemetry: none
|
||||
EnableFile *bool `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"`
|
||||
@@ -1220,6 +1221,10 @@ func (s *LogSettings) SetDefaults() {
|
||||
s.ConsoleLevel = NewString("DEBUG")
|
||||
}
|
||||
|
||||
if s.EnableColor == nil {
|
||||
s.EnableColor = NewBool(false)
|
||||
}
|
||||
|
||||
if s.EnableFile == nil {
|
||||
s.EnableFile = NewBool(true)
|
||||
}
|
||||
@@ -1306,6 +1311,7 @@ type NotificationLogSettings struct {
|
||||
EnableConsole *bool `access:"write_restrictable,cloud_restrictable"`
|
||||
ConsoleLevel *string `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"`
|
||||
FileLevel *string `access:"write_restrictable,cloud_restrictable"`
|
||||
FileJson *bool `access:"write_restrictable,cloud_restrictable"`
|
||||
@@ -1338,6 +1344,10 @@ func (s *NotificationLogSettings) SetDefaults() {
|
||||
s.ConsoleJson = NewBool(true)
|
||||
}
|
||||
|
||||
if s.EnableColor == nil {
|
||||
s.EnableColor = NewBool(false)
|
||||
}
|
||||
|
||||
if s.FileJson == nil {
|
||||
s.FileJson = NewBool(true)
|
||||
}
|
||||
|
||||
@@ -73,6 +73,7 @@ type TargetInfo logr.TargetInfo
|
||||
type LoggerConfiguration struct {
|
||||
EnableConsole bool
|
||||
ConsoleJson bool
|
||||
EnableColor bool
|
||||
ConsoleLevel string
|
||||
EnableFile 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()
|
||||
if json {
|
||||
return zapcore.NewJSONEncoder(encoderConfig)
|
||||
}
|
||||
|
||||
if color {
|
||||
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||||
}
|
||||
encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||||
return zapcore.NewConsoleEncoder(encoderConfig)
|
||||
}
|
||||
@@ -124,7 +128,7 @@ func NewLogger(config *LoggerConfiguration) *Logger {
|
||||
|
||||
if config.EnableConsole {
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -153,7 +157,7 @@ func NewLogger(config *LoggerConfiguration) *Logger {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func NewTestingLogger(tb testing.TB, writer io.Writer) *Logger {
|
||||
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,
|
||||
zap.AddCaller(),
|
||||
|
||||
@@ -29,6 +29,7 @@ func MloggerConfigFromLoggerConfig(s *model.LogSettings, getFileFunc fileLocatio
|
||||
FileJson: *s.FileJson,
|
||||
FileLevel: strings.ToLower(*s.FileLevel),
|
||||
FileLocation: getFileFunc(*s.FileLocation),
|
||||
EnableColor: *s.EnableColor,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,6 +59,7 @@ func GetLogSettingsFromNotificationsLogSettings(notificationLogSettings *model.N
|
||||
FileLevel: notificationLogSettings.FileLevel,
|
||||
FileLocation: notificationLogSettings.FileLocation,
|
||||
AdvancedLoggingConfig: notificationLogSettings.AdvancedLoggingConfig,
|
||||
EnableColor: notificationLogSettings.EnableColor,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user