This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
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.
Merge pull request #13 from palavrapasse/11-enhancement-telegram-writ…
…er-to-keep-logger-message-format enhancement: telegram writer to keep logger message format
- Loading branch information
Showing
2 changed files
with
50 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,32 @@ | ||
package pkg | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"log" | ||
) | ||
|
||
const messageType = "application/json" | ||
|
||
type TelegramClient struct { | ||
url string | ||
chatId string | ||
logs []string | ||
loggers []*log.Logger | ||
} | ||
|
||
func NewTelegramClient(botToken, chatId string) TelegramClient { | ||
|
||
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", botToken) | ||
|
||
return TelegramClient{ | ||
url: url, | ||
logs: levelPrefix, | ||
chatId: chatId, | ||
tw := TelegramWriter{ | ||
botToken: botToken, | ||
chatId: chatId, | ||
} | ||
} | ||
|
||
func (tc TelegramClient) Write(ar Record) { | ||
log := tc.logs[ar.Level] | ||
|
||
body, err := json.Marshal(map[string]string{ | ||
"chat_id": tc.chatId, | ||
"text": fmt.Sprintf("%s%s", log, ar.Message), | ||
}) | ||
loggers := make([]*log.Logger, len(levelPrefix)) | ||
|
||
if err != nil { | ||
return | ||
for i, v := range levelPrefix { | ||
loggers[i] = log.New(tw, v, defaultLoggerFlag) | ||
} | ||
|
||
response, err := http.Post(tc.url, messageType, bytes.NewBuffer(body)) | ||
|
||
if err != nil { | ||
return | ||
return TelegramClient{ | ||
loggers: loggers, | ||
} | ||
} | ||
|
||
defer response.Body.Close() | ||
|
||
func (tc TelegramClient) Write(ar Record) { | ||
tc.loggers[ar.Level].Println(ar.Message) | ||
} |
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,37 @@ | ||
package pkg | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type TelegramWriter struct { | ||
botToken string | ||
chatId string | ||
} | ||
|
||
func (tw TelegramWriter) Write(p []byte) (n int, err error) { | ||
|
||
body, err := json.Marshal(map[string]string{ | ||
"chat_id": tw.chatId, | ||
"text": string(p[:]), | ||
}) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
url := fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", tw.botToken) | ||
|
||
response, err := http.Post(url, messageType, bytes.NewBuffer(body)) | ||
|
||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
defer response.Body.Close() | ||
|
||
return len(p), nil | ||
} |