From 45c974f5938ed53dab57e9674af783b9d0f092e3 Mon Sep 17 00:00:00 2001 From: Thinh Tran Date: Fri, 22 Jul 2016 17:39:55 +0700 Subject: [PATCH] Reformat gelf --- courier/gelf.go | 38 +++++++++++++++++++++++++++++--------- version.go | 2 +- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/courier/gelf.go b/courier/gelf.go index 86dd912..6eccf3d 100644 --- a/courier/gelf.go +++ b/courier/gelf.go @@ -1,11 +1,10 @@ package courier import ( + "encoding/json" "fmt" - "strconv" - "time" - "github.com/robertkowalski/graylog-golang" + "time" ) type Gelf struct { @@ -13,6 +12,15 @@ type Gelf struct { 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{ @@ -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)) + } } diff --git a/version.go b/version.go index 027f0a7..372fdc2 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package main -const VERSION = 1.3 +const VERSION = "1.3.1"