Skip to content

Commit 7788408

Browse files
improve logging
1 parent 7e57c1a commit 7788408

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.dll
55
*.so
66
*.dylib
7+
RSSBot
78

89
# Test binary, built with `go test -c`
910
*.test
@@ -18,3 +19,4 @@
1819
*/config-prod.yml
1920
*/seen.txt
2021
.DS_Store
22+
*.log

app/bot.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ func FormatAuthor(a string) string {
3535
}
3636

3737
func (b *Bot) Run() error {
38+
if b.config.TestRun {
39+
if err := b.twitterClient.PostTweet("Testing 1,2,3"); err != nil {
40+
log.Debugf("Error posting test tweet. (%s)", err.Error())
41+
return err
42+
}
43+
return nil
44+
}
3845
defer b.store.Close()
3946

4047
pendingTweets := make([]string, 0)
@@ -72,7 +79,13 @@ func (b *Bot) Run() error {
7279
return nil
7380
}
7481

75-
log.Infof("Sending %s tweets to Twitter API", len(pendingTweets))
82+
if len(pendingTweets) > 0 {
83+
log.Infof("Sending %d tweets to Twitter API", len(pendingTweets))
84+
} else {
85+
log.Info("No tweets to send. Exiting.")
86+
return nil
87+
}
88+
7689
for _, tweet := range pendingTweets {
7790
log.Infof("Posting tweet (%s)", tweet)
7891
if err := b.twitterClient.PostTweet(tweet); err != nil {
@@ -81,7 +94,6 @@ func (b *Bot) Run() error {
8194
}
8295
}
8396
log.Info("Done sending tweets")
84-
8597
return nil
8698
}
8799

app/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"flag"
5+
"os"
56

67
log "github.com/sirupsen/logrus"
78
)
@@ -17,6 +18,15 @@ func main() {
1718
flag.StringVar(&seenPath, "seen", "seen.txt", "Path to text file containing seen URLs.")
1819
flag.Parse()
1920

21+
// Setup logging to file
22+
f, err := os.OpenFile("rssbot.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
23+
if err != nil {
24+
log.Errorf("error opening file: %v", err)
25+
}
26+
defer f.Close()
27+
log.SetOutput(f)
28+
log.SetLevel(log.InfoLevel)
29+
2030
cfg, err := LoadConfig(configPath)
2131
if err != nil {
2232
log.Fatalf("Error loading config. Shutting Down. (%s)", err.Error())

0 commit comments

Comments
 (0)