Skip to content

Commit

Permalink
Use log/slog package
Browse files Browse the repository at this point in the history
  • Loading branch information
corny committed Sep 11, 2023
1 parent 287d7aa commit e9c660f
Show file tree
Hide file tree
Showing 16 changed files with 90 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, "info", "log-level", "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
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
Expand Down Expand Up @@ -121,6 +122,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand Down Expand Up @@ -159,6 +161,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down
Loading

0 comments on commit e9c660f

Please sign in to comment.