-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from bstasyszyn/130
chore: Use structured logging
- Loading branch information
Showing
18 changed files
with
490 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package log | ||
|
||
import "go.uber.org/zap" | ||
|
||
// WriteResponseBodyError outputs a 'write response body' error log to the given logger. | ||
func WriteResponseBodyError(log *Log, err error) { | ||
log.WithOptions(zap.AddCallerSkip(1)).Error("Error writing response body", WithError(err)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package log | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCommonLogs(t *testing.T) { | ||
const module = "test_module" | ||
|
||
t.Run("WriteResponseBodyError", func(t *testing.T) { | ||
stdErr := newMockWriter() | ||
|
||
logger := New(module, | ||
WithStdErr(stdErr), | ||
WithFields(WithServiceName("myservice")), | ||
) | ||
|
||
WriteResponseBodyError(logger, errors.New("response body error")) | ||
|
||
require.Contains(t, stdErr.Buffer.String(), `Error writing response body`) | ||
require.Contains(t, stdErr.Buffer.String(), `"service": "myservice"`) | ||
require.Contains(t, stdErr.Buffer.String(), `"error": "response body error"`) | ||
require.Contains(t, stdErr.Buffer.String(), "log/common_test.go") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
Copyright SecureKey Technologies Inc. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package log | ||
|
||
import ( | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// Log Fields. | ||
const ( | ||
FieldServiceName = "service" | ||
FieldSize = "size" | ||
FieldAddress = "address" | ||
FieldBackoff = "backoff" | ||
FieldServiceEndpoint = "serviceEndpoint" | ||
FieldTreeID = "treeID" | ||
FieldLeaf = "leaf" | ||
FieldStore = "store" | ||
FieldCommand = "command" | ||
FieldVerifiableCredential = "vc" | ||
FieldSignature = "signature" | ||
FieldTimestamp = "timestamp" | ||
FieldPublicKey = "publicKey" | ||
) | ||
|
||
// WithError sets the error field. | ||
func WithError(err error) zap.Field { | ||
return zap.Error(err) | ||
} | ||
|
||
// WithServiceName sets the service field. | ||
func WithServiceName(value string) zap.Field { | ||
return zap.String(FieldServiceName, value) | ||
} | ||
|
||
// WithSize sets the size field. | ||
func WithSize(value int) zap.Field { | ||
return zap.Int(FieldSize, value) | ||
} | ||
|
||
// WithAddress sets the address field. | ||
func WithAddress(value string) zap.Field { | ||
return zap.String(FieldAddress, value) | ||
} | ||
|
||
// WithBackoff sets the backoff field. | ||
func WithBackoff(value time.Duration) zap.Field { | ||
return zap.Duration(FieldBackoff, value) | ||
} | ||
|
||
// WithServiceEndpoint sets the service-endpoint field. | ||
func WithServiceEndpoint(value string) zap.Field { | ||
return zap.String(FieldServiceEndpoint, value) | ||
} | ||
|
||
// WithTreeID sets the service-endpoint field. | ||
func WithTreeID(value int64) zap.Field { | ||
return zap.Int64(FieldTreeID, value) | ||
} | ||
|
||
// WithLeaf sets the leaf field. | ||
func WithLeaf(value interface{}) zap.Field { | ||
return zap.Inline(NewObjectMarshaller(FieldLeaf, value)) | ||
} | ||
|
||
// WithStore sets the store field. | ||
func WithStore(value string) zap.Field { | ||
return zap.String(FieldStore, value) | ||
} | ||
|
||
// WithCommand sets the command field. | ||
func WithCommand(value string) zap.Field { | ||
return zap.String(FieldCommand, value) | ||
} | ||
|
||
// WithVerifiableCredential sets the vc field. | ||
func WithVerifiableCredential(value []byte) zap.Field { | ||
return zap.String(FieldVerifiableCredential, string(value)) | ||
} | ||
|
||
// WithSignature sets the signature field. | ||
func WithSignature(value []byte) zap.Field { | ||
return zap.ByteString(FieldSignature, value) | ||
} | ||
|
||
// WithTimestamp sets the timestamp field. | ||
func WithTimestamp(value uint64) zap.Field { | ||
return zap.Uint64(FieldTimestamp, value) | ||
} | ||
|
||
// WithPublicKey sets the timestamp field. | ||
func WithPublicKey(value interface{}) zap.Field { | ||
return zap.Inline(NewObjectMarshaller(FieldPublicKey, value)) | ||
} | ||
|
||
// ObjectMarshaller uses reflection to marshal an object's fields. | ||
type ObjectMarshaller struct { | ||
key string | ||
obj interface{} | ||
} | ||
|
||
// NewObjectMarshaller returns a new ObjectMarshaller. | ||
func NewObjectMarshaller(key string, obj interface{}) *ObjectMarshaller { | ||
return &ObjectMarshaller{key: key, obj: obj} | ||
} | ||
|
||
// MarshalLogObject marshals the object's fields. | ||
func (m *ObjectMarshaller) MarshalLogObject(e zapcore.ObjectEncoder) error { | ||
return e.AddReflected(m.key, m.obj) | ||
} |
Oops, something went wrong.