Skip to content

Commit

Permalink
Merge pull request #3 from Baha-sk/extra_fields
Browse files Browse the repository at this point in the history
feat: add 3 additional logging fields
  • Loading branch information
baha-ai authored Nov 10, 2022
2 parents c9110e3 + 197d6e9 commit 40ad3f6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
[![Release](https://img.shields.io/github/release/trustbloc/logutil-go.svg?style=flat-square)](https://github.com/trustbloc/logutil-go/releases/latest)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://raw.githubusercontent.com/trustbloc/logutil-go/main/LICENSE)
[![Godocs](https://img.shields.io/badge/godoc-reference-blue.svg)](https://godoc.org/github.com/trustbloc/logutil-go)

[![Build Status](https://github.com/trustbloc/logutil-go/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/trustbloc/logutil-go/actions/workflows/build.yml)
[![codecov](https://codecov.io/gh/trustbloc/logutil-go/branch/main/graph/badge.svg)](https://codecov.io/gh/trustbloc/logutil-go)
[![Go Report Card](https://goreportcard.com/badge/github.com/trustbloc/logutil-go)](https://goreportcard.com/report/github.com/trustbloc/logutil-go)

# logutil-go

General purpose field enabled logging module. This allows logs to be attribute labelled instead of having long string logs.
22 changes: 20 additions & 2 deletions pkg/log/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// Log Fields.
const (
FieldAdditionalMessage = "additionalMessage"
FieldAddress = "address"
FieldCertPoolSize = "certPoolSize"
FieldCommand = "command"
FieldConcurrencyRequests = "concurrencyRequests"
Expand All @@ -28,20 +29,22 @@ const (
FieldIDToken = "idToken"
FieldJSON = "json"
FieldJSONResolution = "jsonResolution"
FieldMessageBrokers = "message-brokers"
FieldName = "name"
FieldParameter = "parameter"
FieldParameters = "parameters"
FieldPath = "path"
FieldPresDefID = "presDefinitionID"
FieldProfileID = "profileID"
FieldResponseBody = "responseBody"
FieldResponse = "response"
FieldResponseBody = "responseBody"
FieldResponses = "responses"
FieldService = "service"
FieldSleep = "sleep"
FieldState = "state"
FieldService = "service"
FieldToken = "token"
FieldTopic = "topic"
FieldTotalMessages = "total-messages"
FieldTotalRequests = "totalRequests"
FieldTxID = "transactionID"
FieldURL = "url"
Expand Down Expand Up @@ -235,3 +238,18 @@ func WithState(state string) zap.Field {
func WithProfileID(id string) zap.Field {
return zap.String(FieldProfileID, id)
}

// WithAddress sets the address field.
func WithAddress(address string) zap.Field {
return zap.String(FieldAddress, address)
}

// WithMessageBrokers sets the topic field.
func WithMessageBrokers(value []string) zap.Field {
return zap.Any(FieldMessageBrokers, value)
}

// WithTotalMessages sets the total messages field.
func WithTotalMessages(totalMessages int) zap.Field {
return zap.Int(FieldTotalMessages, totalMessages)
}
12 changes: 12 additions & 0 deletions pkg/log/fields_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestStandardFields(t *testing.T) {
topic := "some topic"
msg := "Some message"
hostURL := "https://localhost:8080"
address := "https://localhost:8080"
responseBody := []byte("response body")
token := "someToken"
totalRequests := 10
Expand All @@ -84,6 +85,8 @@ func TestStandardFields(t *testing.T) {
profileID := "some profile id"
parameter := "param1"
parameters := &mockObject{Field1: "param1", Field2: 4612}
brokers := []string{"broker"}
totalMessages := 3

dockerComposeCmd := strings.Join([]string{
"docker-compose",
Expand Down Expand Up @@ -127,6 +130,9 @@ func TestStandardFields(t *testing.T) {
WithUserLogLevel(DEBUG.String()),
WithVPToken(vpToken),
WithWorkers(workers),
WithAddress(address),
WithMessageBrokers(brokers),
WithTotalMessages(totalMessages),
)

t.Logf(stdOut.String())
Expand Down Expand Up @@ -160,6 +166,9 @@ func TestStandardFields(t *testing.T) {
require.Equal(t, presDefID, l.PresDefID)
require.Equal(t, state, l.State)
require.Equal(t, profileID, l.ProfileID)
require.Equal(t, address, l.Address)
require.Equal(t, brokers, l.MessageBrokers)
require.Equal(t, totalMessages, l.TotalMessages)
})

t.Run("json fields 2", func(t *testing.T) {
Expand Down Expand Up @@ -219,6 +228,9 @@ type logData struct {
UserLogLevel string `json:"userLogLevel"`
VPToken string `json:"vpToken"`
Workers int `json:"workers"`
Address string `json:"address"`
MessageBrokers []string `json:"message-brokers"`
TotalMessages int `json:"total-messages"`
}

func unmarshalLogData(t *testing.T, b []byte) *logData {
Expand Down
2 changes: 1 addition & 1 deletion scripts/check_unit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if [ -f profile.out ]; then
fi
}

# Running vcs unit tests
# Running logutil-go unit tests
echo "logutil-go unit tests..."
PKGS=`go list github.com/trustbloc/logutil-go/... 2> /dev/null | \
grep -v /mocks`
Expand Down

0 comments on commit 40ad3f6

Please sign in to comment.