lint: don't use global `log`

users can change the logger now via gavalink.Log
This commit is contained in:
Christopher F 2018-08-23 18:15:39 -04:00
parent 566c1b0777
commit 35bfb14058
2 changed files with 16 additions and 8 deletions

View File

@ -2,8 +2,17 @@ package gavalink
import ( import (
"errors" "errors"
"log"
"os"
) )
// Log sets the log.Logger gavalink will write to
var Log *log.Logger
func init() {
Log = log.New(os.Stdout, "(gavalink)", 0)
}
// Lavalink manages a connection to Lavalink Nodes // Lavalink manages a connection to Lavalink Nodes
type Lavalink struct { type Lavalink struct {
shards string shards string
@ -52,7 +61,7 @@ func (lavalink *Lavalink) AddNodes(nodeConfigs ...NodeConfig) error {
} }
// RemoveNode removes a node from the manager // RemoveNode removes a node from the manager
func (lavalink *Lavalink) RemoveNode(node *Node) error { func (lavalink *Lavalink) removeNode(node *Node) error {
idx := -1 idx := -1
for i, n := range lavalink.nodes { for i, n := range lavalink.nodes {
if n == *node { if n == *node {

13
node.go
View File

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"strconv" "strconv"
@ -56,7 +55,7 @@ func (node *Node) open() error {
node.wsConn = ws node.wsConn = ws
go node.listen() go node.listen()
log.Println("node", node.config.WebSocket, "opened") Log.Println("node", node.config.WebSocket, "opened")
return nil return nil
} }
@ -73,20 +72,20 @@ func (node *Node) listen() {
for { for {
msgType, msg, err := node.wsConn.ReadMessage() msgType, msg, err := node.wsConn.ReadMessage()
if err != nil { if err != nil {
log.Println(err) Log.Println(err)
// try to reconnect // try to reconnect
oerr := node.open() oerr := node.open()
if oerr != nil { if oerr != nil {
log.Println("node", node.config.WebSocket, "failed and could not reconnect, destroying.", err, oerr) Log.Println("node", node.config.WebSocket, "failed and could not reconnect, destroying.", err, oerr)
node.manager.RemoveNode(node) node.manager.removeNode(node)
return return
} }
log.Println("node", node.config.WebSocket, "reconnected") Log.Println("node", node.config.WebSocket, "reconnected")
return return
} }
err = node.onEvent(msgType, msg) err = node.onEvent(msgType, msg)
// TODO: better error handling // TODO: better error handling
log.Println(err) Log.Println(err)
} }
} }