Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Commit

Permalink
Change Gelf Log for chaika
Browse files Browse the repository at this point in the history
  • Loading branch information
duythinht committed Dec 20, 2016
1 parent 4b8af49 commit eb3ee3e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
1 change: 0 additions & 1 deletion chaika/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ func SendOverMonitor(message string) {
}
i++
}
fmt.Println(handlers)
}
6 changes: 4 additions & 2 deletions courier/courier.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (

type Courier interface {
Send(serviceName string, catalog string, level string, message string)
GetHost() string
GetPort() int
}

type LogInfo struct {
Host string
Port int64
Port int
Type string
}

Expand Down Expand Up @@ -74,7 +76,7 @@ func GetLogOutput(serviceName string) LogInfo {
CheckError(err)

if portPair != nil {
logInfo.Port, _ = strconv.ParseInt(string(portPair.Value), 10, 64)
logInfo.Port, _ = strconv.Atoi(string(portPair.Value))
}

typePair, _, err := kv.Get(serviceName+"/log/type", nil)
Expand Down
51 changes: 30 additions & 21 deletions courier/gelf.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package courier

import (
"encoding/json"
"fmt"
"github.com/robertkowalski/graylog-golang"
"time"

"github.com/duythinht/gelf"
"github.com/duythinht/gelf/client"
)

type Gelf struct {
Client *gelf.Gelf
Client *client.Gelf
ServiceName string
Host string
Port int
}

var levels = map[string]int32{
var levels = map[string]int{
"DEBUG": 0,
"INFO": 1,
"WARN": 2,
Expand All @@ -21,35 +24,41 @@ var levels = map[string]int32{
"UNKNOWN": 5,
}

func CreateGelf(serviceName string, graylogHost string, graylogPort int64) Courier {
func CreateGelf(serviceName string, graylogHost string, graylogPort int) Courier {
fmt.Println("Next logs of", serviceName, "will be ship to", graylogHost, graylogPort)
client := Gelf{
Client: gelf.New(gelf.Config{
GraylogPort: int(graylogPort),
GraylogHostname: graylogHost,
Client: client.New(client.Config{
GraylogHost: graylogHost,
GraylogPort: graylogPort,
}),
ServiceName: serviceName,
Host: graylogHost,
Port: graylogPort,
}
return client
}

func (g Gelf) GetHost() string {
return g.Host
}

func (g Gelf) GetPort() int {
return g.Port
}

func (g Gelf) Send(serviceName string, catalog string, level string, message string) {
logObj := map[string]interface{}{
"host": "[" + serviceName + "][" + catalog + "]",
"timestamp": time.Now().Unix(),
"message": message,
}

host := fmt.Sprintf("[%s][%s]", serviceName, catalog)

timestamp := time.Now().Unix()

gm := gelf.Create(message).SetHost(host).SetTimestamp(timestamp)

if lvlNumber, ok := levels[level]; ok {
logObj["level"] = lvlNumber
gm.SetLevel(lvlNumber)
} else {
logObj["level"] = 5
gm.SetLevel(5)
}

logBuff, err := json.Marshal(logObj)
if err != nil {
fmt.Printf(err.Error())
} else {
g.Client.Log(string(logBuff))
}
g.Client.Send(gm.ToJSON())
}
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

const VERSION = "1.3.1"
const VERSION = "1.4.0"

0 comments on commit eb3ee3e

Please sign in to comment.