From 73b2b41e777614d7c372b9d92a90b5fbb68d05c0 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sat, 10 Sep 2022 18:28:19 +0800 Subject: Replace all logger usages to custom logging.Logger. --- bilibili/client.go | 36 ++++++++++++++---------------------- bilibili/request.go | 12 ++++++------ bilibili/request_test.go | 4 +++- bilibili/streaming.go | 10 +++++----- 4 files changed, 28 insertions(+), 34 deletions(-) (limited to 'bilibili') diff --git a/bilibili/client.go b/bilibili/client.go index 8097115..f72432a 100644 --- a/bilibili/client.go +++ b/bilibili/client.go @@ -5,11 +5,10 @@ This struct is a facade of all necessary Bilibili HTTP API wrappers. package bilibili import ( + "bilibili-livestream-archiver/logging" "context" - "log" "net" "net/http" - "os" ) const ( @@ -21,19 +20,12 @@ const ( type Bilibili struct { userAgent string http *http.Client - loggerCommon - ctx context.Context - netTypes []IpNetType + ctx context.Context + netTypes []IpNetType + logger logging.Logger } -func NewBilibiliWithContext(ctx context.Context, netTypes []IpNetType) Bilibili { - logger := loggerCommon{ - debug: log.New(os.Stderr, "DEBUG: ", log.Ldate|log.Ltime|log.Lshortfile), - info: log.New(os.Stderr, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile), - warn: log.New(os.Stderr, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile), - error: log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile), - } - +func NewBilibiliWithContext(ctx context.Context, netTypes []IpNetType, logger logging.Logger) Bilibili { var nets []IpNetType nets = append(nets, netTypes...) if len(nets) == 0 { @@ -47,19 +39,19 @@ func NewBilibiliWithContext(ctx context.Context, netTypes []IpNetType) Bilibili transport.DialContext, _ = np.NextNetworkType(dialer) return Bilibili{ - loggerCommon: logger, - userAgent: kUserAgent, - http: http.DefaultClient, - ctx: ctx, - netTypes: nets, + logger: logger, + userAgent: kUserAgent, + http: http.DefaultClient, + ctx: ctx, + netTypes: nets, } } -func NewBilibiliWithNetType(netTypes []IpNetType) Bilibili { +func NewBilibiliWithNetType(netTypes []IpNetType, logger logging.Logger) Bilibili { ctx := context.Background() - return NewBilibiliWithContext(ctx, netTypes) + return NewBilibiliWithContext(ctx, netTypes, logger) } -func NewBilibili() Bilibili { - return NewBilibiliWithNetType(nil) +func NewBilibili(logger logging.Logger) Bilibili { + return NewBilibiliWithNetType(nil, logger) } diff --git a/bilibili/request.go b/bilibili/request.go index f9e2088..7de35d3 100644 --- a/bilibili/request.go +++ b/bilibili/request.go @@ -18,7 +18,7 @@ func (b Bilibili) newRequest( ) (req *http.Request, err error) { req, err = http.NewRequestWithContext(b.ctx, method, url, body) if err != nil { - b.error.Printf("Cannot create HTTP request instance: %v. Method: %v, URL: %v", err, method, url) + b.logger.Error("Cannot create HTTP request instance: %v. Method: %v, URL: %v", err, method, url) return } req.Header.Set("User-Agent", b.userAgent) @@ -48,24 +48,24 @@ func callGet[T BaseResponse[V], V any](b Bilibili, url string) (resp T, err erro err = validateHttpStatus(r) if err != nil { - b.error.Printf("%v", err) + b.logger.Error("%v", err) return } data, err := io.ReadAll(r.Body) if err != nil { - b.error.Printf("Error when reading HTTP response on API %v: %v", url, err) + b.logger.Error("Error when reading HTTP response on API %v: %v", url, err) return } err = json.Unmarshal(data, &resp) if err != nil { - b.error.Printf("Invalid JSON body of HTTP response on API %v: %v. Text: \"%v\"", + b.logger.Error("Invalid JSON body of HTTP response on API %v: %v. Text: \"%v\"", url, err, string(data)) return } - b.debug.Printf("HTTP %v, len: %v bytes, url: %v", r.StatusCode, len(data), url) + b.logger.Debug("HTTP %v, len: %v bytes, url: %v", r.StatusCode, len(data), url) return } @@ -84,7 +84,7 @@ func (b Bilibili) Do(req *http.Request) (resp *http.Response, err error) { isAddrErr := common.IsErrorOfType(err, &net.AddrError{}) if err == nil || !isOpErr || !isAddrErr { // return the first success request - b.loggerCommon.info.Printf("Request success with network %v.", typeName) + b.logger.Info("Request success with network %v.", typeName) return } } diff --git a/bilibili/request_test.go b/bilibili/request_test.go index 68e5c7a..58b7127 100644 --- a/bilibili/request_test.go +++ b/bilibili/request_test.go @@ -1,12 +1,14 @@ package bilibili import ( + "bilibili-livestream-archiver/logging" + "log" "testing" ) func Test_callGet(t *testing.T) { // an always-fail request should not panic - bi := NewBilibili() + bi := NewBilibili(logging.NewWrappedLogger(log.Default(), "main")) _, err := callGet[BaseResponse[struct{}]](bi, "https://256.256.256.256") if err == nil { t.Fatalf("the artificial request should fail, but it haven't") diff --git a/bilibili/streaming.go b/bilibili/streaming.go index 9a26c2d..7ad852c 100644 --- a/bilibili/streaming.go +++ b/bilibili/streaming.go @@ -27,7 +27,7 @@ func (b Bilibili) CopyLiveStream( r, err := b.newGet(url) if err != nil { - b.error.Printf("Cannot create HTTP GET instance on %v: %v", url, err) + b.logger.Error("Cannot create HTTP GET instance on %v: %v", url, err) return err } @@ -36,7 +36,7 @@ func (b Bilibili) CopyLiveStream( resp, err := b.Do(r) if err != nil { - b.error.Printf("Cannot make HTTP GET request on %v: %v\n", url, err) + b.logger.Error("Cannot make HTTP GET request on %v: %v\n", url, err) return } @@ -56,13 +56,13 @@ func (b Bilibili) CopyLiveStream( n, err := common.CopyToFileWithBuffer(ctx, out, resp.Body, buffer, readChunkSize, false) if err != nil && !errors.Is(err, context.Canceled) { - b.error.Printf("Stream copying was interrupted unexpectedly: %v", err) + b.logger.Error("Stream copying was interrupted unexpectedly: %v", err) } if err == nil { - b.info.Printf("The live is ended. (room %v)", roomId) + b.logger.Info("The live is ended. (room %v)", roomId) } - b.info.Printf("Total downloaded: %v", common.PrettyBytes(uint64(n))) + b.logger.Info("Total downloaded: %v", common.PrettyBytes(uint64(n))) return err } -- cgit v1.2.3