Skip to content

Commit

Permalink
Change logrus to slog
Browse files Browse the repository at this point in the history
  • Loading branch information
trezorg committed May 9, 2024
1 parent f73ad7d commit 682cc3d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 69 deletions.
7 changes: 3 additions & 4 deletions internal/files/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package files
import (
"fmt"
"io"
"log/slog"
"net/http"
"os"

"github.com/trezorg/lingualeo/internal/logger"
)

const (
Expand Down Expand Up @@ -56,7 +55,7 @@ func (f *FileDownloader) Download(url string) (string, error) {
defer func() {
cErr := fd.Close()
if cErr != nil {
logger.Error(cErr)
slog.Error("cannot close write file descriptor", "error", cErr)
}
}()
resp, err := http.Get(url)
Expand All @@ -66,7 +65,7 @@ func (f *FileDownloader) Download(url string) (string, error) {
defer func() {
cErr := resp.Body.Close()
if cErr != nil {
logger.Error(cErr)
slog.Error("cannot close response body", "error", cErr)
}
}()
if resp.StatusCode != http.StatusOK {
Expand Down
63 changes: 24 additions & 39 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,52 +1,37 @@
package logger

import (
"fmt"
"log/slog"
"os"
"sync"

"github.com/sirupsen/logrus"
)

var log *logrus.Entry
var onceLog sync.Once

type utcFormatter struct {
logrus.Formatter
}

func (u utcFormatter) Format(e *logrus.Entry) ([]byte, error) {
e.Time = e.Time.UTC()
return u.Formatter.Format(e)
}

func initLogger(logLevel string, logPrettyPrint bool) {

level, err := logrus.ParseLevel(logLevel)
if err != nil {
logrus.Fatalf("Cannot parse log level: %s", logLevel)
var levelVar = new(slog.LevelVar)

func ParseLevel(level string) (slog.Level, error) {
switch level {
case "DEBUG":
return slog.LevelDebug, nil
case "INFO":
return slog.LevelInfo, nil
case "WARNING", "WARN":
return slog.LevelWarn, nil
case "ERROR":
return slog.LevelError, nil
default:
return slog.LevelError, fmt.Errorf("unknown log level: %s", level)
}

logrus.SetFormatter(utcFormatter{&logrus.JSONFormatter{
PrettyPrint: logPrettyPrint,
TimestampFormat: "2006-01-02 15:04:05 -0700",
}})
logrus.SetReportCaller(true)
logrus.SetOutput(os.Stdout)
logrus.SetLevel(level)
log = logrus.WithFields(logrus.Fields{"service": "lingualeo"})
}

func Error(args ...interface{}) {
log.Error(args...)
}

func Debug(args ...interface{}) {
log.Debug(args...)
func SetLevel(level slog.Level) {
levelVar.Set(level)
}

func InitLogger(logLevel string, logPrettyPrint bool) *logrus.Entry {
onceLog.Do(func() {
initLogger(logLevel, logPrettyPrint)
func Prepare(level slog.Level) {
SetLevel(level)
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: levelVar,
})
return log
logger := slog.New(handler)
slog.SetDefault(logger)
}
6 changes: 3 additions & 3 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/cookiejar"
"net/http/httputil"
Expand All @@ -18,7 +19,6 @@ import (

"github.com/sirupsen/logrus"

"github.com/trezorg/lingualeo/internal/logger"
"github.com/trezorg/lingualeo/pkg/channel"

"golang.org/x/net/publicsuffix"
Expand Down Expand Up @@ -172,12 +172,12 @@ func request(method string, url string, client *http.Client, body []byte, query
defer func() {
dErr := resp.Body.Close()
if dErr != nil {
logger.Error(dErr)
slog.Error("cannot close response body", "error", dErr)
}
}()
responseBody, err := readBody(resp)
if err != nil {
logger.Error(err)
slog.Error("cannot read response body", "error", err)
return nil, err
}
if resp.StatusCode != 200 {
Expand Down
13 changes: 6 additions & 7 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"

"github.com/trezorg/lingualeo/pkg/messages"

"github.com/trezorg/lingualeo/internal/logger"
)

func readBody(resp *http.Response) ([]byte, error) {
defer func() {
err := resp.Body.Close()
if err != nil {
logger.Error(err)
slog.Error("cannot close response body", "error", err)
}
}()
return io.ReadAll(resp.Body)
Expand Down Expand Up @@ -46,11 +45,11 @@ func printTranslation(result *Result) {
}
err := messages.Message(messages.RED, "Found %s word:\n", strTitle)
if err != nil {
logger.Error(err)
slog.Error("cannot show message", "error", err)
}
err = messages.Message(messages.GREEN, "['%s'] (%s)\n", result.Word, result.Transcription)
if err != nil {
logger.Error(err)
slog.Error("cannot show message", "error", err)
}
for _, word := range result.Words {
_ = messages.Message(messages.YELLOW, "%s\n", word)
Expand All @@ -66,10 +65,10 @@ func printAddedTranslation(result *Result) {
}
err := messages.Message(messages.RED, "%s word: ", strTitle)
if err != nil {
logger.Error(err)
slog.Error("cannot show message", "error", err)
}
err = messages.Message(messages.GREEN, "['%s']\n", result.Word)
if err != nil {
logger.Error(err)
slog.Error("cannot show message", "error", err)
}
}
3 changes: 2 additions & 1 deletion pkg/translator/get_word_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package translator

import (
"context"
"log/slog"
"sync"
"testing"

Expand All @@ -28,7 +29,7 @@ func TestProcessTranslationResponseJson(t *testing.T) {
downloader.EXPECT().Download(fakeapi.SoundURL).Return(testFile, nil).Times(count)
translator.EXPECT().TranslateWord(fakeapi.SearchWord).Return(res).Times(count)

logger.InitLogger("FATAL", true)
logger.Prepare(slog.LevelError + 1)
searchWords := make([]string, 0, count)

for i := 0; i < count; i++ {
Expand Down
35 changes: 20 additions & 15 deletions pkg/translator/linguleo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package translator

import (
"context"
"log/slog"
"os"
"strings"
"sync"
Expand All @@ -25,13 +26,13 @@ func (args *Lingualeo) checkMediaPlayer() {
if len(args.Player) == 0 {
err := messages.Message(messages.RED, "Please set player parameter\n")
if err != nil {
logger.Debug(err)
slog.Error("cannot show message", "error", err)
}
args.Sound = false
} else if !isCommandAvailable(args.Player) {
err := messages.Message(messages.RED, "Executable file %s is not available on your system\n", args.Player)
if err != nil {
logger.Debug(err)
slog.Error("cannot show message", "error", err)
}
args.Sound = false
}
Expand Down Expand Up @@ -87,7 +88,11 @@ func New(version string) (Lingualeo, error) {
client.LogLevel = logrus.DebugLevel.String()
client.LogPrettyPrint = true
}
logger.InitLogger(client.LogLevel, client.LogPrettyPrint)
level, err := logger.ParseLevel(client.LogLevel)
if err != nil {
return client, err
}
logger.Prepare(level)
client.checkMediaPlayer()
client.Translator, err = api.New(client.Email, client.Password, client.Debug)
if err != nil {
Expand Down Expand Up @@ -155,15 +160,15 @@ func (args *Lingualeo) translateWords(ctx context.Context) <-chan api.OperationR
cases.Title(language.Make(strings.ToLower(res.Error.Error()))),
)
if err != nil {
logger.Error(err)
slog.Error("cannot show message", "error", err)
}
continue
}
if len(res.Result.Words) == 0 {
_ = messages.Message(messages.RED, "There are no translations for word: ")
err := messages.Message(messages.GREEN, "['%s']\n", res.Result.Word)
if err != nil {
logger.Error(err)
slog.Error("cannot show message", "error", err)
}
continue
}
Expand All @@ -189,29 +194,29 @@ func (args *Lingualeo) downloadAndPronounce(ctx context.Context, urls <-chan str
fileChannel := files.OrderedChannel(DownloadFiles(ctx, urls, downloader), len(urls))
for res := range channel.OrDone(ctx, fileChannel) {
if res.Error != nil {
logger.Error(res.Error)
slog.Error("cannot download", "error", res.Error)
continue
}
if res.Filename == "" {
continue
}
err := PlaySound(args.Player, res.Filename)
if err != nil {
logger.Error(err)
slog.Error("cannot play filename", "filename", res.Filename, "error", err)
}
err = os.Remove(res.Filename)
if err != nil {
logger.Error(err)
slog.Error("cannot remove filename", "filename", res.Filename, "error", err)
}
}
}

func (args *Lingualeo) pronounce(ctx context.Context, urls <-chan string, wg *sync.WaitGroup) {
defer wg.Done()
for res := range channel.OrDone(ctx, urls) {
err := PlaySound(args.Player, res)
for url := range channel.OrDone(ctx, urls) {
err := PlaySound(args.Player, url)
if err != nil {
logger.Error(err)
slog.Error("cannot play url", "url", url, "error", err)
}
}
}
Expand All @@ -231,7 +236,7 @@ func (args *Lingualeo) AddToDictionary(ctx context.Context, resultsToAdd <-chan
ch := addWords(ctx, args.Translator, resultsToAdd)
for res := range ch {
if res.Error != nil {
logger.Error(res.Error)
slog.Error("cannot add word to dictionary", "word", res.Result.Word, "error", res.Error)
continue
}
res.Result.PrintAddedTranslation()
Expand All @@ -254,7 +259,7 @@ func (args *Lingualeo) Process(ctx context.Context, wg *sync.WaitGroup) (<-chan

for result := range args.translateWords(ctx) {
if result.Error != nil {
logger.Error(result.Error)
slog.Error("cannot translate word", "word", result.Result.Word, "error", result.Error)
continue
}
if args.Sound {
Expand Down Expand Up @@ -312,7 +317,7 @@ func (args *Lingualeo) TranslateWithReverseRussian(ctx context.Context, resultFu
var englishWords []string
for result := range channel.OrDone(ctx, args.translateToChan(ctx)) {
if err := resultFunc(result); err != nil {
logger.Error(err)
slog.Error("cannot translate word", "word", result.Word, "error", err)
}
for _, word := range result.Words {
if args.ReverseTranslate && isEnglishWord(word) {
Expand All @@ -324,7 +329,7 @@ func (args *Lingualeo) TranslateWithReverseRussian(ctx context.Context, resultFu
args.Words = englishWords
for result := range channel.OrDone(ctx, args.translateToChan(ctx)) {
if err := resultFunc(result); err != nil {
logger.Error(err)
slog.Error("cannot translate word", "word", result.Word, "error", err)
}
}
}
Expand Down

0 comments on commit 682cc3d

Please sign in to comment.