Skip to content

Commit

Permalink
whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
progrium committed Mar 27, 2015
1 parent 58cb604 commit 302ab6a
Showing 1 changed file with 77 additions and 77 deletions.
154 changes: 77 additions & 77 deletions adapters/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ import (
"errors"
"log"
"net"
"os"
"strings"
"strconv"
"text/template"
"os"
"strings"
"strconv"
"text/template"


"github.com/gliderlabs/logspout/router"
"github.com/gliderlabs/logspout/adapters/syslog"
"github.com/gliderlabs/logspout/adapters/syslog"
)

var configDefaults = map[string]string{
"JSON_FIELDS": "time:uint,message,docker.hostname,docker.image",
"JSON_TIME": "{{.Time.Unix}}",
"JSON_MESSAGE": "{{.Data}}",
"JSON_SOURCE": "{{.Source}}",
"JSON_DOCKER_HOSTNAME": "{{.Container.Config.Hostname}}",
"JSON_DOCKER_IMAGE": "{{.Container.Config.Image}}",
"JSON_DOCKER_ID": "{{.Container.ID}}",
"JSON_DOCKER_NAME": "{{.ContainerName}}",
"JSON_FIELDS": "time:uint,message,docker.hostname,docker.image",
"JSON_TIME": "{{.Time.Unix}}",
"JSON_MESSAGE": "{{.Data}}",
"JSON_SOURCE": "{{.Source}}",
"JSON_DOCKER_HOSTNAME": "{{.Container.Config.Hostname}}",
"JSON_DOCKER_IMAGE": "{{.Container.Config.Image}}",
"JSON_DOCKER_ID": "{{.Container.ID}}",
"JSON_DOCKER_NAME": "{{.ContainerName}}",
}

func init() {
Expand All @@ -39,10 +39,10 @@ func getopt(name string) string {
}

type JSONAdapter struct {
conn net.Conn
conn net.Conn
route *router.Route
tmpl *template.Template
types map[string]string
tmpl *template.Template
types map[string]string
}

func NewJSONAdapter(route *router.Route) (router.LogAdapter, error) {
Expand All @@ -55,35 +55,35 @@ func NewJSONAdapter(route *router.Route) (router.LogAdapter, error) {
return nil, err
}

fields := strings.Split(getopt("JSON_FIELDS"), ",")
types := make(map[string]string)
var values []string
for _, field := range fields {
parts := strings.Split(field, ":")
if len(parts) > 1 {
types[parts[0]] = parts[1]
}
config := "JSON_"+strings.ToUpper(strings.Replace(parts[0], ".", "_", -1))
values = append(values, parts[0] + ":" + getopt(config))
}
tmplStr := strings.Join(values, "\x00")
tmpl, err := template.New("prejson").Parse(tmplStr)
fields := strings.Split(getopt("JSON_FIELDS"), ",")
types := make(map[string]string)
var values []string
for _, field := range fields {
parts := strings.Split(field, ":")
if len(parts) > 1 {
types[parts[0]] = parts[1]
}
config := "JSON_"+strings.ToUpper(strings.Replace(parts[0], ".", "_", -1))
values = append(values, parts[0] + ":" + getopt(config))
}
tmplStr := strings.Join(values, "\x00")
tmpl, err := template.New("prejson").Parse(tmplStr)
if err != nil {
return nil, err
}

return &JSONAdapter{
route: route,
conn: conn,
tmpl: tmpl,
types: types,
conn: conn,
tmpl: tmpl,
types: types,
}, nil
}

func (a *JSONAdapter) Stream(logstream chan *router.Message) {
defer a.route.Close()
defer a.route.Close()
for message := range logstream {
m := syslog.NewSyslogMessage(message, a.conn)
m := syslog.NewSyslogMessage(message, a.conn)
buf, err := m.Render(a.tmpl)
if err != nil {
log.Println("json:", err)
Expand All @@ -103,47 +103,47 @@ func (a *JSONAdapter) Stream(logstream chan *router.Message) {
}

func buildMap(input string, types map[string]string) map[string]interface{} {
m := make(map[string]interface{})
fields := strings.Split(input, "\x00")
for _, field := range fields {
kvp := strings.SplitN(field, ":", 2)
keys := strings.Split(kvp[0], ".")
mm := m
if len(keys) > 1 {
for _, key := range keys[:len(keys)-1] {
if mm[key] == nil {
mm[key] = make(map[string]interface{})
}
mm = mm[key].(map[string]interface{})
}
}
var value interface{}
var err error
switch types[field] {
case "uint":
value, err = strconv.ParseUint(kvp[1], 10, 64)
if err != nil {
value = nil
}
case "int":
value, err = strconv.ParseInt(kvp[1], 10, 64)
if err != nil {
value = nil
}
case "float":
value, err = strconv.ParseFloat(kvp[1], 64)
if err != nil {
value = nil
}
case "bool":
value, err = strconv.ParseBool(kvp[1])
if err != nil {
value = nil
}
case "":
value = kvp[1]
}
mm[keys[len(keys)-1]] = value
}
return m
m := make(map[string]interface{})
fields := strings.Split(input, "\x00")
for _, field := range fields {
kvp := strings.SplitN(field, ":", 2)
keys := strings.Split(kvp[0], ".")
mm := m
if len(keys) > 1 {
for _, key := range keys[:len(keys)-1] {
if mm[key] == nil {
mm[key] = make(map[string]interface{})
}
mm = mm[key].(map[string]interface{})
}
}
var value interface{}
var err error
switch types[field] {
case "uint":
value, err = strconv.ParseUint(kvp[1], 10, 64)
if err != nil {
value = nil
}
case "int":
value, err = strconv.ParseInt(kvp[1], 10, 64)
if err != nil {
value = nil
}
case "float":
value, err = strconv.ParseFloat(kvp[1], 64)
if err != nil {
value = nil
}
case "bool":
value, err = strconv.ParseBool(kvp[1])
if err != nil {
value = nil
}
case "":
value = kvp[1]
}
mm[keys[len(keys)-1]] = value
}
return m
}

0 comments on commit 302ab6a

Please sign in to comment.