From d5d77bf522c4d372873ff5542d7504e8bcc0472f Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Tue, 7 Dec 2021 14:11:10 +0100 Subject: [PATCH] Implement a logger --- backend/logger/logger.go | 22 ++++++++++++++++++++++ backend/main.go | 1 - backend/main_test.go | 5 +++++ backend/youtube/youtube-dl/youtube-dl.go | 8 ++++---- 4 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 backend/logger/logger.go diff --git a/backend/logger/logger.go b/backend/logger/logger.go new file mode 100644 index 0000000..f41c8ef --- /dev/null +++ b/backend/logger/logger.go @@ -0,0 +1,22 @@ +package logger + +import "log" + +const ( + Disabled = 0 + InfoLevel + WarningLevel + DebugLevel +) + +var loggerLevel int + +func SetLevel(level int) { + loggerLevel = level +} + +func Info(format string, v ...interface{}) { + if loggerLevel == InfoLevel { + log.Println(v...) + } +} diff --git a/backend/main.go b/backend/main.go index 9477cea..1f07889 100644 --- a/backend/main.go +++ b/backend/main.go @@ -54,7 +54,6 @@ func getTask(c *gin.Context, id string) error { func data(c *gin.Context, id string) error { c.Writer.Header().Set("Content-Disposition", "attachment; filename="+strconv.Quote(tasks[id].Filename)) c.Writer.Header().Set("Content-Type", "application/octet-stream") - log.Println(id, tasks[id]) http.ServeFile(c.Writer, c.Request, filepath.Join("data", id+filepath.Ext(tasks[id].Filename))) return nil } diff --git a/backend/main_test.go b/backend/main_test.go index 5b762e0..4639b7c 100644 --- a/backend/main_test.go +++ b/backend/main_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "git.andreafazzi.eu/andrea/youtube-dl-service/logger" "git.andreafazzi.eu/andrea/youtube-dl-service/task" "git.andreafazzi.eu/andrea/youtube-dl-service/youtube" "github.com/gin-gonic/gin" @@ -20,6 +21,10 @@ type testSuite struct { } func TestRunner(t *testing.T) { + + gin.SetMode(gin.ReleaseMode) + logger.SetLevel(logger.Disabled) + prettytest.Run( t, new(testSuite), diff --git a/backend/youtube/youtube-dl/youtube-dl.go b/backend/youtube/youtube-dl/youtube-dl.go index c18bb2a..ef093bc 100644 --- a/backend/youtube/youtube-dl/youtube-dl.go +++ b/backend/youtube/youtube-dl/youtube-dl.go @@ -4,10 +4,10 @@ import ( "context" "encoding/json" "fmt" - "log" "os/exec" "path/filepath" + "git.andreafazzi.eu/andrea/youtube-dl-service/logger" "git.andreafazzi.eu/andrea/youtube-dl-service/task" "git.andreafazzi.eu/andrea/youtube-dl-service/youtube" lib "github.com/kkdai/youtube/v2" @@ -32,7 +32,7 @@ func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tas Status: task.StatusDownloading, } - log.Printf("Download of video ID %s STARTED.", video.ID) + logger.Info("Download of video ID %s STARTED.", video.ID) cmd := exec.CommandContext(context.Background(), d.CommandName, "-o", filepath.Join("data/", video.ID+filepath.Ext(video.Filename)), "--newline", d.Url) stdout, err := cmd.StdoutPipe() if err != nil { @@ -44,13 +44,13 @@ func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tas for { tmp := make([]byte, 1024) _, err := stdout.Read(tmp) - fmt.Print(string(tmp)) + logger.Info(string(tmp)) if err != nil { break } } - log.Printf("Download of video ID %s COMPLETED.", video.ID) + logger.Info(fmt.Sprintf("Download of video ID %s COMPLETED.", video.ID)) tasks[video.ID] = &task.Task{ ID: video.ID, Status: task.StatusCompleted,