Skip to content

Commit

Permalink
Refactored logging
Browse files Browse the repository at this point in the history
  • Loading branch information
trezorg committed Apr 20, 2021
1 parent c5b40fb commit 00279a9
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
4 changes: 2 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ func request(method string, url string, client *http.Client, body []byte, query
defer func() {
err := resp.Body.Close()
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
}()
responseBody, err := readBody(resp)
if err != nil {
logger.Log.Error(err)
logger.Error(err)
return nil, err
}
if resp.StatusCode != 200 {
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func readBody(resp *http.Response) (*string, error) {
defer func() {
err := resp.Body.Close()
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
}()
data, err := ioutil.ReadAll(resp.Body)
Expand Down Expand Up @@ -56,11 +56,11 @@ func printTranslation(result Result) {
}
err := messages.Message(messages.RED, "Found %s word:\n", strTitle)
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
err = messages.Message(messages.GREEN, "['%s'] (%s)\n", result.GetWord(), strings.Join(result.GetTranscription(), ", "))
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
for _, word := range result.GetTranslate() {
_ = messages.Message(messages.YELLOW, "%s\n", word)
Expand All @@ -76,10 +76,10 @@ func printAddedTranslation(result Result) {
}
err := messages.Message(messages.RED, "%s word: ", strTitle)
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
err = messages.Message(messages.GREEN, "['%s']\n", result.GetWord())
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
}
4 changes: 2 additions & 2 deletions pkg/files/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (f *FileDownloader) DownloadFile() (string, error) {
defer func() {
err := fd.Close()
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
}()
resp, err := http.Get(f.URL)
Expand All @@ -79,7 +79,7 @@ func (f *FileDownloader) DownloadFile() (string, error) {
defer func() {
err := resp.Body.Close()
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
}()
if resp.StatusCode != http.StatusOK {
Expand Down
16 changes: 12 additions & 4 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/sirupsen/logrus"
)

var Log *logrus.Entry
var log *logrus.Entry
var onceLog sync.Once

type utcFormatter struct {
Expand All @@ -23,7 +23,7 @@ func initLogger(logLevel string, logPrettyPrint bool) {

level, err := logrus.ParseLevel(logLevel)
if err != nil {
logrus.Fatalf("Cannot parse Log level: %s", logLevel)
logrus.Fatalf("Cannot parse log level: %s", logLevel)
}

logrus.SetFormatter(utcFormatter{&logrus.JSONFormatter{
Expand All @@ -33,12 +33,20 @@ func initLogger(logLevel string, logPrettyPrint bool) {
logrus.SetReportCaller(true)
logrus.SetOutput(os.Stdout)
logrus.SetLevel(level)
Log = logrus.WithFields(logrus.Fields{"service": "lingualeo"})
log = logrus.WithFields(logrus.Fields{"service": "lingualeo"})
}

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

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

func InitLogger(logLevel string, logPrettyPrint bool) *logrus.Entry {
onceLog.Do(func() {
initLogger(logLevel, logPrettyPrint)
})
return Log
return log
}
20 changes: 10 additions & 10 deletions pkg/translator/linguleo.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func (args *Lingualeo) checkMediaPlayer() {
if len(args.Player) == 0 {
err := messages.Message(messages.RED, "Please set player parameter\n")
if err != nil {
logger.Log.Debug(err)
logger.Debug(err)
}
args.Sound = false
} else if !utils.IsCommandAvailable(args.Player) {
err := messages.Message(messages.RED, "Executable file %s is not available on your system\n", args.Player)
if err != nil {
logger.Log.Debug(err)
logger.Debug(err)
}
args.Sound = false
}
Expand Down Expand Up @@ -73,15 +73,15 @@ func (args *Lingualeo) translateWords(ctx context.Context) <-chan api.OpResult {
if res.Error != nil {
err := messages.Message(messages.RED, "%s\n", utils.Capitalize(res.Error.Error()))
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
continue
}
if len(res.Result.GetTranslate()) == 0 {
_ = messages.Message(messages.RED, "There are no translations for word: ")
err := messages.Message(messages.GREEN, "['%s']\n", res.Result.GetWord())
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
continue
}
Expand All @@ -107,19 +107,19 @@ func (args *Lingualeo) downloadAndPronounce(ctx context.Context, urls <-chan str
fileChannel := files.OrderedChannel(files.DownloadFiles(ctx, urls, downloader), len(urls))
for res := range files.OrFilesDone(ctx, fileChannel) {
if res.Error != nil {
logger.Log.Error(res.Error)
logger.Error(res.Error)
continue
}
if res.Filename == "" {
continue
}
err := utils.PlaySound(args.Player, res.Filename)
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
err = os.Remove(res.Filename)
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
}
}
Expand All @@ -129,7 +129,7 @@ func (args *Lingualeo) pronounce(ctx context.Context, urls <-chan string, wg *sy
for res := range channel.OrStringDone(ctx, urls) {
err := utils.PlaySound(args.Player, res)
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
}
}
Expand All @@ -148,7 +148,7 @@ func (args *Lingualeo) AddToDictionary(ctx context.Context, resultsToAdd <-chan
defer wg.Done()
for res := range args.API.AddWords(ctx, resultsToAdd) {
if res.Error != nil {
logger.Log.Error(res.Error)
logger.Error(res.Error)
continue
}
res.Result.PrintAddedTranslation()
Expand All @@ -173,7 +173,7 @@ func (args *Lingualeo) Process(ctx context.Context, wg *sync.WaitGroup) (<-chan

for result := range args.translateWords(ctx) {
if result.Error != nil {
logger.Log.Error(result.Error)
logger.Error(result.Error)
continue
}
if args.Sound {
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetJSON(body io.ReadCloser, target interface{}) error {
defer func() {
err := body.Close()
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
}()
return json.NewDecoder(body).Decode(target)
Expand All @@ -37,7 +37,7 @@ func ReadBody(resp *http.Response) (*string, error) {
defer func() {
err := resp.Body.Close()
if err != nil {
logger.Log.Error(err)
logger.Error(err)
}
}()
data, err := ioutil.ReadAll(resp.Body)
Expand Down

0 comments on commit 00279a9

Please sign in to comment.