mirror of https://github.com/dnomd343/XProxy.git
Dnomd343
1 year ago
6 changed files with 146 additions and 123 deletions
@ -0,0 +1,46 @@ |
|||||
|
package logger |
||||
|
|
||||
|
import ( |
||||
|
"github.com/fatih/color" |
||||
|
"go.uber.org/zap/zapcore" |
||||
|
"time" |
||||
|
) |
||||
|
|
||||
|
func encodeTime(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"))) |
||||
|
} |
||||
|
|
||||
|
func encodeCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { |
||||
|
enc.AppendString("[" + caller.TrimmedPath() + "]") |
||||
|
} |
||||
|
|
||||
|
func encodeColoredCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { |
||||
|
enc.AppendString(color.MagentaString("[" + caller.TrimmedPath() + "]")) |
||||
|
} |
||||
|
|
||||
|
func encodeLevel(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() + "]")) |
||||
|
} |
@ -0,0 +1,39 @@ |
|||||
|
package logger |
||||
|
|
||||
|
import "go.uber.org/zap/zapcore" |
||||
|
|
||||
|
const ( |
||||
|
DebugLevel = zapcore.DebugLevel |
||||
|
InfoLevel = zapcore.InfoLevel |
||||
|
WarnLevel = zapcore.WarnLevel |
||||
|
ErrorLevel = zapcore.ErrorLevel |
||||
|
PanicLevel = zapcore.PanicLevel |
||||
|
) |
||||
|
|
||||
|
func GetLevel() zapcore.Level { |
||||
|
return handle.level.Level() |
||||
|
} |
||||
|
|
||||
|
func SetLevel(level zapcore.Level) { |
||||
|
handle.level.SetLevel(level) |
||||
|
} |
||||
|
|
||||
|
func Debugf(template string, args ...interface{}) { |
||||
|
handle.sugar.Debugf(template, args...) |
||||
|
} |
||||
|
|
||||
|
func Infof(template string, args ...interface{}) { |
||||
|
handle.sugar.Infof(template, args...) |
||||
|
} |
||||
|
|
||||
|
func Warnf(template string, args ...interface{}) { |
||||
|
handle.sugar.Warnf(template, args...) |
||||
|
} |
||||
|
|
||||
|
func Errorf(template string, args ...interface{}) { |
||||
|
handle.sugar.Errorf(template, args...) |
||||
|
} |
||||
|
|
||||
|
func Panicf(template string, args ...interface{}) { |
||||
|
handle.sugar.Panicf(template, args...) |
||||
|
} |
@ -1,134 +1,61 @@ |
|||||
package logger |
package logger |
||||
|
|
||||
import ( |
import ( |
||||
"fmt" |
|
||||
"go.uber.org/zap" |
"go.uber.org/zap" |
||||
"os" |
"go.uber.org/zap/zapcore" |
||||
"time" |
|
||||
) |
) |
||||
import "go.uber.org/zap/zapcore" |
|
||||
|
|
||||
var Logger *zap.SugaredLogger |
type Logger struct { |
||||
|
logger *zap.Logger |
||||
const ( |
level *zap.AtomicLevel |
||||
logTmFmt = "2006-01-02 15:04:05" |
sugar *zap.SugaredLogger |
||||
) |
} |
||||
|
|
||||
func init() { |
|
||||
|
|
||||
//coreConfig := zapcore.EncoderConfig{
|
|
||||
// TimeKey: "ts",
|
|
||||
// LevelKey: "level",
|
|
||||
// NameKey: "logger",
|
|
||||
// CallerKey: "caller",
|
|
||||
// FunctionKey: zapcore.OmitKey,
|
|
||||
// MessageKey: "msg",
|
|
||||
// StacktraceKey: "stacktrace",
|
|
||||
// LineEnding: zapcore.DefaultLineEnding, // 默认换行符"\n"
|
|
||||
// EncodeLevel: zapcore.CapitalColorLevelEncoder,
|
|
||||
// EncodeTime: zapcore.RFC3339TimeEncoder, // 日志时间格式显示
|
|
||||
// EncodeDuration: zapcore.MillisDurationEncoder, // 时间序列化,Duration为经过的浮点秒数
|
|
||||
// EncodeCaller: zapcore.ShortCallerEncoder, // 日志行号显示
|
|
||||
//}
|
|
||||
//
|
|
||||
//encoder := zapcore.NewConsoleEncoder(coreConfig)
|
|
||||
//
|
|
||||
//newCore := zapcore.NewCore(encoder, zapcore.AddSync(os.Stdout), zapcore.DebugLevel)
|
|
||||
//
|
|
||||
//logger := zap.New(newCore)
|
|
||||
//
|
|
||||
//Logger = logger.Sugar()
|
|
||||
|
|
||||
GetLogger() |
|
||||
|
|
||||
//log, err := zap.NewDevelopment()
|
|
||||
//if err != nil {
|
|
||||
// panic("log utils init failed")
|
|
||||
//}
|
|
||||
|
|
||||
// TODO: more zap logger configure
|
var handle Logger |
||||
// TODO: reserve raw logger handle
|
|
||||
|
|
||||
//Logger = log.Sugar()
|
func logConfig(level zap.AtomicLevel, colored bool) zapcore.EncoderConfig { |
||||
|
config := zapcore.EncoderConfig{ |
||||
|
ConsoleSeparator: " ", |
||||
|
MessageKey: "msg", |
||||
|
LevelKey: "level", |
||||
|
TimeKey: "time", |
||||
|
CallerKey: "caller", |
||||
|
EncodeTime: encodeTime, |
||||
|
EncodeLevel: encodeLevel, |
||||
|
EncodeCaller: encodeCaller, |
||||
|
} |
||||
|
if colored { |
||||
|
config.EncodeTime = encodeColoredTime |
||||
|
config.EncodeLevel = encodeColoredLevel |
||||
|
config.EncodeCaller = encodeColoredCaller |
||||
|
} |
||||
|
return config |
||||
} |
} |
||||
|
|
||||
func GetLogger() { |
func init() { |
||||
config := zapcore.EncoderConfig{ |
|
||||
MessageKey: "M", |
|
||||
LevelKey: "L", |
|
||||
TimeKey: "T", |
|
||||
NameKey: "N", |
|
||||
CallerKey: "C", |
|
||||
FunctionKey: "F", |
|
||||
StacktraceKey: "S", |
|
||||
SkipLineEnding: false, |
|
||||
LineEnding: "\n", |
|
||||
|
|
||||
EncodeLevel: logEncodeLevel, |
level := zap.NewAtomicLevelAt(DebugLevel) |
||||
//EncodeLevel: zapcore.CapitalColorLevelEncoder,
|
|
||||
EncodeTime: logEncodeTime, |
|
||||
EncodeDuration: zapcore.StringDurationEncoder, |
|
||||
//EncodeCaller: logEncodeCaller,
|
|
||||
EncodeCaller: zapcore.ShortCallerEncoder, |
|
||||
|
|
||||
//EncodeName: logEncodeName,
|
writer, _, _ := zap.Open("/dev/stderr", "/root/XProxy/next/lalala.log") |
||||
ConsoleSeparator: " ", |
|
||||
} |
|
||||
|
|
||||
//var zc = zap.Config{
|
core := zapcore.NewCore( |
||||
// Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
|
zapcore.NewConsoleEncoder(logConfig(level, false)), |
||||
// Development: false,
|
//zapcore.Lock(os.Stderr),
|
||||
// DisableCaller: false,
|
writer, |
||||
// DisableStacktrace: false,
|
level, |
||||
// Sampling: nil,
|
) |
||||
// Encoding: "json",
|
|
||||
// EncoderConfig: config,
|
|
||||
// OutputPaths: []string{"stdout"},
|
|
||||
// ErrorOutputPaths: []string{"stderr"},
|
|
||||
// InitialFields: map[string]interface{}{"app": "zapdex"},
|
|
||||
//}
|
|
||||
|
|
||||
newCore := zapcore.NewCore(zapcore.NewConsoleEncoder(config), zapcore.Lock(os.Stderr), zapcore.DebugLevel) |
//zapcore.AddSync()
|
||||
//newCore := zapcore.NewCore(zapcore.NewJSONEncoder(config), zapcore.Lock(os.Stderr), zapcore.DebugLevel)
|
//zap.Open()
|
||||
logger := zap.New(newCore, zap.AddCaller()) |
|
||||
//logger := zap.New(newCore)
|
|
||||
//logger, _ := zc.Build()
|
|
||||
//logger.Named("123")
|
|
||||
//fmt.Println(logger.Name())
|
|
||||
|
|
||||
zap.ReplaceGlobals(logger) |
//return core
|
||||
Logger = logger.Sugar() |
|
||||
} |
|
||||
|
|
||||
func logEncodeLevel(level zapcore.Level, enc zapcore.PrimitiveArrayEncoder) { |
logger := zap.New(core, zap.AddCaller()) |
||||
enc.AppendString(func(level zapcore.Level) string { |
|
||||
levelStr := level.CapitalString() |
|
||||
// TODO: using shell codes map
|
|
||||
switch level { |
|
||||
case zapcore.DebugLevel: |
|
||||
return fmt.Sprintf("\x1b[39m[%s]\x1b[0m", levelStr) |
|
||||
case zapcore.InfoLevel: |
|
||||
return fmt.Sprintf("\x1b[32m[%s]\x1b[0m", levelStr) |
|
||||
case zapcore.WarnLevel: |
|
||||
return fmt.Sprintf("\x1b[33m[%s]\x1b[0m", levelStr) |
|
||||
case zapcore.ErrorLevel: |
|
||||
return fmt.Sprintf("\x1b[31m[%s]\x1b[0m", levelStr) |
|
||||
case zapcore.PanicLevel: |
|
||||
return fmt.Sprintf("\x1b[95m[%s]\x1b[0m", levelStr) |
|
||||
default: |
|
||||
return fmt.Sprintf("[%s]", levelStr) |
|
||||
} |
|
||||
}(level)) |
|
||||
} |
|
||||
|
|
||||
func logEncodeTime(t time.Time, enc zapcore.PrimitiveArrayEncoder) { |
handle = Logger{ |
||||
// TODO: using `2006-01-02 15:04:05.xxx` format
|
logger: logger, |
||||
enc.AppendString(fmt.Sprintf( |
level: &level, |
||||
"\x1b[36m%s\x1b[0m \x1b[90m%s\x1b[0m", |
sugar: logger.Sugar(), |
||||
"[XProxy]", t.Format("2006-01-02 15:04:05"), |
} |
||||
)) |
|
||||
} |
|
||||
|
|
||||
func logEncodeCaller(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder) { |
|
||||
enc.AppendString("{" + caller.TrimmedPath() + "}") |
|
||||
} |
} |
||||
|
@ -1,13 +1,13 @@ |
|||||
package main |
package main |
||||
|
|
||||
import ( |
import "XProxy/next/logger" |
||||
. "XProxy/next/logger" |
|
||||
) |
|
||||
|
|
||||
func main() { |
func main() { |
||||
Logger.Debugf("here is %s level", "debug") |
|
||||
Logger.Infof("here is %s level", "info") |
logger.Debugf("here is %s level", "debug") |
||||
Logger.Warnf("here is %s level", "warn") |
logger.Infof("here is %s level", "info") |
||||
Logger.Errorf("here is %s level", "error") |
logger.Warnf("here is %s level", "warn") |
||||
//Logger.Panicf("here is %s level", "panic")
|
logger.Errorf("here is %s level", "error") |
||||
|
//logger.Panicf("here is %s level", "panic")
|
||||
|
|
||||
} |
} |
||||
|
Loading…
Reference in new issue