aws-sdk-go-v2自定义日志记录器

huangapple go评论96阅读模式
英文:

aws-sdk-go-v2 custom logger

问题

使用SDK v1时,我可以使用logrus作为自定义日志记录器,例如:

Logger: aws.LoggerFunc(func(args ...interface{}) {
    log.WithField("process", "s3").Debug(args...)
}),

但是在SDK v2中发生了变化,详见https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/logging/

根据https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/config#WithLogger,似乎我需要使用logging.logger。

我在这方面遇到了困难,有人能建议我在这里该怎么做吗?

英文:

With v1 of the SDK i could use logrus for my custom logger, like:

	Logger: aws.LoggerFunc(func(args ...interface{}) {
		log.WithField("process", "s3").Debug(args...)
	}),

This has changed with sdk v2, https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/logging/

It seems i need to use logging.logger as per https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/config#WithLogger

I'm having a hard time using logrus for this purpose, can anyone suggest what i need to do here?

答案1

得分: 4

似乎sdk v2提供了一个函数包装器来满足logging.logger的需求:

import (
   ...
   "github.com/aws/aws-sdk-go-v2/config"
   "github.com/aws/smithy-go/logging"
   log "github.com/sirupsen/logrus"
)

func main() {
    
    logger := logging.LoggerFunc(func(classification logging.Classification, format string, v ...interface{}) {
        // 自定义的日志记录
        log.WithField("process", "s3").Debug(v...)
    })

    cfg, err := config.LoadDefaultConfig(
        context.TODO(),
        ...
        config.WithLogger(logger),
    )
    ....
}
英文:

It seems that sdk v2 offers a func wrapper to satisfy logging.logger:


import (
   ...
   "github.com/aws/aws-sdk-go-v2/config"
   "github.com/aws/smithy-go/logging"
   log "github.com/sirupsen/logrus"
)

func main() {
    
    logger := logging.LoggerFunc(func(classification logging.Classification, format string, v ...interface{}) {
        // your custom logging 
		log.WithField("process", "s3").Debug(v...)
	})

	cfg, err := config.LoadDefaultConfig(
		context.TODO(),
		...
		config.WithLogger(logger),
	)
    ....
}

huangapple
  • 本文由 发表于 2022年1月29日 18:25:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/70904369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定