Skip to content

Instantly share code, notes, and snippets.

@winston-wen
Last active August 22, 2023 08:19
Show Gist options
  • Select an option

  • Save winston-wen/66718f95ef3c82313f5bbf797f286edc to your computer and use it in GitHub Desktop.

Select an option

Save winston-wen/66718f95ef3c82313f5bbf797f286edc to your computer and use it in GitHub Desktop.

Revisions

  1. winston-wen revised this gist Aug 22, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion logging.go
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ func InitLogger() {
    core := zapcore.NewCore(
    zapcore.NewJSONEncoder(conf_enc),
    w2,
    zap.DebugLevel,
    log_level,
    )
    log := zap.New(core)
    zap.ReplaceGlobals(log)
  2. winston-wen revised this gist Aug 22, 2023. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions logging.go
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@ import (
    lbj "gopkg.in/natefinch/lumberjack.v2"
    )

    var log_level zap.AtomicLevel = zap.NewAtomicLevelAt(zap.InfoLevel)

    func InitLogger() {
    w := zapcore.AddSync(&lbj.Logger{
    Filename: "logs/app_name.log",
    @@ -33,6 +35,7 @@ func InitLogger() {
    func main() {
    InitLogger()
    defer zap.S().Sync()
    log_level.SetLevel(zap.DebugLevel)
    for i := 0; i < 114514; i++ {
    for j := 0; j < 1919; j++ {
    zap.S().Debugw("test", "i", i, "j", j)
  3. winston-wen revised this gist Aug 22, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions logging.go
    Original file line number Diff line number Diff line change
    @@ -10,8 +10,8 @@ import (

    func InitLogger() {
    w := zapcore.AddSync(&lbj.Logger{
    Filename: "logs/nesha_server.log",
    MaxSize: 4, // MB
    Filename: "logs/app_name.log",
    MaxSize: 4, // MB per file

    // 15 old files and 1 new file. The new file has no suffix.
    MaxBackups: 15,
  4. winston-wen created this gist Aug 22, 2023.
    41 changes: 41 additions & 0 deletions logging.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    package main

    import (
    "os"

    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
    lbj "gopkg.in/natefinch/lumberjack.v2"
    )

    func InitLogger() {
    w := zapcore.AddSync(&lbj.Logger{
    Filename: "logs/nesha_server.log",
    MaxSize: 4, // MB

    // 15 old files and 1 new file. The new file has no suffix.
    MaxBackups: 15,
    })
    w2 := zapcore.NewMultiWriteSyncer(w, zapcore.AddSync(os.Stdout))
    conf_enc := zap.NewProductionEncoderConfig()
    conf_enc.LevelKey = "lv"
    conf_enc.CallerKey = "pos"
    conf_enc.EncodeTime = zapcore.ISO8601TimeEncoder
    core := zapcore.NewCore(
    zapcore.NewJSONEncoder(conf_enc),
    w2,
    zap.DebugLevel,
    )
    log := zap.New(core)
    zap.ReplaceGlobals(log)
    }

    func main() {
    InitLogger()
    defer zap.S().Sync()
    for i := 0; i < 114514; i++ {
    for j := 0; j < 1919; j++ {
    zap.S().Debugw("test", "i", i, "j", j)
    }
    }
    }