summaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2020-10-28 13:26:00 +0300
committerWolfy-J <[email protected]>2020-10-28 13:26:00 +0300
commitd6ceb16a9a165764739137de6a2b621685bb8ac3 (patch)
tree5a5202aea9021aa2de3daccb06680d6174ed8717 /plugins
parenta9ddea1855ffda6f6388fc91223b8c63ed48b32f (diff)
- new coloring strategy for names
Diffstat (limited to 'plugins')
-rw-r--r--plugins/logger/config.go7
-rw-r--r--plugins/logger/encoder.go65
-rw-r--r--plugins/logger/zap_logger.go38
3 files changed, 78 insertions, 32 deletions
diff --git a/plugins/logger/config.go b/plugins/logger/config.go
index 12badade..4e9f1220 100644
--- a/plugins/logger/config.go
+++ b/plugins/logger/config.go
@@ -1,9 +1,14 @@
package logger
type Config struct {
- Squash bool
+ Default LoggerConfig
+
+ Suppress bool
+
Channels map[string]LoggerConfig
}
type LoggerConfig struct {
+ // Level to report messages from.
+ Level string
}
diff --git a/plugins/logger/encoder.go b/plugins/logger/encoder.go
new file mode 100644
index 00000000..3eef191b
--- /dev/null
+++ b/plugins/logger/encoder.go
@@ -0,0 +1,65 @@
+package logger
+
+import (
+ "github.com/fatih/color"
+ "go.uber.org/zap/zapcore"
+ "hash/fnv"
+ "strings"
+ "time"
+)
+
+var colorMap = []func(string, ...interface{}) string{
+ color.HiYellowString,
+ color.HiGreenString,
+ color.HiBlueString,
+ color.HiRedString,
+ color.HiCyanString,
+ color.HiMagentaString,
+}
+
+// ColoredLevelEncoder colorizes log levels.
+func ColoredLevelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
+ switch level {
+ case zapcore.DebugLevel:
+ enc.AppendString(color.HiWhiteString(level.CapitalString()))
+ case zapcore.InfoLevel:
+ enc.AppendString(color.HiCyanString(level.CapitalString()))
+ case zapcore.WarnLevel:
+ enc.AppendString(color.HiYellowString(level.CapitalString()))
+ case zapcore.ErrorLevel, zapcore.DPanicLevel:
+ enc.AppendString(color.HiRedString(level.CapitalString()))
+ case zapcore.PanicLevel, zapcore.FatalLevel:
+ enc.AppendString(color.HiMagentaString(level.CapitalString()))
+ }
+}
+
+// ColoredNameEncoder colorizes service names.
+func ColoredNameEncoder(s string, enc zapcore.PrimitiveArrayEncoder) {
+ if len(s) < 12 {
+ s = s + strings.Repeat(" ", 12-len(s))
+ }
+
+ enc.AppendString(color.HiGreenString(s))
+}
+
+// ColoredHashedNameEncoder colorizes service names and assigns different colors to different names.
+func ColoredHashedNameEncoder(s string, enc zapcore.PrimitiveArrayEncoder) {
+ if len(s) < 12 {
+ s = s + strings.Repeat(" ", 12-len(s))
+ }
+
+ colorID := stringHash(s, len(colorMap))
+ enc.AppendString(colorMap[colorID](s))
+}
+
+// UTCTimeEncoder encodes time into short UTC specific timestamp.
+func UTCTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
+ enc.AppendString(t.UTC().Format("2006/01/02 15:04:05"))
+}
+
+// returns string hash
+func stringHash(name string, base int) int {
+ h := fnv.New32a()
+ h.Write([]byte(name))
+ return int(h.Sum32()) % base
+}
diff --git a/plugins/logger/zap_logger.go b/plugins/logger/zap_logger.go
index 747cbf77..e1986da3 100644
--- a/plugins/logger/zap_logger.go
+++ b/plugins/logger/zap_logger.go
@@ -1,13 +1,10 @@
package logger
import (
- "github.com/fatih/color"
"github.com/spiral/endure"
"github.com/spiral/roadrunner/v2/plugins/config"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
- "strings"
- "time"
)
// ServiceName declares service name.
@@ -38,34 +35,13 @@ func (z *ZapLogger) Init(cfg config.Provider) (err error) {
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
Encoding: "console",
EncoderConfig: zapcore.EncoderConfig{
- MessageKey: "message",
- LevelKey: "level",
- TimeKey: "time",
- NameKey: "name",
- EncodeName: func(s string, enc zapcore.PrimitiveArrayEncoder) {
- if len(s) < 12 {
- s = s + strings.Repeat(" ", 12-len(s))
- }
-
- enc.AppendString(color.HiGreenString(s))
- },
- EncodeLevel: func(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) {
- switch level {
- case zapcore.DebugLevel:
- enc.AppendString(color.HiWhiteString(level.CapitalString()))
- case zapcore.InfoLevel:
- enc.AppendString(color.HiCyanString(level.CapitalString()))
- case zapcore.WarnLevel:
- enc.AppendString(color.HiYellowString(level.CapitalString()))
- case zapcore.ErrorLevel, zapcore.DPanicLevel:
- enc.AppendString(color.HiRedString(level.CapitalString()))
- case zapcore.PanicLevel, zapcore.FatalLevel:
- enc.AppendString(color.HiMagentaString(level.CapitalString()))
- }
- },
- EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
- enc.AppendString(t.UTC().Format("2006/01/02 15:04:05"))
- },
+ MessageKey: "message",
+ LevelKey: "level",
+ TimeKey: "time",
+ NameKey: "name",
+ EncodeName: ColoredHashedNameEncoder,
+ EncodeLevel: ColoredLevelEncoder,
+ EncodeTime: UTCTimeEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stderr"},