From 88484995be6efdd4b3088571299db0e41c4604c2 Mon Sep 17 00:00:00 2001 From: gempir Date: Wed, 8 Mar 2017 20:08:44 +0100 Subject: [PATCH] filelogger implemented --- .gitignore | 3 ++- command/commands.go | 4 ++-- config.example.json | 1 + filelog/log.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ main.go | 13 ++++++++++++- twitch/bot.go | 2 +- twitch/message.go | 4 ++++ 7 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 filelog/log.go diff --git a/.gitignore b/.gitignore index 1835df8..02b14ff 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ gempbotgo vendor/ glide.lock .idea/ -config.json \ No newline at end of file +config.json +logs/ \ No newline at end of file diff --git a/command/commands.go b/command/commands.go index 03d0e6f..f763544 100644 --- a/command/commands.go +++ b/command/commands.go @@ -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) { + } \ No newline at end of file diff --git a/config.example.json b/config.example.json index 5142619..d71d422 100644 --- a/config.example.json +++ b/config.example.json @@ -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", diff --git a/filelog/log.go b/filelog/log.go new file mode 100644 index 0000000..f9f1d65 --- /dev/null +++ b/filelog/log.go @@ -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()) + } +} \ No newline at end of file diff --git a/main.go b/main.go index 5f12871..f6c3188 100644 --- a/main.go +++ b/main.go @@ -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) + } + } } diff --git a/twitch/bot.go b/twitch/bot.go index ab744fa..f43bd8c 100644 --- a/twitch/bot.go +++ b/twitch/bot.go @@ -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) { diff --git a/twitch/message.go b/twitch/message.go index ab3a1df..d8addbf 100644 --- a/twitch/message.go +++ b/twitch/message.go @@ -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(), } } \ No newline at end of file