github.com/openimsdk/tools@v0.0.49/log/color.go (about) 1 // Copyright © 2023 OpenIM. All rights reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package log 16 17 import ( 18 "fmt" 19 20 "go.uber.org/zap/zapcore" 21 ) 22 23 // Foreground colors. 24 const ( 25 Black Color = iota + 30 26 Red 27 Green 28 Yellow 29 Blue 30 Magenta 31 Cyan 32 White 33 ) 34 35 var ( 36 _levelToColor = map[zapcore.Level]Color{ 37 zapcore.DebugLevel: White, 38 zapcore.InfoLevel: Blue, 39 zapcore.WarnLevel: Yellow, 40 zapcore.ErrorLevel: Red, 41 zapcore.DPanicLevel: Red, 42 zapcore.PanicLevel: Red, 43 zapcore.FatalLevel: Red, 44 } 45 _unknownLevelColor = make(map[zapcore.Level]string, len(_levelToColor)) 46 47 _levelToLowercaseColorString = make(map[zapcore.Level]string, len(_levelToColor)) 48 _levelToCapitalColorString = make(map[zapcore.Level]string, len(_levelToColor)) 49 ) 50 51 func init() { 52 for level, color := range _levelToColor { 53 _levelToLowercaseColorString[level] = color.Add(level.String()) 54 _levelToCapitalColorString[level] = color.Add(level.CapitalString()) 55 } 56 } 57 58 // Color represents a text color. 59 type Color uint8 60 61 // Add adds the coloring to the given string. 62 func (c Color) Add(s string) string { 63 return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c), s) 64 }