Skip to content

Commit

Permalink
add log.AddTelegramHook() method
Browse files Browse the repository at this point in the history
  • Loading branch information
maxhiker committed Feb 10, 2023
1 parent 6a766a7 commit 291150d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/johntdyer/slack-go v0.0.0-20180213144715-95fac1160b22 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/krasun/logrus2telegram v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/krasun/logrus2telegram v1.0.0 h1:rqDFhdj+X7fa9rX24fwl8pYbTGEqJxT9STRGIy1s5HU=
github.com/krasun/logrus2telegram v1.0.0/go.mod h1:qHuAkjpU6xP8lJ/05Upm0+0dX3ZJhGKA/1kSQbS116g=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=
github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand Down
28 changes: 28 additions & 0 deletions sdk/core/log.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package core

import (
"fmt"
"net/url"
"os"
"sort"
"strings"

"github.com/johntdyer/slackrus"
"github.com/krasun/logrus2telegram"
"github.com/sirupsen/logrus"

"github.com/fatih/color"
Expand Down Expand Up @@ -55,6 +57,32 @@ func (mylog *Log) AddSlackHook(hookUrl, level string) {

}

func (mylog *Log) AddTelegramHook(token string, chat_id int64, levels []logrus.Level) error {
hook, err := logrus2telegram.NewHook(
token,
[]int64{chat_id},
// the levels of messages sent to Telegram
// default: []logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel, logrus.WarnLevel, logrus.InfoLevel}
//logrus2telegram.Levels(logrus.AllLevels),
//logrus2telegram.Levels([]logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel}),
logrus2telegram.Levels(levels),
// the levels of messages sent to Telegram with notifications
// default: []logrus.Level{logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel, logrus.WarnLevel, logrus.InfoLevel}
//logrus2telegram.NotifyOn([]logrus.Level{logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel, logrus.InfoLevel}),
// default: 3 * time.second
//logrus2telegram.RequestTimeout(10*time.Second),
// default: entry.String() time="2021-12-22T14:48:56+02:00" level=debug msg="example"
logrus2telegram.Format(func(e *logrus.Entry) (string, error) {
return fmt.Sprintf("%s %s", strings.ToUpper(e.Level.String()), e.Message), nil
}),
)
if err != nil {
return err
}
mylog.AddHook(hook)
return nil
}

func (mylog *Log) SetOutputStdout() {
mylog.SetOutput(os.Stdout)
}
Expand Down

0 comments on commit 291150d

Please sign in to comment.