Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
24 changes: 17 additions & 7 deletions logp/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,7 @@ func ConfigureWithCore(loggerCfg Config, core zapcore.Core) error {
// the core and a new one should not be created. The loggerCfg is
// only used to set selectors and level.
func ConfigureWithCoreLocal(loggerCfg Config, core zapcore.Core) (*Logger, error) {
var (
sink zapcore.Core
level zap.AtomicLevel
)

level = zap.NewAtomicLevelAt(loggerCfg.Level.ZapLevel())
var sink zapcore.Core

if loggerCfg.WithFields != nil {
fields := make([]zapcore.Field, 0, len(loggerCfg.WithFields))
Expand Down Expand Up @@ -248,12 +243,27 @@ func ConfigureWithCoreLocal(loggerCfg Config, core zapcore.Core) (*Logger, error
rootLogger: root,
globalLogger: root.WithOptions(zap.AddCallerSkip(1)),
logger: newLogger(root, ""),
level: level,
level: zap.NewAtomicLevelAt(loggerCfg.Level.ZapLevel()),
observedLogs: nil,
})
return newLogger(root, ""), nil
}

// ConfigureEventLoggingOTel takes a list log messages expected to contain sensitive data
// and ensures they are logged using typed logger
func ConfigureEventLoggingOTel(typedMsg []string, core zapcore.Core) *Logger {
sink := &typedLoggerCore{
defaultCore: core,
typedCore: core,
Comment on lines +256 to +257
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this print all the messages for this logger to core? defaultCore and typedCore are same.

Copy link
Contributor Author

@khushijain21 khushijain21 Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think of this as a core on top of an existing typeLoggerCore. The core passed to this method takes care of re-routing the logs correctly. All this does is add additional fields if a message exists.

The assumption of this method is that the output is already defined. The description also explains that

key: TypeKey,
value: EventType,
message: typedMsg,
}

root := zap.New(sink)
return newLogger(root, "")
}

// ConfigureWithTypedOutput configures the global logger to use typed outputs.
//
// If a log entry matches the defined key/value, this entry is logged using the
Expand Down
8 changes: 7 additions & 1 deletion logp/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,12 @@ func TestTypedLoggerCore(t *testing.T) {
field: strField("log.type", "sensitive"),
expectedTypedLog: `{"level":"info","msg":"msg","log.type":"sensitive"}`,
},

{
name: "info level typed logger containing sensitive message",
entry: zapcore.Entry{Level: zapcore.InfoLevel, Message: "use typed logger"},
field: skipField(),
expectedTypedLog: `{"level":"info","msg":"use typed logger","log.type":"sensitive"}`,
},
{
name: "debug level typed logger",
entry: zapcore.Entry{Level: zapcore.DebugLevel, Message: "msg"},
Expand Down Expand Up @@ -422,6 +427,7 @@ func TestTypedLoggerCore(t *testing.T) {
typedCore: typedCore,
key: "log.type",
value: "sensitive",
message: []string{"typed logger"},
}

for _, tc := range testCases {
Expand Down
12 changes: 12 additions & 0 deletions logp/typedloggercore.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"errors"
"fmt"
"io"
"strings"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

Expand Down Expand Up @@ -57,6 +59,7 @@ type typedLoggerCore struct {
defaultCore zapcore.Core
value string
key string
message []string
}

func (t *typedLoggerCore) Enabled(l zapcore.Level) bool {
Expand All @@ -69,6 +72,7 @@ func (t *typedLoggerCore) With(fields []zapcore.Field) zapcore.Core {
typedCore: t.typedCore.With(fields),
key: t.key,
value: t.value,
message: t.message,
}
return &newCore
}
Expand All @@ -93,6 +97,14 @@ func (t *typedLoggerCore) Sync() error {
}

func (t *typedLoggerCore) Write(e zapcore.Entry, fields []zapcore.Field) error {
if len(t.message) != 0 {
for _, msg := range t.message {
if strings.Contains(e.Message, msg) {
fields = append(fields, zap.String(t.key, t.value))
}
}
}

for _, f := range fields {
if f.Key == t.key {
if f.String == t.value {
Expand Down
Loading