config.go
parent
b2b02a1e1b
commit
093e4eee98
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,15 @@
|
||||||
|
package config
|
||||||
|
|
||||||
|
type AppConfig struct {
|
||||||
|
KafkaConf `ini:"kafka"`
|
||||||
|
TailLog `ini:"taillog"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type KafkaConf struct {
|
||||||
|
Address string `ini:"address"`
|
||||||
|
Topic string `ini:"topic"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type TailLog struct {
|
||||||
|
FileName string `ini:"filename"`
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
[kafka]
|
||||||
|
address=43.143.245.135:9093
|
||||||
|
topic=web_log
|
||||||
|
|
||||||
|
[taillog]
|
||||||
|
filename=./mylog.log
|
1
go.mod
1
go.mod
|
@ -14,5 +14,6 @@ require (
|
||||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
|
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
|
||||||
golang.org/x/sys v0.22.0 // indirect
|
golang.org/x/sys v0.22.0 // indirect
|
||||||
gopkg.in/fsnotify.v1 v1.4.7 // indirect
|
gopkg.in/fsnotify.v1 v1.4.7 // indirect
|
||||||
|
gopkg.in/ini.v1 v1.67.0 // indirect
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
|
||||||
)
|
)
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -20,5 +20,7 @@ golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
|
||||||
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
|
||||||
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
|
||||||
|
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||||
|
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
|
||||||
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
|
||||||
|
|
|
@ -28,13 +28,13 @@ func Init(address []string) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendToKafka(topic string, data string) {
|
func SendToKafka(topic, data string) {
|
||||||
//构造一个消息
|
//构造一个消息
|
||||||
msg := &sarama.ProducerMessage{}
|
msg := &sarama.ProducerMessage{}
|
||||||
msg.Topic = topic
|
msg.Topic = topic
|
||||||
msg.Value = sarama.StringEncoder(data)
|
msg.Value = sarama.StringEncoder(data)
|
||||||
|
|
||||||
//发送kafka
|
//发送到kafka
|
||||||
pid, offset, err := client.SendMessage(msg)
|
pid, offset, err := client.SendMessage(msg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("send message err:", err)
|
fmt.Println("send message err:", err)
|
||||||
|
|
37
main.go
37
main.go
|
@ -2,28 +2,59 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gopkg.in/ini.v1"
|
||||||
|
config "logagent/conf"
|
||||||
"logagent/kafka"
|
"logagent/kafka"
|
||||||
"logagent/taillog"
|
"logagent/taillog"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
//logagent的入口程序
|
//logagent的入口程序
|
||||||
|
|
||||||
|
var (
|
||||||
|
cfg = new(config.AppConfig)
|
||||||
|
)
|
||||||
|
|
||||||
func run() {
|
func run() {
|
||||||
//1.读取日志
|
//1.读取日志
|
||||||
//2.发送给kafka
|
for {
|
||||||
|
select {
|
||||||
|
case line := <-taillog.ReadChan():
|
||||||
|
//2.发送给kafka
|
||||||
|
kafka.SendToKafka(cfg.KafkaConf.Topic, line.Text)
|
||||||
|
default:
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
//0.加载配置文件
|
||||||
|
//cfg, err := ini.Load("./conf/config.ini")
|
||||||
|
//if err != nil {
|
||||||
|
// fmt.Println("load ini config err", err)
|
||||||
|
//}
|
||||||
|
//fmt.Println(cfg.Section("kafka").Key("address"))
|
||||||
|
//fmt.Println(cfg.Section("kafka").Key("topic"))
|
||||||
|
//fmt.Println(cfg.Section("taillog").Key("path"))
|
||||||
|
err := ini.MapTo(cfg, "./conf/config.ini")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("load ini err:", err)
|
||||||
|
}
|
||||||
//1.初始化kafka连接
|
//1.初始化kafka连接
|
||||||
err := kafka.Init([]string{"152.136.226.203:9093"})
|
err = kafka.Init([]string{cfg.KafkaConf.Address})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("init kafka failed,error:%v\n", err)
|
fmt.Printf("init kafka failed,error:%v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Println("init kafka success")
|
||||||
//2.打开日志文件准备收集日志
|
//2.打开日志文件准备收集日志
|
||||||
err = taillog.Init("./mylog.log")
|
err = taillog.Init(cfg.TailLog.FileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("taillog init failed,error:%v\n", err)
|
fmt.Printf("taillog init failed,error:%v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Println("init taillog success")
|
||||||
|
run()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
14
mylog.log
14
mylog.log
|
@ -0,0 +1,14 @@
|
||||||
|
1、我会发着呆然后忘记你,接着紧紧闭上眼,想着那一天会有人代替,让我不再想念你——周杰伦《轨迹》
|
||||||
|
2、从前从前有个人爱你很久,但偏偏风渐渐把距离吹得好远,好不容易又能再多爱一天,但故事的最后你好像还是说了拜拜——周杰伦《晴天》
|
||||||
|
3、断了的弦再怎么连,我的感觉你已听不见,你的转变像断掉的弦,再怎么接音都不对,你的改变我能够分辨——周杰伦《断了的弦》
|
||||||
|
4、转身离开,分手说不出来,海鸟跟鱼相爱,只是一场意外——《珊瑚海》
|
||||||
|
5、风筝在阴天搁浅,想念还在等待救援,我拉着线复习你给的温柔——周杰伦《搁浅》
|
||||||
|
6、雨下整夜我的爱溢出就像雨水,窗台蝴蝶像诗里纷飞的美丽章节。我接着写把永远爱你写进诗的结尾,你是我唯一想要的了解——周杰伦《七里香》
|
||||||
|
7、乘着风游荡在蓝天边,一片云掉落在我面前,捏成你的形状,随风跟着我,一口一口吃掉忧愁——《星晴》
|
||||||
|
8、为你弹奏萧邦的夜曲,纪念我死去的爱情,而我为你隐姓埋名,在月光下弹琴——周杰伦《夜曲》
|
||||||
|
9、最美的不是下雨天,是曾与你躲过雨的屋檐——周杰伦《不能说的秘密》
|
||||||
|
10、我送你离开千里之外你无声黑白,沉默年代或许不该太遥远的相爱;我送你离开天涯之外你是否还在,琴声何来生死难猜用一生去等待——周杰伦《千里之外》
|
||||||
|
11、希望他是真的比我还要爱你,我才会逼自己离开。我真的没有天份安静的没这么快,我会学着放弃你是因为我太爱你。——周杰伦《安静》
|
||||||
|
11、希望他是真的比我还要爱你,我才会逼自己离开。我真的没有天份安静的没这么快,我会学着放弃你是因为我太爱你。——周杰伦《安静》
|
||||||
|
10、我送你离开千里之外你无声黑白,沉默年代或许不该太遥远的相爱;我送你离开天涯之外你是否还在,琴声何来生死难猜用一生去等待——周杰伦《千里之外》
|
||||||
|
1、我会发着呆然后忘记你,接着紧紧闭上眼,想着那一天会有人代替,让我不再想念你——周杰伦《轨迹》
|
|
@ -5,12 +5,12 @@ import (
|
||||||
"github.com/hpcloud/tail"
|
"github.com/hpcloud/tail"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//专门从日志文件收集日志的模块
|
||||||
|
|
||||||
var (
|
var (
|
||||||
tailChan *tail.Tail
|
tailChan *tail.Tail
|
||||||
)
|
)
|
||||||
|
|
||||||
//专门从日志文件收集日志的模块
|
|
||||||
|
|
||||||
func Init(fileName string) (err error) {
|
func Init(fileName string) (err error) {
|
||||||
config := tail.Config{
|
config := tail.Config{
|
||||||
Location: &tail.SeekInfo{Offset: 0, Whence: 2}, //从文件的哪个地方开始读 filebeat记录了文件断点的位置
|
Location: &tail.SeekInfo{Offset: 0, Whence: 2}, //从文件的哪个地方开始读 filebeat记录了文件断点的位置
|
||||||
|
@ -25,3 +25,7 @@ func Init(fileName string) (err error) {
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReadChan() <-chan *tail.Line {
|
||||||
|
return tailChan.Lines
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue