mirror of https://github.com/dnomd343/XProxy.git
Dnomd343
1 year ago
4 changed files with 110 additions and 59 deletions
@ -1,46 +1,58 @@ |
|||
package logger |
|||
|
|||
import ( |
|||
"github.com/fatih/color" |
|||
"fmt" |
|||
"github.com/gookit/color" |
|||
"go.uber.org/zap/zapcore" |
|||
"time" |
|||
) |
|||
|
|||
func encodeTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) { |
|||
// timeEncoder formats the time as a string.
|
|||
func timeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { |
|||
enc.AppendString(t.Format("2006-01-02 15:04:05.000")) |
|||
} |
|||
|
|||
func encodeColoredTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) { |
|||
enc.AppendString(color.WhiteString(t.Format("2006-01-02 15:04:05.000"))) |
|||
// timeColoredEncoder formats the time as a colored string
|
|||
// with `[XProxy]` prefix.
|
|||
func timeColoredEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { |
|||
enc.AppendString(fmt.Sprintf( |
|||
"%s %s", |
|||
color.Cyan.Render("[XProxy]"), |
|||
color.Gray.Render(t.Format("2006-01-02 15:04:05.000")), |
|||
)) |
|||
} |
|||
|
|||
func encodeCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { |
|||
// callerEncoder formats caller in square brackets.
|
|||
func callerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { |
|||
enc.AppendString("[" + caller.TrimmedPath() + "]") |
|||
} |
|||
|
|||
func encodeColoredCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { |
|||
enc.AppendString(color.MagentaString("[" + caller.TrimmedPath() + "]")) |
|||
// callerColoredEncoder formats caller in square brackets
|
|||
// with magenta color.
|
|||
func callerColoredEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { |
|||
enc.AppendString(color.Magenta.Render("[" + caller.TrimmedPath() + "]")) |
|||
} |
|||
|
|||
func encodeLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { |
|||
// levelEncoder formats log level using square brackets.
|
|||
func levelEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { |
|||
enc.AppendString("[" + level.CapitalString() + "]") |
|||
} |
|||
|
|||
func encodeColoredLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { |
|||
enc.AppendString(func(level zapcore.Level) func(string, ...interface{}) string { |
|||
switch level { |
|||
case zapcore.DebugLevel: |
|||
return color.CyanString |
|||
case zapcore.InfoLevel: |
|||
return color.GreenString |
|||
case zapcore.WarnLevel: |
|||
return color.YellowString |
|||
case zapcore.ErrorLevel: |
|||
return color.RedString |
|||
case zapcore.PanicLevel: |
|||
return color.HiRedString |
|||
default: |
|||
return color.WhiteString |
|||
} |
|||
}(level)("[" + level.CapitalString() + "]")) |
|||
// levelColoredEncoder formats log level using square brackets
|
|||
// and uses different colors.
|
|||
func levelColoredEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { |
|||
levelStr := "[" + level.CapitalString() + "]" |
|||
switch level { |
|||
case zapcore.DebugLevel: |
|||
levelStr = color.FgDefault.Render(levelStr) |
|||
case zapcore.InfoLevel: |
|||
levelStr = color.Green.Render(levelStr) |
|||
case zapcore.WarnLevel: |
|||
levelStr = color.Yellow.Render(levelStr) |
|||
case zapcore.ErrorLevel: |
|||
levelStr = color.Red.Render(levelStr) |
|||
case zapcore.PanicLevel: |
|||
levelStr = color.LightRed.Render(levelStr) |
|||
} |
|||
enc.AppendString(levelStr) |
|||
} |
|||
|
@ -1,13 +1,33 @@ |
|||
package main |
|||
|
|||
import "XProxy/next/logger" |
|||
import ( |
|||
"XProxy/next/logger" |
|||
"os" |
|||
) |
|||
|
|||
func main() { |
|||
|
|||
logger.Debugf("here is %s level", "debug") |
|||
logger.Infof("here is %s level", "info") |
|||
logger.Warnf("here is %s level", "warn") |
|||
logger.Errorf("here is %s level", "error") |
|||
//logger.Debugf("here is %s level", "debug")
|
|||
//logger.Infof("here is %s level", "info")
|
|||
//logger.Warnf("here is %s level", "warn")
|
|||
//logger.Errorf("here is %s level", "error")
|
|||
//logger.Panicf("here is %s level", "panic")
|
|||
|
|||
fp1, _ := os.Create("demo_1.log") |
|||
fp2, _ := os.Create("demo_2.log") |
|||
fp3, _ := os.Create("demo_3.log") |
|||
|
|||
logger.Debugf("output msg 1 at debug") |
|||
logger.Infof("output msg 1 at info") |
|||
logger.Warnf("output msg 1 at warn") |
|||
logger.AddOutputs(fp1, fp2) |
|||
logger.SetLevel(logger.InfoLevel) |
|||
logger.Debugf("output msg 2 at debug") |
|||
logger.Infof("output msg 2 at info") |
|||
logger.Warnf("output msg 2 at warn") |
|||
logger.SetLevel(logger.WarnLevel) |
|||
logger.AddOutputs(fp3) |
|||
logger.Debugf("output msg 3 at debug") |
|||
logger.Infof("output msg 3 at info") |
|||
logger.Warnf("output msg 3 at warn") |
|||
} |
|||
|
Loading…
Reference in new issue