github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/logging/color.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package logging
    20  
    21  import (
    22  	"fmt"
    23  
    24  	"go.uber.org/zap/zapcore"
    25  )
    26  
    27  // Foreground colors.
    28  const (
    29  	// Black ...
    30  	Black Color = iota + 30
    31  	// Red ...
    32  	Red
    33  	// Green ...
    34  	Green
    35  	// Yellow ...
    36  	Yellow
    37  	// Blue ...
    38  	Blue
    39  	// Magenta ...
    40  	Magenta
    41  	// Cyan ...
    42  	Cyan
    43  	// White ...
    44  	White
    45  )
    46  
    47  // Color represents a text color.
    48  type Color uint8
    49  
    50  // Add adds the coloring to the given string.
    51  func (c Color) Add(s string) string {
    52  	return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c), s)
    53  }
    54  
    55  var (
    56  	_levelToColor = map[zapcore.Level]Color{
    57  		zapcore.DebugLevel:  White,
    58  		zapcore.InfoLevel:   Green,
    59  		zapcore.WarnLevel:   Yellow,
    60  		zapcore.ErrorLevel:  Red,
    61  		zapcore.DPanicLevel: Red,
    62  		zapcore.PanicLevel:  Red,
    63  		zapcore.FatalLevel:  Red,
    64  	}
    65  	_unknownLevelColor = Red
    66  
    67  	_levelToLowercaseColorString = make(map[zapcore.Level]string, len(_levelToColor))
    68  	_levelToCapitalColorString   = make(map[zapcore.Level]string, len(_levelToColor))
    69  )
    70  
    71  func init() {
    72  	for level, color := range _levelToColor {
    73  		_levelToLowercaseColorString[level] = color.Add(level.String())
    74  		_levelToCapitalColorString[level] = color.Add(level.CapitalString())
    75  	}
    76  }