android-project-46group / api-server

MIT License
2 stars 0 forks source link

ログの出力 #45

Closed kokoichi206 closed 1 year ago

kokoichi206 commented 1 year ago

https://pkg.go.dev/log/syslog#pkg-types

Links

DataDog でのログについて

https://github.com/kokoichi206/routines/issues/18#issuecomment-1405958498

kokoichi206 commented 1 year ago

zap

構造化ロギングを行う手段として zap というライブラリを使うのも手かもしれない

package util

import (
    "context"
    "fmt"

    "go.uber.org/zap"
    "go.uber.org/zap/zapcore"
)

type fileLogger struct {
    level   Level
    logger  *zap.Logger
    host    string
    service string
}

func NewFileLogger(
    path string,
    host string,
    service string,
) (Logger, func(), error) {

    cfg := zap.NewProductionConfig()
    cfg.OutputPaths = []string{
        path,
    }

    level := zap.NewAtomicLevel()
    level.SetLevel(zapcore.DebugLevel)
    cfg.Level = level
    cfg.EncoderConfig = zapcore.EncoderConfig{
        LevelKey:   "status",
        TimeKey:    "Time",
        EncodeTime: zapcore.ISO8601TimeEncoder,
    }
    zapLogger, err := cfg.Build()

    if err != nil {
        return nil, nil, err
    }

    logger := &fileLogger{
        level:   Info,
        logger:  zapLogger,
        host:    host,
        service: service,
    }
    return logger, func() {

    }, nil
}

func (f *fileLogger) Critical(ctx context.Context, msg string) {
    f.Print(ctx, Critical, msg)
}

func (f *fileLogger) Error(ctx context.Context, msg string) {
    f.Print(ctx, Error, msg)
}
func (f *fileLogger) Warn(ctx context.Context, msg string) {
    f.Print(ctx, Warn, msg)
}

func (f *fileLogger) Info(ctx context.Context, msg string) {
    f.Print(ctx, Info, msg)
}

func (f *fileLogger) Debug(ctx context.Context, msg string) {
    f.Print(ctx, Degub, msg)
}

func (f *fileLogger) Criticalf(ctx context.Context, msg string, a ...interface{}) {
    f.Print(ctx, Critical, fmt.Sprintf(msg, a...))
}

func (f *fileLogger) Errorf(ctx context.Context, msg string, a ...interface{}) {
    f.Print(ctx, Error, fmt.Sprintf(msg, a...))
}

func (f *fileLogger) Warnf(ctx context.Context, msg string, a ...interface{}) {
    f.Print(ctx, Warn, fmt.Sprintf(msg, a...))
}

func (f *fileLogger) Infof(ctx context.Context, msg string, a ...interface{}) {
    f.Print(ctx, Info, fmt.Sprintf(msg, a...))
}

func (f *fileLogger) Debugf(ctx context.Context, msg string, a ...interface{}) {
    f.Print(ctx, Degub, fmt.Sprintf(msg, a...))
}

func (f *fileLogger) Print(ctx context.Context, lv Level, msg string) {
    if !shouldPrint(f.level, lv) {
        return
    }

    // m := logMessage{
    //  Host:    f.host,
    //  Service: f.service,
    //  Message: msg,
    //  Status:  lv.String(),
    // }
    // s, err := json.Marshal(m)
    // if err != nil {
    //  f.logfile.Write([]byte("{\"Error\": \"Failed to Marshal Struct to Json\"}"))
    // }
    // s = append(s, []byte("\n")...)

    type logMessage struct {
        Host    string `json:"hostname"`
        Service string `json:"service"`
        Message string `json:"message"`
        Status  string `json:"lv.String()"`
    }
    // f.logger.Debug("test",
    //  // Structured context as strongly typed Field values.
    //  zap.String("hostname", f.host),
    //  zap.String("message", f.service),
    //  zap.String("status", lv.String()),
    // )
    f.logger.Sugar().Debug("test",
        // Structured context as strongly typed Field values.
        zap.String("hostname", f.host),
        zap.String("message", f.service),
        zap.String("status", lv.String()),
    )
}

// Set Level after struct is initialized.
// The default log level is set to Info.
func (f *fileLogger) SetLevel(lv Level) {
    f.level = lv

}