filelogger implemented

This commit is contained in:
gempir 2017-03-08 20:08:44 +01:00
parent 1fc7a75d7e
commit 88484995be
7 changed files with 66 additions and 5 deletions

3
.gitignore vendored
View file

@ -2,4 +2,5 @@ gempbotgo
vendor/
glide.lock
.idea/
config.json
config.json
logs/

View file

@ -15,6 +15,6 @@ func NewHandler(logger logging.Logger) handler {
}
}
func (h *handler) HandleMessage(msg twitch.Message) {
h.log.Debug(msg.Text)
func (h *handler) HandleCommand(msg twitch.Message) {
}

View file

@ -2,6 +2,7 @@
"irc_address": "irc.chat.twitch.tv:6667",
"irc_user": "gempbot",
"irc_token": "oauth:123123123",
"log_path": "/var/twitch_logs/",
"api_port": "8025",
"redis_address": "127.0.0.1:6379",
"redis_password": "asdasd",

44
filelog/log.go Normal file
View file

@ -0,0 +1,44 @@
package filelog
import (
"github.com/op/go-logging"
"github.com/gempir/gempbotgo/twitch"
"strings"
"os"
"fmt"
)
type Logger struct {
log logging.Logger
logPath string
}
func NewFileLogger(logPath string,logger logging.Logger) Logger {
return Logger{
log: logger,
logPath: logPath,
}
}
func (l *Logger) LogMessage(msg twitch.Message) {
year := msg.Timestamp.Year()
month := msg.Timestamp.Month()
channel := strings.Replace(msg.Channel, "#", "", 1)
err := os.MkdirAll(fmt.Sprintf(l.logPath+"%s/%d/%s/", channel, year, month), 0755)
if err != nil {
l.log.Error(err.Error())
return
}
filename := fmt.Sprintf(l.logPath+"%s/%d/%s/%s.txt", channel, year, month, msg.User.Username)
file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0755)
if err != nil {
l.log.Error(err.Error())
}
defer file.Close()
contents := fmt.Sprintf("[%s] %s: %s\r\n", msg.Timestamp.Format("2006-01-2 15:04:05"), msg.User.Username, msg.Text)
if _, err = file.WriteString(contents); err != nil {
l.log.Error(err.Error())
}
}

13
main.go
View file

@ -9,6 +9,8 @@ import (
"github.com/gempir/gempbotgo/twitch"
"gopkg.in/redis.v3"
"github.com/gempir/gempbotgo/command"
"strings"
"github.com/gempir/gempbotgo/filelog"
)
var (
@ -20,6 +22,7 @@ type config struct {
IrcAddress string `json:"irc_address"`
IrcUser string `json:"irc_user"`
IrcToken string `json:"irc_token"`
LogPath string `json:"log_path"`
APIPort string `json:"api_port"`
RedisAddress string `json:"redis_address"`
RedisPassword string `json:"redis_password"`
@ -43,10 +46,18 @@ func main() {
bot := twitch.NewBot(cfg.IrcAddress, cfg.IrcUser, cfg.IrcToken, Log, *rClient)
go bot.CreateConnection()
fileLogger := filelog.NewFileLogger(cfg.LogPath, Log)
cmdHandler := command.NewHandler(Log)
for msg := range bot.Messages {
cmdHandler.HandleMessage(msg)
fileLogger.LogMessage(msg)
if strings.HasPrefix(msg.Text, "!") {
cmdHandler.HandleCommand(msg)
}
}
}

View file

@ -124,7 +124,7 @@ func (bot *bot) parseMessage(msg string) {
}
user := newUser(username, tagMap["User-id"], tagMap["color"], tagMap["display-name"], mod, subscriber, turbo)
bot.Messages <- newMessage(message, user, "#" + channel)
bot.Messages <- newMessage(message, user, channel)
}
func (bot *bot) join(channel string) {

View file

@ -1,9 +1,12 @@
package twitch
import "time"
type Message struct {
Text string
User User
Channel string
Timestamp time.Time
}
func newMessage(text string, user User, channel string) Message {
@ -11,5 +14,6 @@ func newMessage(text string, user User, channel string) Message {
Text: text,
User: user,
Channel: channel,
Timestamp: time.Now(),
}
}