Created
January 7, 2015 09:05
-
-
Save GeertJohan/3d83e85f6e028dcea832 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"github.com/segmentio/go-loggly" | |
"gopkg.in/inconshreveable/log15.v2" | |
) | |
type LogglyHook struct { | |
client *loggly.Client | |
} | |
func NewLogglyHook(token string) *LogglyHook { | |
return &LogglyHook{ | |
client: loggly.New(token), | |
} | |
} | |
func (lh *LogglyHook) Log(r *log15.Record) error { | |
data := make(map[string]interface{}, len(r.Ctx)/2) | |
for i := 0; i < len(r.Ctx); i += 2 { | |
key := r.Ctx[i] | |
value := r.Ctx[i+1] | |
keyStr, ok := key.(string) | |
if !ok { | |
keyStr = fmt.Sprintf("%v", key) | |
} | |
data[keyStr] = value | |
} | |
msg := loggly.Message{ | |
"message": r.Msg, | |
"level": r.Lvl.String(), | |
"timestamp": r.Time, | |
"data": data, | |
} | |
err := lh.client.Send(msg) | |
if err != nil { | |
return err | |
} | |
err = lh.client.Flush() | |
if err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment