Implement a logger
This commit is contained in:
parent
1b2017b3ae
commit
d5d77bf522
4 changed files with 31 additions and 5 deletions
22
backend/logger/logger.go
Normal file
22
backend/logger/logger.go
Normal 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...)
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue