Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move logging to log package #15287

Merged
merged 1 commit into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions sdk/azcore/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"strings"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log"
)

func ExamplePipeline_Do() {
Expand Down Expand Up @@ -49,15 +50,15 @@ func ExampleRequest_SetBody() {
}

// false positive by linter
func ExampleLogSetClassifications() { //nolint:govet
func ExampleSetClassifications() { //nolint:govet
// only log HTTP requests and responses
azcore.LogSetClassifications(azcore.LogRequest, azcore.LogResponse)
azlog.SetClassifications(azlog.Request, azlog.Response)
}

// false positive by linter
func ExampleLogSetListener() { //nolint:govet
func ExampleSetListener() { //nolint:govet
// a simple logger that writes to stdout
azcore.LogSetListener(func(cls azcore.LogClassification, msg string) {
azlog.SetListener(func(cls azlog.Classification, msg string) {
fmt.Printf("%s: %s\n", cls, msg)
})
}
Expand Down
46 changes: 0 additions & 46 deletions sdk/azcore/log.go

This file was deleted.

47 changes: 47 additions & 0 deletions sdk/azcore/log/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// +build go1.13

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// Package log provides functionality for configuring logging facilities.
package log

import (
"github.com/Azure/azure-sdk-for-go/sdk/internal/log"
)

// Classification is used to group entries. Each group can be toggled on or off.
type Classification = log.Classification

const (
// Request entries contain information about HTTP requests.
// This includes information like the URL, query parameters, and headers.
Request = log.Request
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want RequestClassification; not just Request (too vague).


// Response entries contain information about HTTP responses.
// This includes information like the HTTP status code, headers, and request URL.
Response = log.Response

// RetryPolicy entries contain information specific to the retry policy in use.
RetryPolicy = log.RetryPolicy

// LongRunningOperation entries contain information specific to long-running operations.
// This includes information like polling location, operation state and sleep intervals.
LongRunningOperation = log.LongRunningOperation
)

// SetClassifications is used to control which classifications are written to
// the log. By default all log classifications are writen.
func SetClassifications(cls ...Classification) {
log.SetClassifications(cls...)
}

// SetListener will set the Logger to write to the specified Listener.
func SetListener(lst func(log.Classification, string)) {
log.SetListener(lst)
}

// for testing purposes
func resetClassifications() {
log.TestResetClassifications()
}
28 changes: 14 additions & 14 deletions sdk/azcore/log_test.go → sdk/azcore/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package azcore
package log

import (
"fmt"
Expand All @@ -15,11 +15,11 @@ import (

func TestLoggingDefault(t *testing.T) {
// ensure logging with nil listener doesn't fail
LogSetListener(nil)
SetListener(nil)
log.Write(log.Request, "this should work just fine")

testlog := map[LogClassification]string{}
LogSetListener(func(cls LogClassification, msg string) {
testlog := map[Classification]string{}
SetListener(func(cls Classification, msg string) {
testlog[cls] = msg
})
const req = "this is a request"
Expand All @@ -29,28 +29,28 @@ func TestLoggingDefault(t *testing.T) {
if l := len(testlog); l != 2 {
t.Fatalf("unexpected log entry count: %d", l)
}
if testlog[LogRequest] != req {
t.Fatalf("unexpected log request: %s", testlog[LogRequest])
if testlog[Request] != req {
t.Fatalf("unexpected log request: %s", testlog[Request])
}
if testlog[LogResponse] != fmt.Sprintf(resp, http.StatusOK) {
t.Fatalf("unexpected log response: %s", testlog[LogResponse])
if testlog[Response] != fmt.Sprintf(resp, http.StatusOK) {
t.Fatalf("unexpected log response: %s", testlog[Response])
}
}

func TestLoggingClassification(t *testing.T) {
testlog := map[LogClassification]string{}
LogSetListener(func(cls LogClassification, msg string) {
testlog := map[Classification]string{}
SetListener(func(cls Classification, msg string) {
testlog[cls] = msg
})
LogSetClassifications(LogRequest)
SetClassifications(Request)
defer resetClassifications()
log.Write(log.Response, "this shouldn't be in the log")
if s, ok := testlog[LogResponse]; ok {
if s, ok := testlog[Response]; ok {
t.Fatalf("unexpected log entry %s", s)
}
const req = "this is a request"
log.Write(log.Request, req)
if testlog[LogRequest] != req {
t.Fatalf("unexpected log entry: %s", testlog[LogRequest])
if testlog[Request] != req {
t.Fatalf("unexpected log entry: %s", testlog[Request])
}
}
21 changes: 11 additions & 10 deletions sdk/azcore/policy_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import (
"strings"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/internal/log"
"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
)

func TestPolicyLoggingSuccess(t *testing.T) {
log := map[LogClassification]string{}
LogSetListener(func(cls LogClassification, s string) {
log[cls] = s
rawlog := map[log.Classification]string{}
log.SetListener(func(cls log.Classification, s string) {
rawlog[cls] = s
})
srv, close := mock.NewServer()
defer close()
Expand All @@ -40,7 +41,7 @@ func TestPolicyLoggingSuccess(t *testing.T) {
if resp.StatusCode != http.StatusOK {
t.Fatalf("unexpected status code: %d", resp.StatusCode)
}
if logReq, ok := log[LogRequest]; ok {
if logReq, ok := rawlog[log.Request]; ok {
// Request ==> OUTGOING REQUEST (Try=1)
// GET http://127.0.0.1:49475?one=fish&sig=REDACTED
// (no headers)
Expand All @@ -50,7 +51,7 @@ func TestPolicyLoggingSuccess(t *testing.T) {
} else {
t.Fatal("missing LogRequest")
}
if logResp, ok := log[LogResponse]; ok {
if logResp, ok := rawlog[log.Response]; ok {
// Response ==> REQUEST/RESPONSE (Try=1/1.0034ms, OpTime=1.0034ms) -- RESPONSE SUCCESSFULLY RECEIVED
// GET http://127.0.0.1:49475?one=fish&sig=REDACTED
// (no headers)
Expand All @@ -67,9 +68,9 @@ func TestPolicyLoggingSuccess(t *testing.T) {
}

func TestPolicyLoggingError(t *testing.T) {
log := map[LogClassification]string{}
LogSetListener(func(cls LogClassification, s string) {
log[cls] = s
rawlog := map[log.Classification]string{}
log.SetListener(func(cls log.Classification, s string) {
rawlog[cls] = s
})
srv, close := mock.NewServer()
defer close()
Expand All @@ -88,7 +89,7 @@ func TestPolicyLoggingError(t *testing.T) {
if resp != nil {
t.Fatal("unexpected respose")
}
if logReq, ok := log[LogRequest]; ok {
if logReq, ok := rawlog[log.Request]; ok {
// Request ==> OUTGOING REQUEST (Try=1)
// GET http://127.0.0.1:50057
// Authorization: REDACTED
Expand All @@ -99,7 +100,7 @@ func TestPolicyLoggingError(t *testing.T) {
} else {
t.Fatal("missing LogRequest")
}
if logResponse, ok := log[LogResponse]; ok {
if logResponse, ok := rawlog[log.Response]; ok {
// Response ==> REQUEST/RESPONSE (Try=1/0s, OpTime=0s) -- REQUEST ERROR
// GET http://127.0.0.1:50057
// Authorization: REDACTED
Expand Down