Beautify logs with xlog

This commit is contained in:
Andreas Neue 2016-03-12 13:09:25 +01:00
parent 13379b29ea
commit 1dea72f6c7
11 changed files with 60 additions and 48 deletions

25
main.go
View File

@ -9,11 +9,12 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"log"
"runtime" "runtime"
"strings" "strings"
"time" "time"
"code.dnix.de/an/xlog"
"github.com/nickvanw/ircx" "github.com/nickvanw/ircx"
"github.com/sorcix/irc" "github.com/sorcix/irc"
"github.com/sorcix/irc/ctcp" "github.com/sorcix/irc/ctcp"
@ -42,13 +43,15 @@ var (
func main() { func main() {
sayCh = make(chan string, 1024) sayCh = make(chan string, 1024)
xlog.Init(xlog.INFO)
//bot := ircx.Classic(*server, *name) //bot := ircx.Classic(*server, *name)
cfg := ircx.Config{User: *name, MaxRetries: 1000} cfg := ircx.Config{User: *name, MaxRetries: 1000}
bot := ircx.New(*server, *name, cfg) bot := ircx.New(*server, *name, cfg)
log.Printf("%s started", SoftwareInfo()) xlog.Info("%s started", SoftwareInfo())
log.Println("Logging in") xlog.Info("Logging in")
if err := bot.Connect(); err != nil { if err := bot.Connect(); err != nil {
log.Panicln("Unable to dial IRC Server:", err) xlog.Fatal("Unable to dial IRC Server: %v", err)
} }
//mods := strings.Split(*modules, ",") //mods := strings.Split(*modules, ",")
@ -81,11 +84,11 @@ func main() {
RegisterHandlers(bot) RegisterHandlers(bot)
bot.HandleLoop() bot.HandleLoop()
log.Println("Exiting") xlog.Info("Exiting")
} }
func RegisterHandlers(bot *ircx.Bot) { func RegisterHandlers(bot *ircx.Bot) {
log.Println("Registering handlers") xlog.Info("Registering handlers")
bot.HandleFunc(irc.RPL_WELCOME, ConnectHandler) bot.HandleFunc(irc.RPL_WELCOME, ConnectHandler)
bot.HandleFunc(irc.PING, PingHandler) bot.HandleFunc(irc.PING, PingHandler)
bot.HandleFunc(irc.PRIVMSG, PrivmsgHandler) bot.HandleFunc(irc.PRIVMSG, PrivmsgHandler)
@ -93,14 +96,14 @@ func RegisterHandlers(bot *ircx.Bot) {
func ConnectHandler(s ircx.Sender, m *irc.Message) { func ConnectHandler(s ircx.Sender, m *irc.Message) {
if *nspass != "" { if *nspass != "" {
log.Printf("Authenticating with NickServ: %v, %v", *name, *nspass) xlog.Info("Authenticating with NickServ: %v, %v", *name, *nspass)
s.Send(&irc.Message{ s.Send(&irc.Message{
Command: irc.PRIVMSG, Command: irc.PRIVMSG,
Params: []string{*nsname}, Params: []string{*nsname},
Trailing: "IDENTIFY " + *name + " " + *nspass, Trailing: "IDENTIFY " + *name + " " + *nspass,
}) })
} }
log.Printf("Joining channels: %v", *channels) xlog.Info("Joining channels: %v", *channels)
for _, ch := range strings.Split(*channels, ",") { for _, ch := range strings.Split(*channels, ",") {
s.Send(&irc.Message{ s.Send(&irc.Message{
Command: irc.JOIN, Command: irc.JOIN,
@ -108,12 +111,12 @@ func ConnectHandler(s ircx.Sender, m *irc.Message) {
}) })
} }
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf("running on %s", SoftwareInfo())) msg := ctcp.Action(fmt.Sprintf("running on %s", SoftwareInfo()))
sayCh <- fmt.Sprintf("%s\n%s", "*", msg) sayCh <- fmt.Sprintf("%s\n%s", "*", msg)
} }
func PingHandler(s ircx.Sender, m *irc.Message) { func PingHandler(s ircx.Sender, m *irc.Message) {
log.Println("PingPong") xlog.Info("PingPong")
s.Send(&irc.Message{ s.Send(&irc.Message{
Command: irc.PONG, Command: irc.PONG,
Params: m.Params, Params: m.Params,
@ -134,7 +137,7 @@ func HandleMessage(m *irc.Message) {
} }
switch tok[0] { switch tok[0] {
case "!version": case "!version":
msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf("running on %s", SoftwareInfo())) msg := ctcp.Action(fmt.Sprintf("running on %s", SoftwareInfo()))
sayCh <- fmt.Sprintf("%s\n%s", "*", msg) sayCh <- fmt.Sprintf("%s\n%s", "*", msg)
//sayCh <- fmt.Sprintf("%s\n%s", "*", SoftwareInfo()) //sayCh <- fmt.Sprintf("%s\n%s", "*", SoftwareInfo())
default: default:

View File

@ -7,16 +7,17 @@
package modules package modules
import ( import (
"log"
"strings" "strings"
"time" "time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
func init() { func init() {
MsgHandlers["coffee"] = coffeeHandleMessage MsgHandlers["coffee"] = coffeeHandleMessage
log.Println("Initializing coffee module") xlog.Info("Cёffee module initialized")
} }
func coffeeHandleMessage(m *irc.Message) { func coffeeHandleMessage(m *irc.Message) {

View File

@ -5,16 +5,17 @@ package modules
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"log"
"os/exec" "os/exec"
"strings" "strings"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
func init() { func init() {
MsgHandlers["fortune"] = fortuneHandleMessage MsgHandlers["fortune"] = fortuneHandleMessage
log.Println("Initializing fortune module") xlog.Info("Fortune module initialized")
} }
func fortuneHandleMessage(m *irc.Message) { func fortuneHandleMessage(m *irc.Message) {

View File

@ -4,16 +4,17 @@ package modules
import ( import (
"fmt" "fmt"
"log"
"strings" "strings"
"time" "time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
func init() { func init() {
MsgHandlers["fuzzytime"] = fuzzytimeHandleMessage MsgHandlers["fuzzytime"] = fuzzytimeHandleMessage
log.Println("Initializing fuzzytime module") xlog.Info("Fuzzytime module initialized")
} }
func fuzzytimeHandleMessage(m *irc.Message) { func fuzzytimeHandleMessage(m *irc.Message) {

View File

@ -5,10 +5,11 @@
package modules package modules
import ( import (
"log"
"strings" "strings"
"time" "time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
@ -18,7 +19,7 @@ var (
func init() { func init() {
MsgHandlers["fuzzytime"] = gogsHandleMessage MsgHandlers["fuzzytime"] = gogsHandleMessage
log.Println("Initializing gogs module") xlog.Info("Gogs module initialized")
} }
func gogsConfig() { func gogsConfig() {

View File

@ -7,7 +7,6 @@
package modules package modules
import ( import (
"log"
"strings" "strings"
"github.com/sorcix/irc" "github.com/sorcix/irc"
@ -24,7 +23,6 @@ func Init(ch chan string, params string) {
for _, param := range strings.Split(params, "!") { for _, param := range strings.Split(params, "!") {
kv := strings.Split(param, ":") kv := strings.Split(param, ":")
ModParams[kv[0]] = kv[1] ModParams[kv[0]] = kv[1]
log.Println(kv[0], kv[1])
} }
} }

View File

@ -11,10 +11,11 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log"
"os" "os"
"time" "time"
"code.dnix.de/an/xlog"
gorss "github.com/jteeuwen/go-pkg-rss" gorss "github.com/jteeuwen/go-pkg-rss"
"github.com/jteeuwen/go-pkg-xmlx" "github.com/jteeuwen/go-pkg-xmlx"
"github.com/sorcix/irc" "github.com/sorcix/irc"
@ -24,8 +25,8 @@ var hideOutput = true
func init() { func init() {
MsgHandlers["rss"] = rssHandleMessage MsgHandlers["rss"] = rssHandleMessage
log.Printf("Initializing rss module")
go rssRun() go rssRun()
xlog.Info("RSS module initialized")
} }
func rssRun() { func rssRun() {
@ -33,7 +34,7 @@ func rssRun() {
path := ModParams["rss-feeds"] path := ModParams["rss-feeds"]
file, err := os.Open(path) file, err := os.Open(path)
if err != nil { if err != nil {
log.Fatal(err) xlog.Fatal(err.Error())
} }
defer file.Close() defer file.Close()
scanner := bufio.NewScanner(file) scanner := bufio.NewScanner(file)
@ -41,7 +42,7 @@ func rssRun() {
go rssPollFeed(scanner.Text(), 5, rssCharsetReader) go rssPollFeed(scanner.Text(), 5, rssCharsetReader)
} }
if err := scanner.Err(); err != nil { if err := scanner.Err(); err != nil {
log.Fatal(err) xlog.Fatal(err.Error())
} }
go func() { go func() {
time.Sleep(60 * time.Second) time.Sleep(60 * time.Second)
@ -55,9 +56,9 @@ func rssHandleMessage(m *irc.Message) {
func rssPollFeed(uri string, timeout int, cr xmlx.CharsetFunc) { func rssPollFeed(uri string, timeout int, cr xmlx.CharsetFunc) {
feed := gorss.New(timeout, true, rssChanHandler, rssItemHandler) feed := gorss.New(timeout, true, rssChanHandler, rssItemHandler)
for { for {
log.Printf("Polling feed: %s", uri) xlog.Info("Polling feed: %s", uri)
if err := feed.Fetch(uri, cr); err != nil { if err := feed.Fetch(uri, cr); err != nil {
log.Printf("[e] %s: %s", "*", uri, err) xlog.Info("[e] %s: %s", "*", uri, err)
SayCh <- fmt.Sprintf("%s\n[RSS] Error %s: %s", "*", uri, err) SayCh <- fmt.Sprintf("%s\n[RSS] Error %s: %s", "*", uri, err)
return return
} }

View File

@ -7,13 +7,14 @@ import (
"fmt" "fmt"
"html" "html"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"net/url" "net/url"
"regexp" "regexp"
"strings" "strings"
"time" "time"
"code.dnix.de/an/xlog"
"flokatirc/util" "flokatirc/util"
"github.com/sorcix/irc" "github.com/sorcix/irc"
@ -48,8 +49,8 @@ var (
func init() { func init() {
MsgHandlers["sc"] = scHandleMessage MsgHandlers["sc"] = scHandleMessage
log.Println("Initializing sc module")
go scScrapeLoop() go scScrapeLoop()
xlog.Info("SC module initialized")
} }
func scHandleMessage(m *irc.Message) { func scHandleMessage(m *irc.Message) {
@ -82,22 +83,22 @@ func scScrapeLoop() {
func scScraper() { func scScraper() {
var data interface{} var data interface{}
log.Println("Scraping SC stats") xlog.Info("Scraping SC stats")
resp, err := http.PostForm(API_URL+API_METHOD, resp, err := http.PostForm(API_URL+API_METHOD,
url.Values{"fans": {"true"}, "fleet": {"true"}, "funds": {"true"}}) url.Values{"fans": {"true"}, "fleet": {"true"}, "funds": {"true"}})
if err != nil { if err != nil {
log.Printf("Error: %v", err) xlog.Info("Error: %v", err)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Printf("Error: %v", err) xlog.Info("Error: %v", err)
return return
} }
err = json.Unmarshal(body, &data) err = json.Unmarshal(body, &data)
if err != nil { if err != nil {
log.Printf("Error: %v", err) xlog.Info("Error: %v", err)
return return
} }
stats := data.(map[string]interface{})["data"].(map[string]interface{}) stats := data.(map[string]interface{})["data"].(map[string]interface{})
@ -135,13 +136,13 @@ func showScStats() {
func showCitizen(handle string) { func showCitizen(handle string) {
resp, err := http.Get(QUERY_CIT_URL + handle) resp, err := http.Get(QUERY_CIT_URL + handle)
if err != nil { if err != nil {
log.Printf("Error: %v", err) xlog.Info("Error: %v", err)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Printf("Error: %v", err) xlog.Info("Error: %v", err)
return return
} }
reName := regexp.MustCompile(RE_CIT_NAME) reName := regexp.MustCompile(RE_CIT_NAME)
@ -175,13 +176,13 @@ func showCitizen(handle string) {
func showOrganization(handle string) { func showOrganization(handle string) {
resp, err := http.Get(QUERY_ORG_URL + handle) resp, err := http.Get(QUERY_ORG_URL + handle)
if err != nil { if err != nil {
log.Printf("Error: %v", err) xlog.Info("Error: %v", err)
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Printf("Error: %v", err) xlog.Info("Error: %v", err)
return return
} }
reName := regexp.MustCompile(RE_ORG_NAME) reName := regexp.MustCompile(RE_ORG_NAME)

View File

@ -7,6 +7,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
@ -521,6 +523,7 @@ var quotes = [][]string{
func init() { func init() {
MsgHandlers["stoll"] = stollHandleMessage MsgHandlers["stoll"] = stollHandleMessage
xlog.Info("Stoll module initialized")
} }
func stollHandleMessage(m *irc.Message) { func stollHandleMessage(m *irc.Message) {

View File

@ -10,11 +10,12 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"strings" "strings"
"time" "time"
"code.dnix.de/an/xlog"
"flokatirc/util" "flokatirc/util"
"github.com/sorcix/irc" "github.com/sorcix/irc"
@ -137,8 +138,8 @@ var (
func init() { func init() {
MsgHandlers["twitch"] = twitchHandleMessage MsgHandlers["twitch"] = twitchHandleMessage
log.Println("Initializing twitch module")
go pollStreamData() go pollStreamData()
xlog.Info("Twitch module initialized")
} }
func twitchHandleMessage(m *irc.Message) { func twitchHandleMessage(m *irc.Message) {
@ -208,11 +209,11 @@ func getTwitchStreamObject(streamname string) TwitchStreamObject {
var tobj TwitchStreamObject var tobj TwitchStreamObject
resp, err := http.Get(twsurl) resp, err := http.Get(twsurl)
if err != nil { if err != nil {
log.Println(err) xlog.Error(err.Error())
} else { } else {
reader, err := ioutil.ReadAll(resp.Body) reader, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Println(err) xlog.Error(err.Error())
} else { } else {
defer resp.Body.Close() defer resp.Body.Close()
json.Unmarshal(reader, &tobj) json.Unmarshal(reader, &tobj)
@ -227,11 +228,11 @@ func getTwitchChannelObject(streamname string) TwitchChannelObject {
var tcobj TwitchChannelObject var tcobj TwitchChannelObject
resp2, err := http.Get(twcurl) resp2, err := http.Get(twcurl)
if err != nil { if err != nil {
log.Println(err) xlog.Error(err.Error())
} else { } else {
reader2, err := ioutil.ReadAll(resp2.Body) reader2, err := ioutil.ReadAll(resp2.Body)
if err != nil { if err != nil {
log.Println(err) xlog.Error(err.Error())
} else { } else {
defer resp2.Body.Close() defer resp2.Body.Close()
json.Unmarshal(reader2, &tcobj) json.Unmarshal(reader2, &tcobj)

View File

@ -10,12 +10,13 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time" "time"
"code.dnix.de/an/xlog"
"github.com/sorcix/irc" "github.com/sorcix/irc"
) )
@ -71,8 +72,8 @@ type WeatherObject struct {
func init() { func init() {
MsgHandlers["weather"] = weatherHandleMessage MsgHandlers["weather"] = weatherHandleMessage
log.Println("Initializing weather module")
go weatherConfig() go weatherConfig()
xlog.Info("Weather module initialized")
} }
func weatherConfig() { func weatherConfig() {
@ -105,19 +106,19 @@ func getWeather(query string) {
q := owmQueryURLPrefix + query + owmQueryURLSuffix + owmQueryAPIKey q := owmQueryURLPrefix + query + owmQueryURLSuffix + owmQueryAPIKey
r, err := http.Get(q) r, err := http.Get(q)
if err != nil { if err != nil {
log.Println(err) xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
} else { } else {
re, err := ioutil.ReadAll(r.Body) re, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
log.Println(err) xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
} else { } else {
defer r.Body.Close() defer r.Body.Close()
var wo WeatherObject var wo WeatherObject
err := json.Unmarshal(re, &wo) err := json.Unmarshal(re, &wo)
if err != nil { if err != nil {
log.Println(err) xlog.Error(err.Error())
SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error())
} else { } else {
var description string var description string