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 |
package logger |
||||
|
|
||||
import ( |
import ( |
||||
"github.com/fatih/color" |
"fmt" |
||||
|
"github.com/gookit/color" |
||||
"go.uber.org/zap/zapcore" |
"go.uber.org/zap/zapcore" |
||||
"time" |
"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")) |
enc.AppendString(t.Format("2006-01-02 15:04:05.000")) |
||||
} |
} |
||||
|
|
||||
func encodeColoredTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) { |
// timeColoredEncoder formats the time as a colored string
|
||||
enc.AppendString(color.WhiteString(t.Format("2006-01-02 15:04:05.000"))) |
// 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() + "]") |
enc.AppendString("[" + caller.TrimmedPath() + "]") |
||||
} |
} |
||||
|
|
||||
func encodeColoredCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { |
// callerColoredEncoder formats caller in square brackets
|
||||
enc.AppendString(color.MagentaString("[" + caller.TrimmedPath() + "]")) |
// 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() + "]") |
enc.AppendString("[" + level.CapitalString() + "]") |
||||
} |
} |
||||
|
|
||||
func encodeColoredLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { |
// levelColoredEncoder formats log level using square brackets
|
||||
enc.AppendString(func(level zapcore.Level) func(string, ...interface{}) string { |
// and uses different colors.
|
||||
switch level { |
func levelColoredEncoder(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { |
||||
case zapcore.DebugLevel: |
levelStr := "[" + level.CapitalString() + "]" |
||||
return color.CyanString |
switch level { |
||||
case zapcore.InfoLevel: |
case zapcore.DebugLevel: |
||||
return color.GreenString |
levelStr = color.FgDefault.Render(levelStr) |
||||
case zapcore.WarnLevel: |
case zapcore.InfoLevel: |
||||
return color.YellowString |
levelStr = color.Green.Render(levelStr) |
||||
case zapcore.ErrorLevel: |
case zapcore.WarnLevel: |
||||
return color.RedString |
levelStr = color.Yellow.Render(levelStr) |
||||
case zapcore.PanicLevel: |
case zapcore.ErrorLevel: |
||||
return color.HiRedString |
levelStr = color.Red.Render(levelStr) |
||||
default: |
case zapcore.PanicLevel: |
||||
return color.WhiteString |
levelStr = color.LightRed.Render(levelStr) |
||||
} |
} |
||||
}(level)("[" + level.CapitalString() + "]")) |
enc.AppendString(levelStr) |
||||
} |
} |
||||
|
@ -1,13 +1,33 @@ |
|||||
package main |
package main |
||||
|
|
||||
import "XProxy/next/logger" |
import ( |
||||
|
"XProxy/next/logger" |
||||
|
"os" |
||||
|
) |
||||
|
|
||||
func main() { |
func main() { |
||||
|
|
||||
logger.Debugf("here is %s level", "debug") |
//logger.Debugf("here is %s level", "debug")
|
||||
logger.Infof("here is %s level", "info") |
//logger.Infof("here is %s level", "info")
|
||||
logger.Warnf("here is %s level", "warn") |
//logger.Warnf("here is %s level", "warn")
|
||||
logger.Errorf("here is %s level", "error") |
//logger.Errorf("here is %s level", "error")
|
||||
//logger.Panicf("here is %s level", "panic")
|
//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