Created
January 12, 2018 19:26
-
-
Save prashantv/eb1e5f197812309b6d0b01a6185c552b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"go.uber.org/zap" | |
"go.uber.org/zap/zapcore" | |
lumberjack "gopkg.in/natefinch/lumberjack.v2" | |
"time" | |
) | |
type bufwriter chan []byte | |
func (bw bufwriter) Write(p []byte) (int, error) { | |
bw <- p | |
return len(p), nil | |
} | |
func NewBufwriter(n int, logFile string) bufwriter { | |
w := make(bufwriter, n) | |
logwriter := &lumberjack.Logger{ | |
Filename: logFile, | |
MaxSize: 500, // megabytes | |
MaxBackups: 10, | |
MaxAge: 7, //days | |
} | |
go func(l *lumberjack.Logger, c bufwriter) { | |
fmt.Println("l========", l) | |
fmt.Println("&l========", &l) | |
fmt.Println("c========", c) | |
fmt.Println("&c========", &c) | |
for p := range c { | |
// os.Stdout.Write(p) | |
l.Write(p) | |
} | |
}(logwriter, w) | |
return w | |
} | |
func newZapLogger(logFile string, lvl zapcore.Level) *zap.Logger { | |
dp := "xxxxxxxxx" | |
w := zapcore.AddSync(NewBufwriter(10000, logFile)) | |
cfg := zap.NewProductionEncoderConfig() | |
cfg.TimeKey = "time" | |
core := zapcore.NewCore( | |
zapcore.NewJSONEncoder(cfg), | |
w, | |
lvl, | |
) | |
return zap.New(core).WithOptions(zap.Fields(zap.String("deploy", dp), zap.String("host", "hankji"))) | |
} | |
func main() { | |
z1 := newZapLogger("ccc.log", zap.InfoLevel) | |
z2 := newZapLogger("ddd.log", zap.InfoLevel) | |
for i := 1; i < 20; i++ { | |
go func() { | |
i := 1 | |
for i < 1000 { | |
z2.Info("2222222222") | |
i++ | |
} | |
}() | |
} | |
i := 1 | |
for i < 1000 { | |
z1.Info("1111111111111") | |
i++ | |
} | |
z1.Sync() | |
z2.Sync() | |
time.Sleep(10 * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment