1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package cloudwatch
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/bahadirbb/zapcloudwatch"
"go.uber.org/zap"
)
func SetupCloudwatchLogger(group, stream string, opts ...Options) (*zap.Logger, error) {
options := &OptionsStruct{
name: "",
creds: nil,
region: "",
isAsync: true,
level: zap.DebugLevel,
}
for _, opt := range opts {
opt(options)
}
cfg := aws.NewConfig().
WithRegion(options.region).
WithCredentials(options.creds)
cwHook, err := zapcloudwatch.NewCloudwatchHook(
group,
stream,
options.isAsync,
cfg,
options.level,
).GetHook()
if err != nil {
return nil, err
}
config := zap.NewDevelopmentConfig()
config.Encoding = "json"
logger, _ := config.Build()
logger = logger.WithOptions(zap.Hooks(cwHook)).Named(options.name)
return logger, nil
}
|