diff --git a/internal/activity.go b/internal/activity.go index e9cf47134..75b2253e3 100644 --- a/internal/activity.go +++ b/internal/activity.go @@ -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" ) @@ -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, diff --git a/internal/internal_event_handlers.go b/internal/internal_event_handlers.go index 71d5512c0..dbf4acd32 100644 --- a/internal/internal_event_handlers.go +++ b/internal/internal_event_handlers.go @@ -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, diff --git a/internal/internal_worker.go b/internal/internal_worker.go index 360082ccf..3674fdb4a 100644 --- a/internal/internal_worker.go +++ b/internal/internal_worker.go @@ -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, diff --git a/internal/internal_worker_base.go b/internal/internal_worker_base.go index 9fa4d6f6b..049984a0a 100644 --- a/internal/internal_worker_base.go +++ b/internal/internal_worker_base.go @@ -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" ) @@ -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. diff --git a/internal/log/logger_test.go b/internal/log/logger_test.go index 6797afd36..5e98a578c 100644 --- a/internal/log/logger_test.go +++ b/internal/log/logger_test.go @@ -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) diff --git a/internal/log/replay_logger.go b/internal/log/replay_logger.go index 87071772c..ad7748b11 100644 --- a/internal/log/replay_logger.go +++ b/internal/log/replay_logger.go @@ -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) } diff --git a/internal/log/with_logger.go b/internal/log/with_logger.go deleted file mode 100644 index daf87b612..000000000 --- a/internal/log/with_logger.go +++ /dev/null @@ -1,71 +0,0 @@ -// 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 ( - "go.temporal.io/sdk/log" -) - -// With returns Logger instance that prepend every log entry with keyvals. If logger implments WithLogger it is used, otherwise every log call will be intercepted. -func With(logger log.Logger, keyvals ...interface{}) log.Logger { - if wl, ok := logger.(log.WithLogger); ok { - return wl.With(keyvals...) - } - - return newWithLogger(logger, keyvals...) -} - -type withLogger struct { - logger log.Logger - keyvals []interface{} -} - -func newWithLogger(logger log.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)...) -} diff --git a/log/with_logger.go b/log/with_logger.go index 5dc692c5b..25f384e72 100644 --- a/log/with_logger.go +++ b/log/with_logger.go @@ -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)...) +} diff --git a/log/with_logger_test.go b/log/with_logger_test.go new file mode 100644 index 000000000..96d8c1050 --- /dev/null +++ b/log/with_logger_test.go @@ -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) +}