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

Add wrapped logger #119

Merged
merged 16 commits into from
Mar 12, 2021
8 changes: 8 additions & 0 deletions changelog/@unreleased/pr-119.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: feature
feature:
description: |-
Add support for emitting wrapped.1 logs.
Wrapped loggers expose the same interface to consumers as the logger of the underlying format.
This change includes a wrapped logging implementation for service.1 logs.
links:
- https://github.com/palantir/witchcraft-go-logging/pull/119
25 changes: 25 additions & 0 deletions wlog-glog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import (
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log/svc1logtests"
"github.com/palantir/witchcraft-go-logging/wlog/trclog/trc1log"
"github.com/palantir/witchcraft-go-logging/wlog/trclog/trc1log/trc1logtests"
"github.com/palantir/witchcraft-go-logging/wlog/wrappedlog/wrapped1log"
"github.com/palantir/witchcraft-go-logging/wlog/wrappedlog/wrapped1log/wrapped1logtests"
"github.com/palantir/witchcraft-go-tracing/wtracing"
"github.com/palantir/witchcraft-go-tracing/wzipkin"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -198,3 +200,26 @@ func TestDiag1Log(t *testing.T) {
)
}
}

func TestWrapped1Log(t *testing.T) {
os.Args = []string{
os.Args[0],
"-logtostderr=true",
}
flag.Parse()

entityName := "entity"
entityVersion := "version"
for _, tc := range wrapped1logtests.TestCases(entityName, entityVersion) {
// TODO: test output
logger := wrapped1log.NewFromProvider(
os.Stdout,
wlog.DebugLevel,
wlogglog.LoggerProvider(),
entityName,
entityVersion,
).Service(svc1log.Origin(tc.Origin))

logger.Info(tc.Message, tc.LogParams...)
}
}
14 changes: 14 additions & 0 deletions wlog-zap/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log/svc1logtests"
"github.com/palantir/witchcraft-go-logging/wlog/trclog/trc1log"
"github.com/palantir/witchcraft-go-logging/wlog/trclog/trc1log/trc1logtests"
"github.com/palantir/witchcraft-go-logging/wlog/wrappedlog/wrapped1log"
"github.com/palantir/witchcraft-go-logging/wlog/wrappedlog/wrapped1log/wrapped1logtests"
)

func TestSvc1Log(t *testing.T) {
Expand Down Expand Up @@ -103,3 +105,15 @@ func TestDiag1Log(t *testing.T) {
)
})
}

func TestWrapped1LogSvc1Log(t *testing.T) {
entityName := "entity"
entityVersion := "version"
wrapped1logtests.Svc1LogJSONTestSuite(
t,
entityName,
entityVersion,
func(w io.Writer, level wlog.LogLevel, origin string) svc1log.Logger {
return wrapped1log.NewFromProvider(w, level, zapimpl.LoggerProvider(), entityName, entityVersion).Service(svc1log.Origin(origin))
})
}
14 changes: 14 additions & 0 deletions wlog-zerolog/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import (
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log/svc1logtests"
"github.com/palantir/witchcraft-go-logging/wlog/trclog/trc1log"
"github.com/palantir/witchcraft-go-logging/wlog/trclog/trc1log/trc1logtests"
"github.com/palantir/witchcraft-go-logging/wlog/wrappedlog/wrapped1log"
"github.com/palantir/witchcraft-go-logging/wlog/wrappedlog/wrapped1log/wrapped1logtests"
)

func TestSvc1Log(t *testing.T) {
Expand Down Expand Up @@ -103,3 +105,15 @@ func TestDiag1Log(t *testing.T) {
)
})
}

func TestWrapped1Log(t *testing.T) {
entityName := "entity"
entityVersion := "version"
wrapped1logtests.Svc1LogJSONTestSuite(
t,
entityName,
entityVersion,
func(w io.Writer, level wlog.LogLevel, origin string) svc1log.Logger {
return wrapped1log.NewFromProvider(w, level, wlogzerolog.LoggerProvider(), entityName, entityVersion).Service(svc1log.Origin(origin))
})
}
11 changes: 5 additions & 6 deletions wlog/svclog/svc1log/logger_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,26 @@ type defaultLogger struct {
}

func (l *defaultLogger) Debug(msg string, params ...Param) {
l.logger.Debug(msg, toParams(DebugLevelParam(), params)...)
l.logger.Debug(msg, ToParams(DebugLevelParam(), params)...)
}

func (l *defaultLogger) Info(msg string, params ...Param) {
l.logger.Info(msg, toParams(InfoLevelParam(), params)...)

l.logger.Info(msg, ToParams(InfoLevelParam(), params)...)
}

func (l *defaultLogger) Warn(msg string, params ...Param) {
l.logger.Warn(msg, toParams(WarnLevelParam(), params)...)
l.logger.Warn(msg, ToParams(WarnLevelParam(), params)...)
}

func (l *defaultLogger) Error(msg string, params ...Param) {
l.logger.Error(msg, toParams(ErrorLevelParam(), params)...)
l.logger.Error(msg, ToParams(ErrorLevelParam(), params)...)
}

func (l *defaultLogger) SetLevel(level wlog.LogLevel) {
l.logger.SetLevel(level)
}

func toParams(level wlog.Param, inParams []Param) []wlog.Param {
func ToParams(level wlog.Param, inParams []Param) []wlog.Param {
outParams := make([]wlog.Param, len(defaultTypeParam)+1+len(inParams))
copy(outParams, defaultTypeParam)
outParams[len(defaultTypeParam)] = level
Expand Down
30 changes: 30 additions & 0 deletions wlog/wrappedlog/wrapped1log/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) 2021 Palantir Technologies. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package wrapped1log

import (
"context"
)

type wrapped1LogContextKeyType string

const contextKey = wrapped1LogContextKeyType(TypeValue)

// WithLogger returns a copy of the provided context with the provided Logger included as a value. This operation will
// replace any logger that was previously set on the context (along with all parameters that may have been set on the
// logger).
func WithLogger(ctx context.Context, logger Logger) context.Context {
return context.WithValue(ctx, contextKey, logger)
}
39 changes: 39 additions & 0 deletions wlog/wrappedlog/wrapped1log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright (c) 2021 Palantir Technologies. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package wrapped1log

import (
"io"

"github.com/palantir/witchcraft-go-logging/wlog"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
)

type Logger interface {
Service(params ...svc1log.Param) svc1log.Logger
}

func New(w io.Writer, level wlog.LogLevel, name, version string) Logger {
return NewFromProvider(w, level, wlog.DefaultLoggerProvider(), name, version)
}

func NewFromProvider(w io.Writer, level wlog.LogLevel, creator wlog.LoggerProvider, name, version string) Logger {
return &defaultLogger{
name: name,
version: version,
logger: creator.NewLogger(w),
levellogger: creator.NewLeveledLogger(w, level),
}
}
42 changes: 42 additions & 0 deletions wlog/wrappedlog/wrapped1log/logger_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2021 Palantir Technologies. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package wrapped1log

import (
"github.com/palantir/witchcraft-go-logging/wlog"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
)

type defaultLogger struct {
name string
version string
logger wlog.Logger
levellogger wlog.LeveledLogger
}

func (l *defaultLogger) Service(params ...svc1log.Param) svc1log.Logger {
return &wrappedSvc1Logger{
params: params,
name: l.name,
version: l.version,
logger: l.levellogger,
}
}

var defaultTypeParam = []wlog.Param{
wlog.NewParam(func(entry wlog.LogEntry) {
entry.StringValue(wlog.TypeKey, TypeValue)
}),
}
56 changes: 56 additions & 0 deletions wlog/wrappedlog/wrapped1log/logger_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2021 Palantir Technologies. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package wrapped1log

import (
"github.com/palantir/witchcraft-go-logging/wlog"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
)

type wrappedSvc1Logger struct {
name string
version string
params []svc1log.Param

logger wlog.LeveledLogger
}

func (l *wrappedSvc1Logger) Debug(msg string, params ...svc1log.Param) {
l.logger.Debug("", l.toServiceParams(msg, svc1log.DebugLevelParam(), params)...)
}

func (l *wrappedSvc1Logger) Info(msg string, params ...svc1log.Param) {
l.logger.Info("", l.toServiceParams(msg, svc1log.InfoLevelParam(), params)...)
}

func (l *wrappedSvc1Logger) Warn(msg string, params ...svc1log.Param) {
l.logger.Warn("", l.toServiceParams(msg, svc1log.WarnLevelParam(), params)...)
}

func (l *wrappedSvc1Logger) Error(msg string, params ...svc1log.Param) {
l.logger.Error("", l.toServiceParams(msg, svc1log.ErrorLevelParam(), params)...)
}

func (l *wrappedSvc1Logger) SetLevel(level wlog.LogLevel) {
l.logger.SetLevel(level)
}

func (l *wrappedSvc1Logger) toServiceParams(message string, levelParam wlog.Param, inParams []svc1log.Param) []wlog.Param {
outParams := make([]wlog.Param, len(defaultTypeParam)+2)
copy(outParams, defaultTypeParam)
outParams[len(defaultTypeParam)] = wlog.NewParam(wrappedTypeParams(l.name, l.version).apply)
outParams[len(defaultTypeParam)+1] = wlog.NewParam(svc1PayloadParams(message, levelParam, append(l.params, inParams...)).apply)
return outParams
}
73 changes: 73 additions & 0 deletions wlog/wrappedlog/wrapped1log/params.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2021 Palantir Technologies. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package wrapped1log

import (
"github.com/palantir/witchcraft-go-logging/wlog"
"github.com/palantir/witchcraft-go-logging/wlog/svclog/svc1log"
)

const (
TypeValue = "wrapped.1"

WrappedEntityNameKey = "entityName"
WrappedEntityVersionKey = "entityVersion"

PayloadKey = "payload"
PayloadTypeKey = "type"
PayloadServiceLogV1 = "serviceLogV1"
PayloadRequestLogV2 = "requestLogV2"
PayloadTraceLogV1 = "traceLogV1"
PayloadEventLogV2 = "eventLogV2"
PayloadMetricLogV1 = "metricLogV1"
PayloadAuditLogV2 = "auditLogV2"
PayloadDiagnosticLogV1 = "diagnosticLogV1"
)

type Param interface {
apply(entry wlog.LogEntry)
}

func ApplyParam(p Param, entry wlog.LogEntry) {
if p == nil {
return
}
p.apply(entry)
}

type paramFunc func(entry wlog.LogEntry)

func (f paramFunc) apply(entry wlog.LogEntry) {
f(entry)
}

func svc1PayloadParams(message string, level wlog.Param, params []svc1log.Param) Param {
return paramFunc(func(entry wlog.LogEntry) {
svc1Log := wlog.NewMapLogEntry()
wlog.ApplyParams(svc1Log, wlog.ParamsWithMessage(message, svc1log.ToParams(level, params)))
payload := wlog.NewMapLogEntry()
payload.StringValue(PayloadTypeKey, PayloadServiceLogV1)
payload.AnyMapValue(PayloadServiceLogV1, svc1Log.AllValues())

entry.AnyMapValue(PayloadKey, payload.AllValues())
})
}

func wrappedTypeParams(name, version string) Param {
return paramFunc(func(logger wlog.LogEntry) {
logger.StringValue(WrappedEntityNameKey, name)
logger.StringValue(WrappedEntityVersionKey, version)
})
}
Loading