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

Expose log.With func #381

Merged
merged 3 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions internal/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (

"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/internal/common"
ilog "go.temporal.io/sdk/internal/log"
"go.temporal.io/sdk/log"
)

Expand Down Expand Up @@ -273,7 +272,7 @@ func WithActivityTask(
deadline = startToCloseDeadline
}

logger = ilog.With(logger,
logger = log.With(logger,
tagActivityID, task.ActivityId,
tagActivityType, task.ActivityType.GetName(),
tagAttempt, task.Attempt,
Expand Down
2 changes: 1 addition & 1 deletion internal/internal_event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func newWorkflowExecutionEventHandler(
tracer: tracer,
}
context.logger = ilog.NewReplayLogger(
ilog.With(logger,
log.With(logger,
tagWorkflowType, workflowInfo.WorkflowType.Name,
tagWorkflowID, workflowInfo.WorkflowExecution.ID,
tagRunID, workflowInfo.WorkflowExecution.RunID,
Expand Down
2 changes: 1 addition & 1 deletion internal/internal_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@ func NewAggregatedWorker(client *WorkflowClient, taskQueue string, options Worke
}

ensureRequiredParams(&workerParams)
workerParams.Logger = ilog.With(workerParams.Logger,
workerParams.Logger = log.With(workerParams.Logger,
tagNamespace, client.namespace,
tagTaskQueue, taskQueue,
tagWorkerID, workerParams.Identity,
Expand Down
3 changes: 1 addition & 2 deletions internal/internal_worker_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/internal/common/backoff"
"go.temporal.io/sdk/internal/common/metrics"
ilog "go.temporal.io/sdk/internal/log"
"go.temporal.io/sdk/log"
)

Expand Down Expand Up @@ -178,7 +177,7 @@ func newBaseWorker(options baseWorkerOptions, logger log.Logger, metricsScope ta
stopCh: make(chan struct{}),
taskLimiter: rate.NewLimiter(rate.Limit(options.maxTaskPerSecond), 1),
retrier: backoff.NewConcurrentRetrier(pollOperationRetryPolicy),
logger: ilog.With(logger, tagWorkerType, options.workerType),
logger: log.With(logger, tagWorkerType, options.workerType),
metricsScope: metrics.GetWorkerScope(metricsScope, options.workerType),
pollerRequestCh: make(chan struct{}, options.maxConcurrentTask),
taskQueueCh: make(chan interface{}), // no buffer, so poller only able to poll new task after previous is dispatched.
Expand Down
14 changes: 3 additions & 11 deletions internal/log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,13 @@ import (
"testing"

"github.com/stretchr/testify/assert"

"go.temporal.io/sdk/log"
)

func TestMemoryLogger_With(t *testing.T) {
logger := NewMemoryLogger()
withLogger := With(logger, "p1", 1, "p2", "v2")
withLogger.Info("message", "p3", float64(3))
logger.Info("message2", "p4", 4)

assert.Equal(t, "INFO message p1 1 p2 v2 p3 3\n", logger.Lines()[0])
assert.Equal(t, "INFO message2 p4 4\n", logger.Lines()[1])
}

func TestWithLogger(t *testing.T) {
logger := NewMemoryLogger()
withLogger := newWithLogger(logger, "p1", 1, "p2", "v2")
withLogger := log.With(logger, "p1", 1, "p2", "v2")
withLogger.Info("message", "p3", float64(3))
logger.Info("message2", "p4", 4)

Expand Down
2 changes: 1 addition & 1 deletion internal/log/replay_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ func (l *ReplayLogger) Error(msg string, keyvals ...interface{}) {

// With returns new logger the prepend every log entry with keyvals.
func (l *ReplayLogger) With(keyvals ...interface{}) log.Logger {
return NewReplayLogger(With(l.logger, keyvals), l.isReplay, l.enableLoggingInReplay)
return NewReplayLogger(log.With(l.logger, keyvals), l.isReplay, l.enableLoggingInReplay)
}
71 changes: 0 additions & 71 deletions internal/log/with_logger.go

This file was deleted.

42 changes: 42 additions & 0 deletions log/with_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,45 @@ package log
type WithLogger interface {
With(keyvals ...interface{}) Logger
}

// With returns Logger instance that prepend every log entry with keyvals. If logger implements WithLogger it is used, otherwise every log call will be intercepted.
func With(logger Logger, keyvals ...interface{}) Logger {
if wl, ok := logger.(WithLogger); ok {
return wl.With(keyvals...)
}

return newWithLogger(logger, keyvals...)
}

type withLogger struct {
logger Logger
keyvals []interface{}
}

func newWithLogger(logger Logger, keyvals ...interface{}) *withLogger {
return &withLogger{logger: logger, keyvals: keyvals}
}

func (l *withLogger) prependKeyvals(keyvals []interface{}) []interface{} {
return append(l.keyvals, keyvals...)
}

// Debug writes message to the log.
func (l *withLogger) Debug(msg string, keyvals ...interface{}) {
l.logger.Debug(msg, l.prependKeyvals(keyvals)...)
}

// Info writes message to the log.
func (l *withLogger) Info(msg string, keyvals ...interface{}) {
l.logger.Info(msg, l.prependKeyvals(keyvals)...)
}

// Warn writes message to the log.
func (l *withLogger) Warn(msg string, keyvals ...interface{}) {
l.logger.Warn(msg, l.prependKeyvals(keyvals)...)
}

// Error writes message to the log.
func (l *withLogger) Error(msg string, keyvals ...interface{}) {
l.logger.Error(msg, l.prependKeyvals(keyvals)...)
}
39 changes: 39 additions & 0 deletions log/with_logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package log

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestWithLogger(t *testing.T) {
wl := &withLogger{
keyvals: []interface{}{"p1", 1, "p2", "v2"},
}
allKeys := wl.prependKeyvals([]interface{}{"p4", 4})
assert.Equal(t, []interface{}{"p1", 1, "p2", "v2", "p4", 4}, allKeys)
}