From 1dea72f6c7f92251c9fb73324e68feb064be7c09 Mon Sep 17 00:00:00 2001 From: Andreas Neue Date: Sat, 12 Mar 2016 13:09:25 +0100 Subject: [PATCH] Beautify logs with xlog --- main.go | 25 ++++++++++++++----------- modules/coffee.go | 5 +++-- modules/fortune.go | 5 +++-- modules/fuzzytime.go | 5 +++-- modules/gogs.go | 5 +++-- modules/modules.go | 2 -- modules/rss.go | 13 +++++++------ modules/sc.go | 21 +++++++++++---------- modules/stoll.go | 3 +++ modules/twitch.go | 13 +++++++------ modules/weather.go | 11 ++++++----- 11 files changed, 60 insertions(+), 48 deletions(-) diff --git a/main.go b/main.go index d68c647..44bed25 100644 --- a/main.go +++ b/main.go @@ -9,11 +9,12 @@ package main import ( "flag" "fmt" - "log" "runtime" "strings" "time" + "code.dnix.de/an/xlog" + "github.com/nickvanw/ircx" "github.com/sorcix/irc" "github.com/sorcix/irc/ctcp" @@ -42,13 +43,15 @@ var ( func main() { sayCh = make(chan string, 1024) + xlog.Init(xlog.INFO) + //bot := ircx.Classic(*server, *name) cfg := ircx.Config{User: *name, MaxRetries: 1000} bot := ircx.New(*server, *name, cfg) - log.Printf("%s started", SoftwareInfo()) - log.Println("Logging in") + xlog.Info("%s started", SoftwareInfo()) + xlog.Info("Logging in") 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, ",") @@ -81,11 +84,11 @@ func main() { RegisterHandlers(bot) bot.HandleLoop() - log.Println("Exiting") + xlog.Info("Exiting") } func RegisterHandlers(bot *ircx.Bot) { - log.Println("Registering handlers") + xlog.Info("Registering handlers") bot.HandleFunc(irc.RPL_WELCOME, ConnectHandler) bot.HandleFunc(irc.PING, PingHandler) bot.HandleFunc(irc.PRIVMSG, PrivmsgHandler) @@ -93,14 +96,14 @@ func RegisterHandlers(bot *ircx.Bot) { func ConnectHandler(s ircx.Sender, m *irc.Message) { if *nspass != "" { - log.Printf("Authenticating with NickServ: %v, %v", *name, *nspass) + xlog.Info("Authenticating with NickServ: %v, %v", *name, *nspass) s.Send(&irc.Message{ Command: irc.PRIVMSG, Params: []string{*nsname}, Trailing: "IDENTIFY " + *name + " " + *nspass, }) } - log.Printf("Joining channels: %v", *channels) + xlog.Info("Joining channels: %v", *channels) for _, ch := range strings.Split(*channels, ",") { s.Send(&irc.Message{ Command: irc.JOIN, @@ -108,12 +111,12 @@ func ConnectHandler(s ircx.Sender, m *irc.Message) { }) } 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) } func PingHandler(s ircx.Sender, m *irc.Message) { - log.Println("PingPong") + xlog.Info("PingPong") s.Send(&irc.Message{ Command: irc.PONG, Params: m.Params, @@ -134,7 +137,7 @@ func HandleMessage(m *irc.Message) { } switch tok[0] { 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", "*", SoftwareInfo()) default: diff --git a/modules/coffee.go b/modules/coffee.go index 4801721..0418e77 100644 --- a/modules/coffee.go +++ b/modules/coffee.go @@ -7,16 +7,17 @@ package modules import ( - "log" "strings" "time" + "code.dnix.de/an/xlog" + "github.com/sorcix/irc" ) func init() { MsgHandlers["coffee"] = coffeeHandleMessage - log.Println("Initializing coffee module") + xlog.Info("Cёffee module initialized") } func coffeeHandleMessage(m *irc.Message) { diff --git a/modules/fortune.go b/modules/fortune.go index b0db561..b71172d 100644 --- a/modules/fortune.go +++ b/modules/fortune.go @@ -5,16 +5,17 @@ package modules import ( "bytes" "fmt" - "log" "os/exec" "strings" + "code.dnix.de/an/xlog" + "github.com/sorcix/irc" ) func init() { MsgHandlers["fortune"] = fortuneHandleMessage - log.Println("Initializing fortune module") + xlog.Info("Fortune module initialized") } func fortuneHandleMessage(m *irc.Message) { diff --git a/modules/fuzzytime.go b/modules/fuzzytime.go index 4ce52cb..b6197f7 100644 --- a/modules/fuzzytime.go +++ b/modules/fuzzytime.go @@ -4,16 +4,17 @@ package modules import ( "fmt" - "log" "strings" "time" + "code.dnix.de/an/xlog" + "github.com/sorcix/irc" ) func init() { MsgHandlers["fuzzytime"] = fuzzytimeHandleMessage - log.Println("Initializing fuzzytime module") + xlog.Info("Fuzzytime module initialized") } func fuzzytimeHandleMessage(m *irc.Message) { diff --git a/modules/gogs.go b/modules/gogs.go index 8d45700..deac5ad 100644 --- a/modules/gogs.go +++ b/modules/gogs.go @@ -5,10 +5,11 @@ package modules import ( - "log" "strings" "time" + "code.dnix.de/an/xlog" + "github.com/sorcix/irc" ) @@ -18,7 +19,7 @@ var ( func init() { MsgHandlers["fuzzytime"] = gogsHandleMessage - log.Println("Initializing gogs module") + xlog.Info("Gogs module initialized") } func gogsConfig() { diff --git a/modules/modules.go b/modules/modules.go index ee61d66..482d3df 100644 --- a/modules/modules.go +++ b/modules/modules.go @@ -7,7 +7,6 @@ package modules import ( - "log" "strings" "github.com/sorcix/irc" @@ -24,7 +23,6 @@ func Init(ch chan string, params string) { for _, param := range strings.Split(params, "!") { kv := strings.Split(param, ":") ModParams[kv[0]] = kv[1] - log.Println(kv[0], kv[1]) } } diff --git a/modules/rss.go b/modules/rss.go index 10b4a5f..93685a4 100644 --- a/modules/rss.go +++ b/modules/rss.go @@ -11,10 +11,11 @@ import ( "errors" "fmt" "io" - "log" "os" "time" + "code.dnix.de/an/xlog" + gorss "github.com/jteeuwen/go-pkg-rss" "github.com/jteeuwen/go-pkg-xmlx" "github.com/sorcix/irc" @@ -24,8 +25,8 @@ var hideOutput = true func init() { MsgHandlers["rss"] = rssHandleMessage - log.Printf("Initializing rss module") go rssRun() + xlog.Info("RSS module initialized") } func rssRun() { @@ -33,7 +34,7 @@ func rssRun() { path := ModParams["rss-feeds"] file, err := os.Open(path) if err != nil { - log.Fatal(err) + xlog.Fatal(err.Error()) } defer file.Close() scanner := bufio.NewScanner(file) @@ -41,7 +42,7 @@ func rssRun() { go rssPollFeed(scanner.Text(), 5, rssCharsetReader) } if err := scanner.Err(); err != nil { - log.Fatal(err) + xlog.Fatal(err.Error()) } go func() { time.Sleep(60 * time.Second) @@ -55,9 +56,9 @@ func rssHandleMessage(m *irc.Message) { func rssPollFeed(uri string, timeout int, cr xmlx.CharsetFunc) { feed := gorss.New(timeout, true, rssChanHandler, rssItemHandler) for { - log.Printf("Polling feed: %s", uri) + xlog.Info("Polling feed: %s", uri) 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) return } diff --git a/modules/sc.go b/modules/sc.go index 96ad87f..3d50d51 100644 --- a/modules/sc.go +++ b/modules/sc.go @@ -7,13 +7,14 @@ import ( "fmt" "html" "io/ioutil" - "log" "net/http" "net/url" "regexp" "strings" "time" + "code.dnix.de/an/xlog" + "flokatirc/util" "github.com/sorcix/irc" @@ -48,8 +49,8 @@ var ( func init() { MsgHandlers["sc"] = scHandleMessage - log.Println("Initializing sc module") go scScrapeLoop() + xlog.Info("SC module initialized") } func scHandleMessage(m *irc.Message) { @@ -82,22 +83,22 @@ func scScrapeLoop() { func scScraper() { var data interface{} - log.Println("Scraping SC stats") + xlog.Info("Scraping SC stats") resp, err := http.PostForm(API_URL+API_METHOD, url.Values{"fans": {"true"}, "fleet": {"true"}, "funds": {"true"}}) if err != nil { - log.Printf("Error: %v", err) + xlog.Info("Error: %v", err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Printf("Error: %v", err) + xlog.Info("Error: %v", err) return } err = json.Unmarshal(body, &data) if err != nil { - log.Printf("Error: %v", err) + xlog.Info("Error: %v", err) return } stats := data.(map[string]interface{})["data"].(map[string]interface{}) @@ -135,13 +136,13 @@ func showScStats() { func showCitizen(handle string) { resp, err := http.Get(QUERY_CIT_URL + handle) if err != nil { - log.Printf("Error: %v", err) + xlog.Info("Error: %v", err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Printf("Error: %v", err) + xlog.Info("Error: %v", err) return } reName := regexp.MustCompile(RE_CIT_NAME) @@ -175,13 +176,13 @@ func showCitizen(handle string) { func showOrganization(handle string) { resp, err := http.Get(QUERY_ORG_URL + handle) if err != nil { - log.Printf("Error: %v", err) + xlog.Info("Error: %v", err) return } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Printf("Error: %v", err) + xlog.Info("Error: %v", err) return } reName := regexp.MustCompile(RE_ORG_NAME) diff --git a/modules/stoll.go b/modules/stoll.go index dd3ca79..59a6931 100644 --- a/modules/stoll.go +++ b/modules/stoll.go @@ -7,6 +7,8 @@ import ( "fmt" "strings" + "code.dnix.de/an/xlog" + "github.com/sorcix/irc" ) @@ -521,6 +523,7 @@ var quotes = [][]string{ func init() { MsgHandlers["stoll"] = stollHandleMessage + xlog.Info("Stoll module initialized") } func stollHandleMessage(m *irc.Message) { diff --git a/modules/twitch.go b/modules/twitch.go index 185d3d1..0500135 100644 --- a/modules/twitch.go +++ b/modules/twitch.go @@ -10,11 +10,12 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "net/http" "strings" "time" + "code.dnix.de/an/xlog" + "flokatirc/util" "github.com/sorcix/irc" @@ -137,8 +138,8 @@ var ( func init() { MsgHandlers["twitch"] = twitchHandleMessage - log.Println("Initializing twitch module") go pollStreamData() + xlog.Info("Twitch module initialized") } func twitchHandleMessage(m *irc.Message) { @@ -208,11 +209,11 @@ func getTwitchStreamObject(streamname string) TwitchStreamObject { var tobj TwitchStreamObject resp, err := http.Get(twsurl) if err != nil { - log.Println(err) + xlog.Error(err.Error()) } else { reader, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Println(err) + xlog.Error(err.Error()) } else { defer resp.Body.Close() json.Unmarshal(reader, &tobj) @@ -227,11 +228,11 @@ func getTwitchChannelObject(streamname string) TwitchChannelObject { var tcobj TwitchChannelObject resp2, err := http.Get(twcurl) if err != nil { - log.Println(err) + xlog.Error(err.Error()) } else { reader2, err := ioutil.ReadAll(resp2.Body) if err != nil { - log.Println(err) + xlog.Error(err.Error()) } else { defer resp2.Body.Close() json.Unmarshal(reader2, &tcobj) diff --git a/modules/weather.go b/modules/weather.go index 6815b9b..76c2ad9 100644 --- a/modules/weather.go +++ b/modules/weather.go @@ -10,12 +10,13 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "net/http" "strconv" "strings" "time" + "code.dnix.de/an/xlog" + "github.com/sorcix/irc" ) @@ -71,8 +72,8 @@ type WeatherObject struct { func init() { MsgHandlers["weather"] = weatherHandleMessage - log.Println("Initializing weather module") go weatherConfig() + xlog.Info("Weather module initialized") } func weatherConfig() { @@ -105,19 +106,19 @@ func getWeather(query string) { q := owmQueryURLPrefix + query + owmQueryURLSuffix + owmQueryAPIKey r, err := http.Get(q) if err != nil { - log.Println(err) + xlog.Error(err.Error()) SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) } else { re, err := ioutil.ReadAll(r.Body) if err != nil { - log.Println(err) + xlog.Error(err.Error()) SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) } else { defer r.Body.Close() var wo WeatherObject err := json.Unmarshal(re, &wo) if err != nil { - log.Println(err) + xlog.Error(err.Error()) SayCh <- fmt.Sprintf("*\n%s", weatherPrefix+err.Error()) } else { var description string