|
|
|
// Copyright © 2023 OpenIM. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Foreground colors.
|
|
|
|
const (
|
|
|
|
Black Color = iota + 30
|
|
|
|
Red
|
|
|
|
Green
|
|
|
|
Yellow
|
|
|
|
Blue
|
|
|
|
Magenta
|
|
|
|
Cyan
|
|
|
|
White
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_levelToColor = map[zapcore.Level]Color{
|
|
|
|
zapcore.DebugLevel: White,
|
|
|
|
zapcore.InfoLevel: Blue,
|
|
|
|
zapcore.WarnLevel: Yellow,
|
|
|
|
zapcore.ErrorLevel: Red,
|
|
|
|
zapcore.DPanicLevel: Red,
|
|
|
|
zapcore.PanicLevel: Red,
|
|
|
|
zapcore.FatalLevel: Red,
|
|
|
|
}
|
|
|
|
_unknownLevelColor = make(map[zapcore.Level]string, len(_levelToColor))
|
|
|
|
|
|
|
|
_levelToLowercaseColorString = make(map[zapcore.Level]string, len(_levelToColor))
|
|
|
|
_levelToCapitalColorString = make(map[zapcore.Level]string, len(_levelToColor))
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for level, color := range _levelToColor {
|
|
|
|
_levelToLowercaseColorString[level] = color.Add(level.String())
|
|
|
|
_levelToCapitalColorString[level] = color.Add(level.CapitalString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Color represents a text color.
|
|
|
|
type Color uint8
|
|
|
|
|
|
|
|
// Add adds the coloring to the given string.
|
|
|
|
func (c Color) Add(s string) string {
|
|
|
|
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c), s)
|
|
|
|
}
|