Skip to content

Commit 223cb2b

Browse files
GotaXGota
authored and
Gota
committed
first commit
0 parents  commit 223cb2b

18 files changed

+1887
-0
lines changed

.gitignore

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Go template
3+
# Binaries for programs and plugins
4+
*.exe
5+
*.exe~
6+
*.dll
7+
*.so
8+
*.dylib
9+
10+
# Test binary, built with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Dependency directories (remove the comment below to include it)
17+
# vendor/
18+
19+
### JetBrains template
20+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm
21+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
22+
23+
.idea
24+
25+
# User-specific stuff
26+
.idea/**/workspace.xml
27+
.idea/**/tasks.xml
28+
.idea/**/usage.statistics.xml
29+
.idea/**/dictionaries
30+
.idea/**/shelf
31+
32+
# Generated files
33+
.idea/**/contentModel.xml
34+
35+
# Sensitive or high-churn files
36+
.idea/**/dataSources/
37+
.idea/**/dataSources.ids
38+
.idea/**/dataSources.local.xml
39+
.idea/**/sqlDataSources.xml
40+
.idea/**/dynamic.xml
41+
.idea/**/uiDesigner.xml
42+
.idea/**/dbnavigator.xml
43+
44+
# Gradle
45+
.idea/**/gradle.xml
46+
.idea/**/libraries
47+
48+
# Gradle and Maven with auto-import
49+
# When using Gradle or Maven with auto-import, you should exclude module files,
50+
# since they will be recreated, and may cause churn. Uncomment if using
51+
# auto-import.
52+
# .idea/artifacts
53+
# .idea/compiler.xml
54+
# .idea/modules.xml
55+
# .idea/*.iml
56+
# .idea/modules
57+
# *.iml
58+
# *.ipr
59+
60+
# CMake
61+
cmake-build-*/
62+
63+
# Mongo Explorer plugin
64+
.idea/**/mongoSettings.xml
65+
66+
# File-based project format
67+
*.iws
68+
69+
# IntelliJ
70+
out/
71+
72+
# mpeltonen/sbt-idea plugin
73+
.idea_modules/
74+
75+
# JIRA plugin
76+
atlassian-ide-plugin.xml
77+
78+
# Cursive Clojure plugin
79+
.idea/replstate.xml
80+
81+
# Crashlytics plugin (for Android Studio and IntelliJ)
82+
com_crashlytics_export_strings.xml
83+
crashlytics.properties
84+
crashlytics-build.properties
85+
fabric.properties
86+
87+
# Editor-based Rest Client
88+
.idea/httpRequests
89+
90+
# Android studio 3.1+ serialized cache file
91+
.idea/caches/build_file_checksums.ser
92+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 GotaX
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Aliyun Log Hook for [Logrus](https://github.com/sirupsen/logrus)
2+
3+
[![godoc reference](https://godoc.org/github.com/GotaX/logrus-aliyun-log-hook?status.svg)](https://godoc.org/github.com/GotaX/logrus-aliyun-log-hook)
4+
5+
此 Hook 用于将通过 logrus 记录的日志发送到阿里云日志服务.
6+
7+
特点:
8+
9+
- 采用非阻塞设计, 由一个后台线程将日志批量刷到远端日志库.
10+
- 采用轻量级设计, 直接使用 [PugLogs](https://help.aliyun.com/document_detail/29026.html) 接口, 不依赖于 `github.com/aliyun/aliyun-log-go-sdk`
11+
- 内存占用较低, 大约是直接使用 sdk 的 70%
12+
13+
## 使用指南
14+
15+
```go
16+
package main
17+
18+
import (
19+
"math/rand"
20+
"os"
21+
"time"
22+
23+
"github.com/sirupsen/logrus"
24+
25+
"github.com/GotaX/logrus-aliyun-log-hook"
26+
)
27+
28+
func main() {
29+
hook, err := slsh.New(slsh.Config{
30+
Endpoint: os.Getenv("ENDPOINT"), // 接入点, 例如: "cn-hangzhou-intranet.log.aliyuncs.com",
31+
AccessKey: os.Getenv("ACCESS_KEY"), // 授权密钥对: key
32+
AccessSecret: os.Getenv("ACCESS_SECRET"), // 授权密钥对: secret
33+
Project: os.Getenv("PROJECT"), // 日志项目名称
34+
Store: os.Getenv("STORE"), // 日志库名称
35+
Topic: "demo", // 日志 __topic__ 字段
36+
Extra: map[string]string{"service": "demo"}, // 日志附加字段, 可选
37+
// 更多配置说明, 参考字段注释
38+
})
39+
if err != nil {
40+
panic(err)
41+
}
42+
43+
logrus.SetLevel(logrus.TraceLevel)
44+
logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true})
45+
logrus.AddHook(hook)
46+
47+
// 加上这行关闭本地日志输出, 仅写入阿里云日志
48+
// logrus.SetOutput(ioutil.Discard)
49+
50+
time.AfterFunc(5*time.Second, func() { _ = hook.Close() })
51+
52+
for i := 0; i < 10; i++ {
53+
logrus.WithField("n", i).Info("Hi!")
54+
time.Sleep(time.Duration(rand.Intn(3) * int(time.Second)))
55+
}
56+
}
57+
58+
```
59+
60+
## Benchmark
61+
62+
I/O 部分对比, 配置: Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz
63+
64+
`go test -run ^BeachmarkWriter$ -bench=BenchmarkWriter -count 5 -benchmem `
65+
66+
| 名称 | CPU/op | alloc/op | allocs/op |
67+
| ------- | ---------- | ----------- | --------- |
68+
| hook | 110µs ± 1% | 9.51kB ± 0% | 135 ± 0% |
69+
| sls-sdk | 127µs ± 3% | 13.4kB ± 0% | 165 ± 0% |
70+
71+
## 外部依赖
72+
73+
```
74+
.
75+
├ github.com/golang/protobuf/proto
76+
├ github.com/pierrec/lz4
77+
└ github.com/sirupsen/logrus
78+
```
79+

0 commit comments

Comments
 (0)