Skip to content

Commit

Permalink
Use log/slog package
Browse files Browse the repository at this point in the history
Minimum Go version required: 1.21
  • Loading branch information
corny committed Sep 16, 2023
1 parent 31bf9e2 commit 8eabf5f
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 68 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-22.04]
go-version: [1.21.x, 1.20.x]
go-version: [1.21.x, 1.x]
runs-on: ${{ matrix.platform }}
name: Linters (Static Analysis) for Go
steps:
Expand All @@ -27,7 +27,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-22.04]
go-version: [1.21.x, 1.20.x]
go-version: [1.21.x, 1.x]
runs-on: ${{ matrix.platform }}
name: integration tests
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/schedule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
platform: [ubuntu-22.04]
go-version: [1.20.x]
go-version: [1.21.x]
runs-on: ${{ matrix.platform }}
name: integration tests
env:
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FILES_TO_FMT ?= $(shell find . -path ./vendor -prune -o -name '*.go' -print)
LOGLEVEL ?= debug

## help: Show makefile commands
.PHONY: help
Expand Down Expand Up @@ -38,14 +39,14 @@ format:
## test-unit: Run all Youtube Go unit tests
.PHONY: test-unit
test-unit:
go test -v -cover ./...
LOGLEVEL=${LOGLEVEL} go test -v -cover ./...

## test-integration: Run all Youtube Go integration tests
.PHONY: test-integration
test-integration:
mkdir -p output
rm -f output/*
ARTIFACTS=output go test -race -covermode=atomic -coverprofile=coverage.out -tags=integration ./...
LOGLEVEL=${LOGLEVEL} ARTIFACTS=output go test -v -race -covermode=atomic -coverprofile=coverage.out -tags=integration ./...

.PHONY: coverage.out
coverage.out:
Expand Down
10 changes: 6 additions & 4 deletions artifacts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package youtube

import (
"log"
"log/slog"
"os"
"path/filepath"
)
Expand All @@ -13,15 +13,17 @@ func writeArtifact(name string, content []byte) {
// Ensure folder exists
err := os.MkdirAll(artifactsFolder, os.ModePerm)
if err != nil {
log.Printf("unable to create artifacts folder %s: %s", artifactsFolder, err)
slog.Error("unable to create artifacts folder", "path", artifactsFolder, "error", err)
return
}

path := filepath.Join(artifactsFolder, name)
err = os.WriteFile(path, content, 0600)

log := slog.With("path", path)
if err != nil {
log.Printf("unable to write artifact %s: %v", path, err)
log.Error("unable to write artifact", "error", err)
} else {
log.Println("artifact created:", path)
log.Debug("artifact created")
}
}
18 changes: 8 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"errors"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"net/url"
"strconv"
"sync/atomic"

"log/slog"
)

const (
Expand All @@ -32,9 +33,6 @@ var DefaultClient = AndroidClient

// Client offers methods to download video metadata and video streams.
type Client struct {
// Debug enables debugging output through log package
Debug bool

// HTTPClient can be used to set a custom HTTP client.
// If not set, http.DefaultClient will be used
HTTPClient *http.Client
Expand Down Expand Up @@ -484,10 +482,6 @@ func (c *Client) httpDo(req *http.Request) (*http.Response, error) {
client = http.DefaultClient
}

if c.Debug {
log.Println(req.Method, req.URL)
}

req.Header.Set("User-Agent", c.client.userAgent)
req.Header.Set("Origin", "https://youtube.com")
req.Header.Set("Sec-Fetch-Mode", "navigate")
Expand All @@ -505,8 +499,12 @@ func (c *Client) httpDo(req *http.Request) (*http.Response, error) {

res, err := client.Do(req)

if c.Debug && res != nil {
log.Println(res.Status)
log := slog.With("method", req.Method, "url", req.URL)

if err != nil {
log.Debug("HTTP request failed", "error", err)
} else {
log.Debug("HTTP request succeeded", "status", res.Status)
}

return res, err
Expand Down
7 changes: 3 additions & 4 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ import (
"golang.org/x/net/context"
)

var testClient = Client{Debug: true}
var testWebClient = Client{Debug: true, client: &WebClient}

const (
dwlURL string = "https://www.youtube.com/watch?v=rFejpH_tAHM"
streamURL string = "https://www.youtube.com/watch?v=a9LDPn-MO4I"
errURL string = "https://www.youtube.com/watch?v=I8oGsuQ"
)

var testClient = Client{}
var testWebClient = Client{client: &WebClient}

func TestParseVideo(t *testing.T) {
video, err := testClient.GetVideo(dwlURL)
assert.NoError(t, err)
Expand Down Expand Up @@ -162,7 +162,6 @@ func TestGetStream(t *testing.T) {

// Create testclient to enforce re-using of routines
testClient := Client{
Debug: true,
MaxRoutines: 10,
ChunkSize: int64(expectedSize) / 11,
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/youtubedr/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/tls"
"errors"
"fmt"
"log"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -54,8 +53,10 @@ func getDownloader() *ytdl.Downloader {
}).DialContext,
}

youtube.SetLogLevel(logLevel)

if insecureSkipVerify {
log.Println("Skip server certificate verification")
youtube.Logger.Info("Skip server certificate verification")
httpTransport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
Expand All @@ -64,7 +65,6 @@ func getDownloader() *ytdl.Downloader {
downloader = &ytdl.Downloader{
OutputDir: outputDir,
}
downloader.Client.Debug = verbose
downloader.HTTPClient = &http.Client{Transport: httpTransport}

return downloader
Expand Down
13 changes: 3 additions & 10 deletions cmd/youtubedr/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package main

import (
"fmt"
"io"
"log"
"os"

homedir "github.com/mitchellh/go-homedir"
Expand All @@ -12,8 +10,8 @@ import (
)

var (
cfgFile string
verbose bool
cfgFile string
logLevel string
)

// rootCmd represents the base command when called without any subcommands
Expand All @@ -31,18 +29,13 @@ Use the HTTP_PROXY environment variable to set a HTTP or SOCSK5 proxy. The proxy

func init() {
cobra.OnInitialize(initConfig)
cobra.OnInitialize(func() {
if !verbose {
log.SetOutput(io.Discard)
}
})

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.youtubedr.yaml)")
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable verbose output")
rootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Set log level (error/warn/info/debug)")
rootCmd.PersistentFlags().BoolVar(&insecureSkipVerify, "insecure", false, "Skip TLS server certificate verification")
}

Expand Down
8 changes: 4 additions & 4 deletions decipher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"log"
"net/url"
"regexp"
"strconv"
Expand Down Expand Up @@ -76,17 +75,18 @@ func (c *Client) unThrottle(ctx context.Context, videoID string, urlString strin
func (c *Client) decryptNParam(config playerConfig, query url.Values) (url.Values, error) {
// decrypt n-parameter
nSig := query.Get("v")
log := Logger.With("n", nSig)

if nSig != "" {
nDecoded, err := config.decodeNsig(nSig)
if err != nil {
return nil, fmt.Errorf("unable to decode nSig: %w", err)
}
query.Set("v", nDecoded)
log = log.With("decoded", nDecoded)
}

if c.Debug {
log.Printf("[nParam] n: %s; nDecoded: %s\nQuery: %v\n", nSig, query.Get("v"), query)
}
log.Debug("nParam")

return query, nil
}
Expand Down
31 changes: 18 additions & 13 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -38,7 +37,12 @@ func (dl *Downloader) getOutputFile(v *youtube.Video, format *youtube.Format, ou

// Download : Starting download video by arguments.
func (dl *Downloader) Download(ctx context.Context, v *youtube.Video, format *youtube.Format, outputFile string) error {
dl.logf("Video '%s' - Quality '%s' - Codec '%s'", v.Title, format.QualityLabel, format.MimeType)
youtube.Logger.Info(
"Downloading video",
"id", v.ID,
"quality", format.Quality,
"mimeType", format.MimeType,
)
destFile, err := dl.getOutputFile(v, format, outputFile)
if err != nil {
return err
Expand All @@ -51,7 +55,6 @@ func (dl *Downloader) Download(ctx context.Context, v *youtube.Video, format *yo
}
defer out.Close()

dl.logf("Download to file=%s", destFile)
return dl.videoDLWorker(ctx, out, v, format)
}

Expand All @@ -62,7 +65,15 @@ func (dl *Downloader) DownloadComposite(ctx context.Context, outputFile string,
return err1
}

dl.logf("Video '%s' - Quality '%s' - Video Codec '%s' - Audio Codec '%s'", v.Title, videoFormat.QualityLabel, videoFormat.MimeType, audioFormat.MimeType)
log := youtube.Logger.With("id", v.ID)

log.Info(
"Downloading composite video",
"videoQuality", videoFormat.QualityLabel,
"videoMimeType", videoFormat.MimeType,
"audioMimeType", audioFormat.MimeType,
)

destFile, err := dl.getOutputFile(v, videoFormat, outputFile)
if err != nil {
return err
Expand All @@ -83,13 +94,13 @@ func (dl *Downloader) DownloadComposite(ctx context.Context, outputFile string,
}
defer os.Remove(audioFile.Name())

dl.logf("Downloading video file...")
log.Debug("Downloading video file...")
err = dl.videoDLWorker(ctx, videoFile, v, videoFormat)
if err != nil {
return err
}

dl.logf("Downloading audio file...")
log.Debug("Downloading audio file...")
err = dl.videoDLWorker(ctx, audioFile, v, audioFormat)
if err != nil {
return err
Expand All @@ -106,7 +117,7 @@ func (dl *Downloader) DownloadComposite(ctx context.Context, outputFile string,
)
ffmpegVersionCmd.Stderr = os.Stderr
ffmpegVersionCmd.Stdout = os.Stdout
dl.logf("merging video and audio to %s", destFile)
log.Info("merging video and audio", "output", destFile)

return ffmpegVersionCmd.Run()
}
Expand Down Expand Up @@ -184,9 +195,3 @@ func (dl *Downloader) videoDLWorker(ctx context.Context, out *os.File, video *yo
progress.Wait()
return nil
}

func (dl *Downloader) logf(format string, v ...interface{}) {
if dl.Debug {
log.Printf(format, v...)
}
}
5 changes: 2 additions & 3 deletions downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package downloader

import (
"context"
"log"
"os"
"testing"
"time"
Expand All @@ -15,15 +14,15 @@ import (

var testDownloader = func() (dl Downloader) {
dl.OutputDir = "download_test"
dl.Debug = true

return
}()

func TestMain(m *testing.M) {
exitCode := m.Run()
// the following code doesn't work under debugger, please delete download files manually
if err := os.RemoveAll(testDownloader.OutputDir); err != nil {
log.Fatal(err.Error())
panic(err)
}
os.Exit(exitCode)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kkdai/youtube/v2

go 1.20
go 1.21

require (
github.com/bitly/go-simplejson v0.5.1
Expand Down
Loading

0 comments on commit 8eabf5f

Please sign in to comment.