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

Commit

Permalink
Reformat gelf
Browse files Browse the repository at this point in the history
  • Loading branch information
duythinht committed Jul 22, 2016
1 parent f46b02b commit 45c974f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
38 changes: 29 additions & 9 deletions courier/gelf.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package courier

import (
"encoding/json"
"fmt"
"strconv"
"time"

"github.com/robertkowalski/graylog-golang"
"time"
)

type Gelf struct {
Client *gelf.Gelf
ServiceName string
}

var levels = map[string]int32{
"DEBUG": 0,
"INFO": 1,
"WARN": 2,
"ERROR": 3,
"FATAL": 4,
"UNKNOWN": 5,
}

func CreateGelf(serviceName string, graylogHost string, graylogPort int64) Courier {
fmt.Println("Next logs of", serviceName, "will be ship to", graylogHost, graylogPort)
client := Gelf{
Expand All @@ -26,10 +34,22 @@ func CreateGelf(serviceName string, graylogHost string, graylogPort int64) Couri
}

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

if lvlNumber, ok := levels[level]; ok {
logObj["level"] = lvlNumber
} else {
logObj["level"] = 5
}

logBuff, err := json.Marshal(logObj)
if err != nil {
fmt.Printf(err.Error())
} else {
g.Client.Log(string(logBuff))
}
}
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
const VERSION = "1.3.1"

0 comments on commit 45c974f

Please sign in to comment.