Skip to content

Commit

Permalink
Add setupLogging()
Browse files Browse the repository at this point in the history
  • Loading branch information
ryukinix committed Jul 22, 2020
1 parent f93b258 commit a8d5fb1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
/logs.txt
/troll-shield

*.log
15 changes: 15 additions & 0 deletions troll_shield.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"fmt"
"io"
"log"
"os"

Expand All @@ -21,6 +22,8 @@ var trollGroups = []string{
"@commonlispbrofficial",
}

const logfile = "troll-shield.log"

// findTrollHouse return the troll house group name if is well-known
// otherwise, returns a empty string
func findTrollHouse(bot *tgbotapi.BotAPI, userID int) (string, error) {
Expand Down Expand Up @@ -49,7 +52,19 @@ func selectedEvent(update *tgbotapi.Update) bool {
return update.Message != nil && update.Message.NewChatMembers != nil
}

func setupLogging() {
// log to console and file
f, err := os.OpenFile(logfile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
wrt := io.MultiWriter(os.Stdout, f)

log.SetOutput(wrt)
}

func main() {
setupLogging()
token, exists := os.LookupEnv("TELEGRAM_BOT_TOKEN")
if !exists {
log.Fatal("TELEGRAM_BOT_TOKEN env should be defined.")
Expand Down

0 comments on commit a8d5fb1

Please sign in to comment.