Skip to content

Commit 2daa105

Browse files
committed
日志输出到 es、syslog
1 parent 5394600 commit 2daa105

File tree

7 files changed

+185
-5
lines changed

7 files changed

+185
-5
lines changed

internal/options/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Options struct {
3636
CacheSize int64 `yaml:"CacheSize"`
3737
Plugin []*plugin.Plugin `yaml:"-"`
3838
SupportMIME *replace.SupportMIME `yaml:"SupportMIME"`
39-
OutLog *logging.Config
39+
OutLog *logging.Config `yaml:"OutLog"`
4040
}
4141

4242
type noticeConfig struct {

internal/options/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func ParseOptions() *Options {
3535
},
3636
EsLog: &logging.EsLog{
3737
LogLevel: logrus.InfoLevel,
38-
DSN: "http://127.0,0.1:9001",
38+
DSN: "http://127.0.0.1:9200",
3939
Host: "localhost",
4040
Index: "goblin",
4141
},

internal/reverse/response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (reverse *Reverse) ModifyResponse(shost string) func(response *http.Respons
8888
//todo 抑制消息, 异步告警
8989
target := reverse.AllowSite[shost]
9090
realHost := GetClientIP(response.Request)
91-
addr, _, _ := utils.SplitHost(realHost)
91+
addr, _, err := utils.SplitHost(realHost)
9292
ipLocation := ipinfo.DB.Area(addr)
9393
log.Info("[dintalk]:目标: %s, 来源地址: %s, 地理位置: %s, 请求链接:%s", target, realHost, ipLocation, response.Request.RequestURI)
9494
go func(target, remote, location, ruleName, path string) {

pkg/logging/log.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
//go:build !windows && !nacl && !plan9
2+
// +build !windows,!nacl,!plan9
3+
14
package logging
25

36
import (
7+
"bufio"
8+
"fmt"
49
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
510
es6 "github.com/olivere/elastic"
611
es7 "github.com/olivere/elastic/v7"
@@ -22,10 +27,17 @@ var (
2227

2328
func (es *EsLog) Es7Setup(level logrus.Level) (log *logrus.Logger) {
2429
log = logrus.New()
30+
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
31+
if err != nil {
32+
fmt.Println("Open Src File err", err)
33+
}
34+
writer := bufio.NewWriter(src)
35+
log.SetOutput(writer)
2536
log.SetLevel(level)
2637
client, err := es7.NewClient(es7.SetURL(es.DSN))
2738
if err != nil {
28-
log.Panic(err)
39+
fmt.Printf("es conn fail please check: %s\n", err.Error())
40+
os.Exit(-1)
2941
}
3042
hook, err := els7.NewAsyncElasticHook(client, es.Host, es.LogLevel, es.Index)
3143
if err != nil {
@@ -37,10 +49,17 @@ func (es *EsLog) Es7Setup(level logrus.Level) (log *logrus.Logger) {
3749

3850
func (es *EsLog) Es6Setup(level logrus.Level) (log *logrus.Logger) {
3951
log = logrus.New()
52+
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
53+
if err != nil {
54+
fmt.Println("Open Src File err", err)
55+
}
56+
writer := bufio.NewWriter(src)
57+
log.SetOutput(writer)
4058
log.SetLevel(level)
4159
client, err := es6.NewClient(es6.SetURL(es.DSN))
4260
if err != nil {
43-
log.Panic(err)
61+
fmt.Printf("es conn fail please check: %s\n", err.Error())
62+
os.Exit(-1)
4463
}
4564
hook, err := els6.NewAsyncElasticHook(client, es.Host, es.LogLevel, es.Index)
4665
if err != nil {

pkg/logging/log_win.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//go:build windows || nacl || plan9
2+
// +build windows nacl plan9
3+
4+
package logging
5+
6+
import (
7+
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
8+
es6 "github.com/olivere/elastic"
9+
es7 "github.com/olivere/elastic/v7"
10+
"github.com/sirupsen/logrus"
11+
prefixed "github.com/x-cray/logrus-prefixed-formatter"
12+
els6 "gopkg.in/sohlich/elogrus.v3"
13+
els7 "gopkg.in/sohlich/elogrus.v7"
14+
"os"
15+
"path"
16+
"path/filepath"
17+
)
18+
19+
var (
20+
AccLogger *logrus.Logger
21+
ErrorLogger *logrus.Logger
22+
)
23+
24+
func (es *EsLog) Es7Setup(level logrus.Level) (log *logrus.Logger) {
25+
log = logrus.New()
26+
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
27+
if err != nil {
28+
fmt.Println("Open Src File err", err)
29+
}
30+
writer := bufio.NewWriter(src)
31+
log.SetOutput(writer)
32+
log.SetLevel(level)
33+
client, err := es7.NewClient(es7.SetURL(es.DSN))
34+
if err != nil {
35+
fmt.Printf("es conn fail please check: %s\n", err.Error())
36+
os.Exit(-1)
37+
}
38+
hook, err := els7.NewAsyncElasticHook(client, es.Host, es.LogLevel, es.Index)
39+
if err != nil {
40+
log.Panic(err)
41+
}
42+
log.Hooks.Add(hook)
43+
return log
44+
}
45+
46+
func (es *EsLog) Es6Setup(level logrus.Level) (log *logrus.Logger) {
47+
log = logrus.New()
48+
src, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
49+
if err != nil {
50+
fmt.Println("Open Src File err", err)
51+
}
52+
writer := bufio.NewWriter(src)
53+
log.SetOutput(writer)
54+
log.SetLevel(level)
55+
client, err := es6.NewClient(es6.SetURL(es.DSN))
56+
if err != nil {
57+
fmt.Printf("es conn fail please check: %s\n", err.Error())
58+
os.Exit(-1)
59+
}
60+
hook, err := els6.NewAsyncElasticHook(client, es.Host, es.LogLevel, es.Index)
61+
if err != nil {
62+
log.Panic(err)
63+
}
64+
log.Hooks.Add(hook)
65+
return log
66+
}
67+
68+
func (flog *FileLog) FileSetup(level logrus.Level) (log *logrus.Logger) {
69+
70+
log = &logrus.Logger{
71+
Formatter: &prefixed.TextFormatter{
72+
ForceFormatting: true,
73+
FullTimestamp: true,
74+
TimestampFormat: "2006-01-02 15:04:05",
75+
},
76+
}
77+
if flog.Mode == "json" {
78+
log.SetFormatter(&logrus.JSONFormatter{})
79+
}
80+
log.SetLevel(level)
81+
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
82+
if err != nil {
83+
log.Fatalf("logging.Setup, fail to get current dir")
84+
85+
}
86+
87+
file := path.Join(dir, flog.DSN)
88+
fileOutput, err := rotatelogs.New(file)
89+
if err != nil {
90+
log.Fatalf("logging.Setup, fail to create '%s': %v", flog.DSN, err)
91+
}
92+
log.SetOutput(fileOutput)
93+
return log
94+
}

pkg/logging/type.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
//go:build !windows && !nacl && !plan9
2+
// +build !windows,!nacl,!plan9
3+
14
package logging
25

36
import (

pkg/logging/type_win.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//go:build windows || nacl || plan9
2+
// +build windows nacl plan9
3+
4+
package logging
5+
6+
import (
7+
"fmt"
8+
"github.com/sirupsen/logrus"
9+
"goblin/pkg/utils"
10+
"strings"
11+
)
12+
13+
var outType []string = []string{"es7", "es6", "file"}
14+
var modeType = []string{"json", "text"}
15+
16+
type Config struct {
17+
Type string
18+
LogLevel logrus.Level
19+
EsLog *EsLog
20+
FileLog *FileLog
21+
Syslog *Syslog
22+
}
23+
24+
type EsLog struct {
25+
LogLevel logrus.Level
26+
DSN string
27+
Index string // name
28+
Host string
29+
}
30+
type FileLog struct {
31+
Mode string
32+
DSN string
33+
}
34+
type Syslog struct {
35+
Mode string
36+
DSN string
37+
}
38+
39+
func (conf *Config) New() (log *logrus.Logger) {
40+
switch conf.Type {
41+
case "es7":
42+
return conf.EsLog.Es7Setup(conf.LogLevel)
43+
case "es6":
44+
return conf.EsLog.Es6Setup(conf.LogLevel)
45+
case "file":
46+
return conf.FileLog.FileSetup(conf.LogLevel)
47+
}
48+
return
49+
}
50+
51+
func (cf *Config) ValidateType() error {
52+
if !utils.StrEqualOrInList(cf.Type, outType) {
53+
return fmt.Errorf("log value %s type must %s ", cf.Type, strings.Join(outType, ","))
54+
}
55+
switch cf.Type {
56+
case "file":
57+
if !utils.StrEqualOrInList(cf.FileLog.Mode, modeType) {
58+
return fmt.Errorf("file log value %s type must %s ", cf.Type, strings.Join(modeType, ","))
59+
}
60+
case "syslog":
61+
return fmt.Errorf("no support syslog")
62+
}
63+
return nil
64+
}

0 commit comments

Comments
 (0)