Implement a logger

This commit is contained in:
Andrea Fazzi 2021-12-07 14:11:10 +01:00
parent 1b2017b3ae
commit d5d77bf522
4 changed files with 31 additions and 5 deletions

22
backend/logger/logger.go Normal file
View file

@ -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...)
}
}

View file

@ -54,7 +54,6 @@ func getTask(c *gin.Context, id string) error {
func data(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-Disposition", "attachment; filename="+strconv.Quote(tasks[id].Filename))
c.Writer.Header().Set("Content-Type", "application/octet-stream") 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))) http.ServeFile(c.Writer, c.Request, filepath.Join("data", id+filepath.Ext(tasks[id].Filename)))
return nil return nil
} }

View file

@ -8,6 +8,7 @@ import (
"strings" "strings"
"testing" "testing"
"git.andreafazzi.eu/andrea/youtube-dl-service/logger"
"git.andreafazzi.eu/andrea/youtube-dl-service/task" "git.andreafazzi.eu/andrea/youtube-dl-service/task"
"git.andreafazzi.eu/andrea/youtube-dl-service/youtube" "git.andreafazzi.eu/andrea/youtube-dl-service/youtube"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -20,6 +21,10 @@ type testSuite struct {
} }
func TestRunner(t *testing.T) { func TestRunner(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
logger.SetLevel(logger.Disabled)
prettytest.Run( prettytest.Run(
t, t,
new(testSuite), new(testSuite),

View file

@ -4,10 +4,10 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os/exec" "os/exec"
"path/filepath" "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/task"
"git.andreafazzi.eu/andrea/youtube-dl-service/youtube" "git.andreafazzi.eu/andrea/youtube-dl-service/youtube"
lib "github.com/kkdai/youtube/v2" lib "github.com/kkdai/youtube/v2"
@ -32,7 +32,7 @@ func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tas
Status: task.StatusDownloading, 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) cmd := exec.CommandContext(context.Background(), d.CommandName, "-o", filepath.Join("data/", video.ID+filepath.Ext(video.Filename)), "--newline", d.Url)
stdout, err := cmd.StdoutPipe() stdout, err := cmd.StdoutPipe()
if err != nil { if err != nil {
@ -44,13 +44,13 @@ func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tas
for { for {
tmp := make([]byte, 1024) tmp := make([]byte, 1024)
_, err := stdout.Read(tmp) _, err := stdout.Read(tmp)
fmt.Print(string(tmp)) logger.Info(string(tmp))
if err != nil { if err != nil {
break 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{ tasks[video.ID] = &task.Task{
ID: video.ID, ID: video.ID,
Status: task.StatusCompleted, Status: task.StatusCompleted,