switch to syslog log

This commit is contained in:
Martin Geno 2017-08-06 16:33:32 +02:00
parent 0ac8fcb908
commit ae15e1341e
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
6 changed files with 36 additions and 22 deletions

View File

@ -2,10 +2,10 @@ package circleci
import ( import (
"fmt" "fmt"
"log"
"net/http" "net/http"
libHTTP "github.com/genofire/golang-lib/http" libHTTP "github.com/genofire/golang-lib/http"
"github.com/genofire/logmania/log"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
"github.com/genofire/hook2xmpp/config" "github.com/genofire/hook2xmpp/config"
@ -37,14 +37,14 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
payload := body["payload"].(map[string]interface{}) payload := body["payload"].(map[string]interface{})
vcsURL, ok := payload["vcs_url"].(string) vcsURL, ok := payload["vcs_url"].(string)
if !ok { if !ok {
log.Error(r.Body) log.Fatal("no readable payload")
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError) http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
return return
} }
hook, ok := h.hooks[vcsURL] hook, ok := h.hooks[vcsURL]
if !ok { if !ok {
log.Errorf("No hook found for: '%s'", vcsURL) log.Fatalf("No hook found for: '%s'", vcsURL)
http.Error(w, fmt.Sprintf("no configuration for circleci for url %s", vcsURL), http.StatusNotFound) http.Error(w, fmt.Sprintf("no configuration for circleci for url %s", vcsURL), http.StatusNotFound)
return return
} }
@ -55,6 +55,6 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
subject := payload["subject"].(string) subject := payload["subject"].(string)
msg := fmt.Sprintf("[%s] %s (%0.fs) - #%0.f: %s \n%s", vcsURL, status, buildTime/1000, buildNum, subject, buildURL) msg := fmt.Sprintf("[%s] %s (%0.fs) - #%0.f: %s \n%s", vcsURL, status, buildTime/1000, buildNum, subject, buildURL)
log.New().AddField("type", "circleci").Info(msg) log.Printf("circle ci: %s", msg)
ownXMPP.Notify(h.client, hook, msg) ownXMPP.Notify(h.client, hook, msg)
} }

View File

@ -2,19 +2,18 @@ package main
import ( import (
"flag" "flag"
"log"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/genofire/logmania/log"
logmania "github.com/genofire/logmania/log/hook/client"
_ "github.com/genofire/logmania/log/hook/output"
"github.com/mattn/go-xmpp" "github.com/mattn/go-xmpp"
"github.com/genofire/hook2xmpp/circleci" "github.com/genofire/hook2xmpp/circleci"
configuration "github.com/genofire/hook2xmpp/config" configuration "github.com/genofire/hook2xmpp/config"
"github.com/genofire/hook2xmpp/git" "github.com/genofire/hook2xmpp/git"
"github.com/genofire/hook2xmpp/syslog"
ownXMPP "github.com/genofire/hook2xmpp/xmpp" ownXMPP "github.com/genofire/hook2xmpp/xmpp"
) )
@ -25,8 +24,8 @@ func main() {
config := configuration.ReadConfigFile(configFile) config := configuration.ReadConfigFile(configFile)
if config.Logmania.Enable { if config.Syslog.Enable {
logmania.Init(config.Logmania.Address, config.Logmania.Token, log.LogLevel(config.Logmania.Level)) syslog.Bind(config)
} }
// load config // load config
@ -45,7 +44,7 @@ func main() {
log.Panic(err) log.Panic(err)
} }
log.Infof("Started hock2xmpp with %s", client.JID()) log.Printf("Started hock2xmpp with %s", client.JID())
client.SendHtml(xmpp.Chat{Remote: config.XMPP.StartupNotify, Type: "chat", Text: "startup of hock2xmpp"}) client.SendHtml(xmpp.Chat{Remote: config.XMPP.StartupNotify, Type: "chat", Text: "startup of hock2xmpp"})
go ownXMPP.Start(client) go ownXMPP.Start(client)
@ -74,5 +73,5 @@ func main() {
srv.Close() srv.Close()
log.Info("received", sig) log.Print("received", sig)
} }

View File

@ -2,19 +2,17 @@ package config
import ( import (
"io/ioutil" "io/ioutil"
"log"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"github.com/genofire/logmania/log"
) )
type Config struct { type Config struct {
Logmania struct { Syslog struct {
Enable bool `toml:"enable"` Enable bool `toml:"enable"`
Type string `toml:"type"`
Address string `toml:"address"` Address string `toml:"address"`
Token string `toml:"token"` } `toml:"syslog"`
Level int `toml:"level"`
} `toml:"logmania"`
WebserverBind string `toml:"webserver_bind"` WebserverBind string `toml:"webserver_bind"`

View File

@ -1,10 +1,10 @@
package git package git
import ( import (
"log"
"net/http" "net/http"
libHTTP "github.com/genofire/golang-lib/http" libHTTP "github.com/genofire/golang-lib/http"
"github.com/genofire/logmania/log"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
"github.com/genofire/hook2xmpp/config" "github.com/genofire/hook2xmpp/config"
@ -54,10 +54,10 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
hook, ok := h.hooks[url] hook, ok := h.hooks[url]
if !ok { if !ok {
log.Errorf("No hook found for: '%s'", url) log.Fatalf("No hook found for: '%s'", url)
return return
} }
log.New().AddField("type", "git").Info(msg) log.Printf("git: %s", msg)
ownXMPP.Notify(h.client, hook, msg) ownXMPP.Notify(h.client, hook, msg)
} }

16
syslog/main.go Normal file
View File

@ -0,0 +1,16 @@
package syslog
import (
"log"
"log/syslog"
"github.com/genofire/hook2xmpp/config"
)
func Bind(config *config.Config) {
sysLog, err := syslog.Dial(config.Syslog.Type, config.Syslog.Address, syslog.LOG_WARNING|syslog.LOG_DAEMON, "hook2xmpp")
if err != nil {
log.Fatal(err)
}
log.SetOutput(sysLog)
}

View File

@ -1,8 +1,9 @@
package xmpp package xmpp
import ( import (
"log"
"github.com/genofire/hook2xmpp/config" "github.com/genofire/hook2xmpp/config"
"github.com/genofire/logmania/log"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
) )
@ -16,7 +17,7 @@ func Start(client *xmpp.Client) {
switch v := m.(type) { switch v := m.(type) {
case xmpp.Chat: case xmpp.Chat:
if v.Type == "chat" { if v.Type == "chat" {
log.Infof("from %s: %s", v.Remote, v.Text) log.Printf("from %s: %s", v.Remote, v.Text)
} }
if v.Type == "groupchat" { if v.Type == "groupchat" {
} }