-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* integrate telegram as an optional system logger * fix config.example.yml * update readme * add some unit tests
- Loading branch information
Showing
10 changed files
with
187 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package syslog | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" | ||
"github.com/utkuufuk/entrello/internal/config" | ||
) | ||
|
||
type Logger struct { | ||
enabled bool | ||
api *tgbotapi.BotAPI | ||
chatId int64 | ||
} | ||
|
||
// NewLogger creates a new system logger instance | ||
func NewLogger(cfg config.Telegram) (l Logger) { | ||
if !cfg.Enabled { | ||
return l | ||
} | ||
|
||
api, err := tgbotapi.NewBotAPI(cfg.Token) | ||
if err != nil { | ||
log.Printf("could not create Telegram Logger: %v", err) | ||
return l | ||
} | ||
return Logger{true, api, cfg.ChatId} | ||
} | ||
|
||
// Printf logs an informational message to stdout, and also sends a Telegram message if enabled | ||
func (l Logger) Printf(msg string, v ...interface{}) { | ||
l.logf("Entrello:", msg, v...) | ||
} | ||
|
||
// Errorf logs an error message to stdout, and also sends a Telegram message if enabled | ||
func (l Logger) Errorf(msg string, v ...interface{}) { | ||
l.logf("Entrello Error:", msg, v...) | ||
} | ||
|
||
// Fatalf works like Errorf, but it returns with a non-zero exit code after logging | ||
func (l Logger) Fatalf(msg string, v ...interface{}) { | ||
l.Errorf(msg, v...) | ||
os.Exit(1) | ||
} | ||
|
||
// logf prints the message to stdout, and after prepending the given prefix to the message, | ||
// also sends a Telegram message if enabled | ||
func (l Logger) logf(prefix, msg string, v ...interface{}) { | ||
msg = fmt.Sprintf(msg, v...) | ||
log.Println(msg) | ||
|
||
if !l.enabled || l.api == nil || l.chatId == 0 { | ||
return | ||
} | ||
msg = fmt.Sprintf("%s %s", prefix, msg) | ||
m := tgbotapi.NewMessage(l.chatId, msg) | ||
l.api.Send(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package syslog | ||
|
||
import ( | ||
"bytes" | ||
"log" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/utkuufuk/entrello/internal/config" | ||
) | ||
|
||
func TestSystemLog(t *testing.T) { | ||
tt := []struct { | ||
name string | ||
cfg config.Telegram | ||
}{ | ||
{ | ||
name: "null config", | ||
cfg: config.Telegram{Enabled: false, Token: "", ChatId: 0}, | ||
}, | ||
{ | ||
name: "psuedo-valid config but disabled", | ||
cfg: config.Telegram{ | ||
Enabled: false, | ||
Token: "xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | ||
ChatId: 1234567890, | ||
}, | ||
}, | ||
{ | ||
name: "psuedo-valid config and enabled", | ||
cfg: config.Telegram{ | ||
Enabled: false, | ||
Token: "xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | ||
ChatId: 1234567890, | ||
}, | ||
}, | ||
{ | ||
name: "enabled but invalid token", | ||
cfg: config.Telegram{ | ||
Enabled: true, | ||
Token: "banana", | ||
ChatId: 1234567890, | ||
}, | ||
}, | ||
{ | ||
name: "enabled but invalid chat ID", | ||
cfg: config.Telegram{ | ||
Enabled: true, | ||
Token: "xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | ||
ChatId: 0, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
t.Run(tc.name, func(t *testing.T) { | ||
msg := "test" | ||
logger := NewLogger(tc.cfg) | ||
var i, e bytes.Buffer | ||
|
||
log.SetOutput(&i) | ||
logger.Printf(msg) | ||
|
||
log.SetOutput(&e) | ||
logger.Errorf(msg) | ||
|
||
log.SetOutput(os.Stderr) | ||
|
||
oi := i.String() | ||
if !strings.Contains(oi, msg) { | ||
t.Errorf("wanted '%s' to contain '%s'", oi, msg) | ||
} | ||
|
||
oe := e.String() | ||
if !strings.Contains(oe, msg) { | ||
t.Errorf("wanted '%s' to contain '%s'", oe, msg) | ||
} | ||
}) | ||
} | ||
} |