Merge pull request #178 from mxyns/logger-interface
interface-ified logger
This commit is contained in:
commit
0072d121f5
4 changed files with 45 additions and 28 deletions
|
@ -28,13 +28,13 @@ type Server struct {
|
||||||
logPath string
|
logPath string
|
||||||
bot *bot.Bot
|
bot *bot.Bot
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
fileLogger *filelog.Logger
|
fileLogger filelog.Logger
|
||||||
helixClient helix.TwitchApiClient
|
helixClient helix.TwitchApiClient
|
||||||
assetsHandler http.Handler
|
assetsHandler http.Handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewServer create api Server
|
// NewServer create api Server
|
||||||
func NewServer(cfg *config.Config, bot *bot.Bot, fileLogger *filelog.Logger, helixClient helix.TwitchApiClient, assets fs.FS) Server {
|
func NewServer(cfg *config.Config, bot *bot.Bot, fileLogger filelog.Logger, helixClient helix.TwitchApiClient, assets fs.FS) Server {
|
||||||
build, err := fs.Sub(assets, "web/build")
|
build, err := fs.Sub(assets, "web/build")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("failed to read public assets")
|
log.Fatal("failed to read public assets")
|
||||||
|
|
20
bot/main.go
20
bot/main.go
|
@ -19,7 +19,7 @@ type Bot struct {
|
||||||
startTime time.Time
|
startTime time.Time
|
||||||
cfg *config.Config
|
cfg *config.Config
|
||||||
helixClient helix.TwitchApiClient
|
helixClient helix.TwitchApiClient
|
||||||
fileLogger *filelog.Logger
|
logger filelog.Logger
|
||||||
worker []*worker
|
worker []*worker
|
||||||
channels map[string]helix.UserData
|
channels map[string]helix.UserData
|
||||||
clearchats sync.Map
|
clearchats sync.Map
|
||||||
|
@ -39,7 +39,7 @@ func newWorker(client *twitch.Client) *worker {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewBot create new bot instance
|
// NewBot create new bot instance
|
||||||
func NewBot(cfg *config.Config, helixClient helix.TwitchApiClient, fileLogger *filelog.Logger) *Bot {
|
func NewBot(cfg *config.Config, helixClient helix.TwitchApiClient, fileLogger filelog.Logger) *Bot {
|
||||||
channels, err := helixClient.GetUsersByUserIds(cfg.Channels)
|
channels, err := helixClient.GetUsersByUserIds(cfg.Channels)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("[bot] failed to load configured channels %s", err.Error())
|
log.Fatalf("[bot] failed to load configured channels %s", err.Error())
|
||||||
|
@ -48,7 +48,7 @@ func NewBot(cfg *config.Config, helixClient helix.TwitchApiClient, fileLogger *f
|
||||||
return &Bot{
|
return &Bot{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
helixClient: helixClient,
|
helixClient: helixClient,
|
||||||
fileLogger: fileLogger,
|
logger: fileLogger,
|
||||||
channels: channels,
|
channels: channels,
|
||||||
worker: []*worker{},
|
worker: []*worker{},
|
||||||
OptoutCodes: sync.Map{},
|
OptoutCodes: sync.Map{},
|
||||||
|
@ -150,14 +150,14 @@ func (b *Bot) handlePrivateMessage(message twitch.PrivateMessage) {
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := b.fileLogger.LogPrivateMessageForUser(message.User, message)
|
err := b.logger.LogPrivateMessageForUser(message.User, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := b.fileLogger.LogPrivateMessageForChannel(message)
|
err := b.logger.LogPrivateMessageForChannel(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
@ -170,7 +170,7 @@ func (b *Bot) handleUserNotice(message twitch.UserNoticeMessage) {
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := b.fileLogger.LogUserNoticeMessageForUser(message.User.ID, message)
|
err := b.logger.LogUserNoticeMessageForUser(message.User.ID, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
@ -178,7 +178,7 @@ func (b *Bot) handleUserNotice(message twitch.UserNoticeMessage) {
|
||||||
|
|
||||||
if _, ok := message.Tags["msg-param-recipient-id"]; ok {
|
if _, ok := message.Tags["msg-param-recipient-id"]; ok {
|
||||||
go func() {
|
go func() {
|
||||||
err := b.fileLogger.LogUserNoticeMessageForUser(message.Tags["msg-param-recipient-id"], message)
|
err := b.logger.LogUserNoticeMessageForUser(message.Tags["msg-param-recipient-id"], message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
@ -186,7 +186,7 @@ func (b *Bot) handleUserNotice(message twitch.UserNoticeMessage) {
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := b.fileLogger.LogUserNoticeMessageForChannel(message)
|
err := b.logger.LogUserNoticeMessageForChannel(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
@ -223,14 +223,14 @@ func (b *Bot) handleClearChat(message twitch.ClearChatMessage) {
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := b.fileLogger.LogClearchatMessageForUser(message.TargetUserID, message)
|
err := b.logger.LogClearchatMessageForUser(message.TargetUserID, message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
err := b.fileLogger.LogClearchatMessageForChannel(message)
|
err := b.logger.LogClearchatMessageForChannel(message)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err.Error())
|
log.Error(err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,7 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (l *Logger) LogPrivateMessageForChannel(message twitch.PrivateMessage) error {
|
func (l *FileLogger) LogPrivateMessageForChannel(message twitch.PrivateMessage) error {
|
||||||
year := message.Time.Year()
|
year := message.Time.Year()
|
||||||
month := int(message.Time.Month())
|
month := int(message.Time.Month())
|
||||||
day := message.Time.Day()
|
day := message.Time.Day()
|
||||||
|
@ -38,7 +38,7 @@ func (l *Logger) LogPrivateMessageForChannel(message twitch.PrivateMessage) erro
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) LogClearchatMessageForChannel(message twitch.ClearChatMessage) error {
|
func (l *FileLogger) LogClearchatMessageForChannel(message twitch.ClearChatMessage) error {
|
||||||
year := message.Time.Year()
|
year := message.Time.Year()
|
||||||
month := int(message.Time.Month())
|
month := int(message.Time.Month())
|
||||||
day := message.Time.Day()
|
day := message.Time.Day()
|
||||||
|
@ -61,7 +61,7 @@ func (l *Logger) LogClearchatMessageForChannel(message twitch.ClearChatMessage)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) LogUserNoticeMessageForChannel(message twitch.UserNoticeMessage) error {
|
func (l *FileLogger) LogUserNoticeMessageForChannel(message twitch.UserNoticeMessage) error {
|
||||||
year := message.Time.Year()
|
year := message.Time.Year()
|
||||||
month := int(message.Time.Month())
|
month := int(message.Time.Month())
|
||||||
day := message.Time.Day()
|
day := message.Time.Day()
|
||||||
|
@ -84,7 +84,7 @@ func (l *Logger) LogUserNoticeMessageForChannel(message twitch.UserNoticeMessage
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) ReadLogForChannel(channelID string, year int, month int, day int) ([]string, error) {
|
func (l *FileLogger) ReadLogForChannel(channelID string, year int, month int, day int) ([]string, error) {
|
||||||
filename := fmt.Sprintf(l.logPath+"/%s/%d/%d/%d/channel.txt", channelID, year, month, day)
|
filename := fmt.Sprintf(l.logPath+"/%s/%d/%d/%d/channel.txt", channelID, year, month, day)
|
||||||
|
|
||||||
if _, err := os.Stat(filename); err != nil {
|
if _, err := os.Stat(filename); err != nil {
|
||||||
|
@ -126,7 +126,7 @@ func (l *Logger) ReadLogForChannel(channelID string, year int, month int, day in
|
||||||
return content, nil
|
return content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) ReadRandomMessageForChannel(channelID string) (string, error) {
|
func (l *FileLogger) ReadRandomMessageForChannel(channelID string) (string, error) {
|
||||||
var dayFileList []string
|
var dayFileList []string
|
||||||
var lines []string
|
var lines []string
|
||||||
|
|
||||||
|
|
|
@ -17,17 +17,34 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Logger struct {
|
type Logger interface {
|
||||||
|
LogPrivateMessageForUser(user twitch.User, message twitch.PrivateMessage) error
|
||||||
|
LogClearchatMessageForUser(userID string, message twitch.ClearChatMessage) error
|
||||||
|
LogUserNoticeMessageForUser(userID string, message twitch.UserNoticeMessage) error
|
||||||
|
GetLastLogYearAndMonthForUser(channelID, userID string) (int, int, error)
|
||||||
|
GetAvailableLogsForUser(channelID, userID string) ([]UserLogFile, error)
|
||||||
|
ReadLogForUser(channelID, userID string, year string, month string) ([]string, error)
|
||||||
|
ReadRandomMessageForUser(channelID, userID string) (string, error)
|
||||||
|
|
||||||
|
LogPrivateMessageForChannel(message twitch.PrivateMessage) error
|
||||||
|
LogClearchatMessageForChannel(message twitch.ClearChatMessage) error
|
||||||
|
LogUserNoticeMessageForChannel(message twitch.UserNoticeMessage) error
|
||||||
|
ReadLogForChannel(channelID string, year int, month int, day int) ([]string, error)
|
||||||
|
ReadRandomMessageForChannel(channelID string) (string, error)
|
||||||
|
GetAvailableLogsForChannel(channelID string) ([]ChannelLogFile, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
type FileLogger struct {
|
||||||
logPath string
|
logPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFileLogger(logPath string) Logger {
|
func NewFileLogger(logPath string) FileLogger {
|
||||||
return Logger{
|
return FileLogger{
|
||||||
logPath: logPath,
|
logPath: logPath,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) LogPrivateMessageForUser(user twitch.User, message twitch.PrivateMessage) error {
|
func (l *FileLogger) LogPrivateMessageForUser(user twitch.User, message twitch.PrivateMessage) error {
|
||||||
year := message.Time.Year()
|
year := message.Time.Year()
|
||||||
month := int(message.Time.Month())
|
month := int(message.Time.Month())
|
||||||
|
|
||||||
|
@ -49,7 +66,7 @@ func (l *Logger) LogPrivateMessageForUser(user twitch.User, message twitch.Priva
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) LogClearchatMessageForUser(userID string, message twitch.ClearChatMessage) error {
|
func (l *FileLogger) LogClearchatMessageForUser(userID string, message twitch.ClearChatMessage) error {
|
||||||
year := message.Time.Year()
|
year := message.Time.Year()
|
||||||
month := int(message.Time.Month())
|
month := int(message.Time.Month())
|
||||||
|
|
||||||
|
@ -71,7 +88,7 @@ func (l *Logger) LogClearchatMessageForUser(userID string, message twitch.ClearC
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) LogUserNoticeMessageForUser(userID string, message twitch.UserNoticeMessage) error {
|
func (l *FileLogger) LogUserNoticeMessageForUser(userID string, message twitch.UserNoticeMessage) error {
|
||||||
year := message.Time.Year()
|
year := message.Time.Year()
|
||||||
month := int(message.Time.Month())
|
month := int(message.Time.Month())
|
||||||
|
|
||||||
|
@ -99,7 +116,7 @@ type UserLogFile struct {
|
||||||
Month string `json:"month"`
|
Month string `json:"month"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) GetLastLogYearAndMonthForUser(channelID, userID string) (int, int, error) {
|
func (l *FileLogger) GetLastLogYearAndMonthForUser(channelID, userID string) (int, int, error) {
|
||||||
if channelID == "" || userID == "" {
|
if channelID == "" || userID == "" {
|
||||||
return 0, 0, fmt.Errorf("Invalid channelID: %s or userID: %s", channelID, userID)
|
return 0, 0, fmt.Errorf("Invalid channelID: %s or userID: %s", channelID, userID)
|
||||||
}
|
}
|
||||||
|
@ -148,7 +165,7 @@ func (l *Logger) GetLastLogYearAndMonthForUser(channelID, userID string) (int, i
|
||||||
return 0, 0, errors.New("No logs file")
|
return 0, 0, errors.New("No logs file")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) GetAvailableLogsForUser(channelID, userID string) ([]UserLogFile, error) {
|
func (l *FileLogger) GetAvailableLogsForUser(channelID, userID string) ([]UserLogFile, error) {
|
||||||
if channelID == "" || userID == "" {
|
if channelID == "" || userID == "" {
|
||||||
return []UserLogFile{}, fmt.Errorf("Invalid channelID: %s or userID: %s", channelID, userID)
|
return []UserLogFile{}, fmt.Errorf("Invalid channelID: %s or userID: %s", channelID, userID)
|
||||||
}
|
}
|
||||||
|
@ -201,7 +218,7 @@ type ChannelLogFile struct {
|
||||||
Day string `json:"day"`
|
Day string `json:"day"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) GetAvailableLogsForChannel(channelID string) ([]ChannelLogFile, error) {
|
func (l *FileLogger) GetAvailableLogsForChannel(channelID string) ([]ChannelLogFile, error) {
|
||||||
if channelID == "" {
|
if channelID == "" {
|
||||||
return []ChannelLogFile{}, fmt.Errorf("Invalid channelID: %s", channelID)
|
return []ChannelLogFile{}, fmt.Errorf("Invalid channelID: %s", channelID)
|
||||||
}
|
}
|
||||||
|
@ -263,7 +280,7 @@ func (l *Logger) GetAvailableLogsForChannel(channelID string) ([]ChannelLogFile,
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadLogForUser fetch logs
|
// ReadLogForUser fetch logs
|
||||||
func (l *Logger) ReadLogForUser(channelID, userID string, year string, month string) ([]string, error) {
|
func (l *FileLogger) ReadLogForUser(channelID, userID string, year string, month string) ([]string, error) {
|
||||||
if channelID == "" || userID == "" {
|
if channelID == "" || userID == "" {
|
||||||
return []string{}, fmt.Errorf("Invalid channelID: %s or userID: %s", channelID, userID)
|
return []string{}, fmt.Errorf("Invalid channelID: %s or userID: %s", channelID, userID)
|
||||||
}
|
}
|
||||||
|
@ -310,7 +327,7 @@ func (l *Logger) ReadLogForUser(channelID, userID string, year string, month str
|
||||||
return content, nil
|
return content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) ReadRandomMessageForUser(channelID, userID string) (string, error) {
|
func (l *FileLogger) ReadRandomMessageForUser(channelID, userID string) (string, error) {
|
||||||
var userLogs []string
|
var userLogs []string
|
||||||
var lines []string
|
var lines []string
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue