英文:
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),
)
....
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论