1 Star 1 Fork 0

netany/quick

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
aliyunlog.go 3.81 KB
Copy Edit Raw Blame History
liutianzheng authored 2025-06-17 15:11 +08:00 . 错误通知
package logger
import (
"encoding/json"
"fmt"
"strconv"
"time"
sls "github.com/aliyun/aliyun-log-go-sdk"
"google.golang.org/protobuf/proto"
"github.com/go-kratos/kratos/v2/log"
)
// Logger see more detail https://github.com/aliyun/aliyun-log-go-sdk
type Logger interface {
log.Logger
}
type aliyunLog struct {
opts *options
client sls.ClientInterface
}
type options struct {
accessKey string
accessSecret string
endpoint string
project string
logStore string
region string
topic string
source string
}
func defaultOptions() *options {
return &options{
project: "projectName",
logStore: "app",
topic: "topic",
source: "source",
}
}
func WithRegion(region string) Option {
return func(alc *options) {
alc.region = region
}
}
func WithEndpoint(endpoint string) Option {
return func(alc *options) {
alc.endpoint = endpoint
}
}
func WithProject(project string) Option {
return func(alc *options) {
alc.project = project
}
}
func WithLogStore(logStore string) Option {
return func(alc *options) {
alc.logStore = logStore
}
}
func WithAccessKey(ak string) Option {
return func(alc *options) {
alc.accessKey = ak
}
}
func WithAccessSecret(as string) Option {
return func(alc *options) {
alc.accessSecret = as
}
}
func WithSource(source string) Option {
return func(alc *options) {
alc.source = source
}
}
func WithTopic(topic string) Option {
return func(alc *options) {
alc.topic = topic
}
}
type Option func(alc *options)
func (a *aliyunLog) Log(level log.Level, keyVals ...interface{}) error {
contents := make([]*sls.LogContent, 0, len(keyVals)/2+1)
contents = append(contents, &sls.LogContent{
Key: newString(level.Key()),
Value: newString(level.String()),
})
for i := 0; i < len(keyVals); i += 2 {
contents = append(contents, &sls.LogContent{
Key: newString(toString(keyVals[i])),
Value: newString(toString(keyVals[i+1])),
})
}
var logs []*sls.Log
logInst := &sls.Log{
Time: proto.Uint32(uint32(time.Now().Unix())),
Contents: contents,
}
logs = append(logs, logInst)
logGroup := &sls.LogGroup{
Topic: proto.String(a.opts.topic),
Source: proto.String(a.opts.source),
Logs: logs,
}
err := a.client.PutLogs(a.opts.project, a.opts.logStore, logGroup)
return err
}
// NewAliyunLog new aliyun logger with options.
func NewAliyunLog(options ...Option) Logger {
opts := defaultOptions()
for _, o := range options {
o(opts)
}
// 创建日志服务Client。
provider := sls.NewStaticCredentialsProvider(opts.accessKey, opts.accessSecret, "")
client := sls.CreateNormalInterfaceV2(opts.endpoint, provider)
// 设置使用 v4 签名
client.SetAuthVersion(sls.AuthV4)
// 设置地域
client.SetRegion(opts.region)
return &aliyunLog{
opts: opts,
client: client,
}
}
// newString string convert to *string
func newString(s string) *string {
return &s
}
// toString convert any type to string
func toString(v interface{}) string {
var key string
if v == nil {
return key
}
switch v := v.(type) {
case float64:
key = strconv.FormatFloat(v, 'f', -1, 64)
case float32:
key = strconv.FormatFloat(float64(v), 'f', -1, 32)
case int:
key = strconv.Itoa(v)
case uint:
key = strconv.FormatUint(uint64(v), 10)
case int8:
key = strconv.Itoa(int(v))
case uint8:
key = strconv.FormatUint(uint64(v), 10)
case int16:
key = strconv.Itoa(int(v))
case uint16:
key = strconv.FormatUint(uint64(v), 10)
case int32:
key = strconv.Itoa(int(v))
case uint32:
key = strconv.FormatUint(uint64(v), 10)
case int64:
key = strconv.FormatInt(v, 10)
case uint64:
key = strconv.FormatUint(v, 10)
case string:
key = v
case bool:
key = strconv.FormatBool(v)
case []byte:
key = string(v)
case fmt.Stringer:
key = v.String()
default:
newValue, _ := json.Marshal(v)
key = string(newValue)
}
return key
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/netany/quick.git
git@gitee.com:netany/quick.git
netany
quick
quick
74b72eb989a9

Search