Improve logging

This commit is contained in:
Andrea Fazzi 2021-12-10 12:06:04 +01:00
commit d333a21175
7 changed files with 24 additions and 20 deletions

View file

@ -1,4 +1,4 @@
package youtube package downloader
import ( import (
"time" "time"

View file

@ -7,9 +7,9 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"git.andreafazzi.eu/andrea/youtube-dl-service/downloader"
"git.andreafazzi.eu/andrea/youtube-dl-service/logger" "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"
lib "github.com/kkdai/youtube/v2" lib "github.com/kkdai/youtube/v2"
) )
@ -22,17 +22,17 @@ func NewYoutubeDlDownloader(url string, commandName string) *YoutubeDlDownloader
return &YoutubeDlDownloader{url, commandName} return &YoutubeDlDownloader{url, commandName}
} }
func (d *YoutubeDlDownloader) GetVideo() (*youtube.Video, error) { func (d *YoutubeDlDownloader) GetVideo() (*downloader.Video, error) {
return d.getVideoJson(d.Url) return d.getVideoJson(d.Url)
} }
func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tasks) { func (d *YoutubeDlDownloader) StartDownload(video *downloader.Video, tasks task.Tasks) {
tasks[video.ID] = &task.Task{ tasks[video.ID] = &task.Task{
ID: video.ID, ID: video.ID,
Status: task.StatusDownloading, Status: task.StatusDownloading,
} }
logger.Info("Download of video ID %s STARTED.", video.ID) logger.Info(fmt.Sprintf("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 {
@ -62,13 +62,13 @@ func (d *YoutubeDlDownloader) ExtractVideoID() (string, error) {
return lib.ExtractVideoID(d.Url) return lib.ExtractVideoID(d.Url)
} }
func (d *YoutubeDlDownloader) getVideoJson(url string) (*youtube.Video, error) { func (d *YoutubeDlDownloader) getVideoJson(url string) (*downloader.Video, error) {
cmd := exec.CommandContext(context.Background(), d.CommandName, "-j", url) cmd := exec.CommandContext(context.Background(), d.CommandName, "-j", url)
output, err := cmd.CombinedOutput() output, err := cmd.CombinedOutput()
if err != nil { if err != nil {
return nil, fmt.Errorf("Command terminated with %w: %s", err, output) return nil, fmt.Errorf("Command terminated with %w: %s", err, output)
} }
result := new(youtube.Video) result := new(downloader.Video)
err = json.Unmarshal(output, &result) err = json.Unmarshal(output, &result)
if err != nil { if err != nil {
return nil, err return nil, err

View file

@ -3,19 +3,22 @@ package logger
import "log" import "log"
const ( const (
Disabled = 0 Disabled = iota
InfoLevel InfoLevel
WarningLevel WarningLevel
DebugLevel DebugLevel
) )
var loggerLevel int var loggerLevel int = InfoLevel
func SetLevel(level int) { func SetLevel(level int) {
loggerLevel = level loggerLevel = level
} }
func Info(format string, v ...interface{}) { func Info(v ...interface{}) {
if loggerLevel == Disabled {
return
}
if loggerLevel == InfoLevel { if loggerLevel == InfoLevel {
log.Println(v...) log.Println(v...)
} }

View file

@ -7,9 +7,9 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"git.andreafazzi.eu/andrea/youtube-dl-service/downloader"
youtube_dl "git.andreafazzi.eu/andrea/youtube-dl-service/downloader/youtube-dl"
"git.andreafazzi.eu/andrea/youtube-dl-service/task" "git.andreafazzi.eu/andrea/youtube-dl-service/task"
"git.andreafazzi.eu/andrea/youtube-dl-service/youtube"
youtube_dl "git.andreafazzi.eu/andrea/youtube-dl-service/youtube/youtube-dl"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -21,7 +21,7 @@ func init() {
tasks = make(task.Tasks, 0) tasks = make(task.Tasks, 0)
} }
func download(c *gin.Context, downloader youtube.Downloader) error { func download(c *gin.Context, downloader downloader.Downloader) error {
video, err := downloader.GetVideo() video, err := downloader.GetVideo()
if err != nil { if err != nil {
return err return err

View file

@ -8,9 +8,9 @@ import (
"strings" "strings"
"testing" "testing"
"git.andreafazzi.eu/andrea/youtube-dl-service/downloader"
"git.andreafazzi.eu/andrea/youtube-dl-service/logger" "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"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/remogatto/prettytest" "github.com/remogatto/prettytest"
) )
@ -21,16 +21,17 @@ 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),
) )
} }
func (t *testSuite) BeforeAll() {
gin.SetMode(gin.ReleaseMode)
logger.SetLevel(logger.Disabled)
}
// Test the creation of a new task. A new task is created with a POST // Test the creation of a new task. A new task is created with a POST
// request to the endpoint. // request to the endpoint.
func (t *testSuite) TestCreateTask() { func (t *testSuite) TestCreateTask() {
@ -73,7 +74,7 @@ func (t *testSuite) TestGetTask() {
t.Equal("AVIBLFl28vo", task.ID) t.Equal("AVIBLFl28vo", task.ID)
} }
func postTask(ytUrl string) (*youtube.Video, error) { func postTask(ytUrl string) (*downloader.Video, error) {
// Set form values // Set form values
form := url.Values{} form := url.Values{}
form.Add("url", ytUrl) form.Add("url", ytUrl)
@ -96,7 +97,7 @@ func postTask(ytUrl string) (*youtube.Video, error) {
createTask(c) createTask(c)
// Read the response and assert it. // Read the response and assert it.
video := new(youtube.Video) video := new(downloader.Video)
if err := json.Unmarshal(rr.Body.Bytes(), &video); err != nil { if err := json.Unmarshal(rr.Body.Bytes(), &video); err != nil {
return nil, err return nil, err