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 Logger.Write method to interface #4091

Merged
merged 3 commits into from
Jun 21, 2024
Merged
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
56 changes: 28 additions & 28 deletions logging/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (imp *impl) shouldLog(logLevel Level) bool {
return logLevel >= imp.level.Get()
}

func (imp *impl) log(entry *LogEntry) {
func (imp *impl) Write(entry *LogEntry) {
Copy link
Member

Choose a reason for hiding this comment

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

Ok, this makes sense to me now. Sorry I was being dense.

imp.testHelper()
for _, appender := range imp.appenders {
err := appender.Write(entry.Entry, entry.fields)
Expand Down Expand Up @@ -226,7 +226,7 @@ func (imp *impl) formatw(logLevel Level, traceKey, msg string, keysAndValues ...
func (imp *impl) Debug(args ...interface{}) {
imp.testHelper()
if imp.shouldLog(DEBUG) {
imp.log(imp.format(DEBUG, emptyTraceKey, args...))
imp.Write(imp.format(DEBUG, emptyTraceKey, args...))
}
}

Expand All @@ -236,14 +236,14 @@ func (imp *impl) CDebug(ctx context.Context, args ...interface{}) {

// We log if the logger is configured for debug, or if there's a trace key.
if imp.shouldLog(DEBUG) || dbgName != emptyTraceKey {
imp.log(imp.format(DEBUG, dbgName, args...))
imp.Write(imp.format(DEBUG, dbgName, args...))
}
}

func (imp *impl) Debugf(template string, args ...interface{}) {
imp.testHelper()
if imp.shouldLog(DEBUG) {
imp.log(imp.formatf(DEBUG, emptyTraceKey, template, args...))
imp.Write(imp.formatf(DEBUG, emptyTraceKey, template, args...))
}
}

Expand All @@ -253,14 +253,14 @@ func (imp *impl) CDebugf(ctx context.Context, template string, args ...interface

// We log if the logger is configured for debug, or if there's a trace key.
if imp.shouldLog(DEBUG) || dbgName != emptyTraceKey {
imp.log(imp.formatf(DEBUG, dbgName, template, args...))
imp.Write(imp.formatf(DEBUG, dbgName, template, args...))
}
}

func (imp *impl) Debugw(msg string, keysAndValues ...interface{}) {
imp.testHelper()
if imp.shouldLog(DEBUG) {
imp.log(imp.formatw(DEBUG, emptyTraceKey, msg, keysAndValues...))
imp.Write(imp.formatw(DEBUG, emptyTraceKey, msg, keysAndValues...))
}
}

Expand All @@ -270,14 +270,14 @@ func (imp *impl) CDebugw(ctx context.Context, msg string, keysAndValues ...inter

// We log if the logger is configured for debug, or if there's a trace key.
if imp.shouldLog(DEBUG) || dbgName != emptyTraceKey {
imp.log(imp.formatw(DEBUG, dbgName, msg, keysAndValues...))
imp.Write(imp.formatw(DEBUG, dbgName, msg, keysAndValues...))
}
}

func (imp *impl) Info(args ...interface{}) {
imp.testHelper()
if imp.shouldLog(INFO) {
imp.log(imp.format(INFO, emptyTraceKey, args...))
imp.Write(imp.format(INFO, emptyTraceKey, args...))
}
}

Expand All @@ -287,14 +287,14 @@ func (imp *impl) CInfo(ctx context.Context, args ...interface{}) {

// We log if the logger is configured for info, or if there's a trace key.
if imp.shouldLog(INFO) || dbgName != emptyTraceKey {
imp.log(imp.format(INFO, dbgName, args...))
imp.Write(imp.format(INFO, dbgName, args...))
}
}

func (imp *impl) Infof(template string, args ...interface{}) {
imp.testHelper()
if imp.shouldLog(INFO) {
imp.log(imp.formatf(INFO, emptyTraceKey, template, args...))
imp.Write(imp.formatf(INFO, emptyTraceKey, template, args...))
}
}

Expand All @@ -304,14 +304,14 @@ func (imp *impl) CInfof(ctx context.Context, template string, args ...interface{

// We log if the logger is configured for info, or if there's a trace key.
if imp.shouldLog(INFO) || dbgName != emptyTraceKey {
imp.log(imp.formatf(INFO, dbgName, template, args...))
imp.Write(imp.formatf(INFO, dbgName, template, args...))
}
}

func (imp *impl) Infow(msg string, keysAndValues ...interface{}) {
imp.testHelper()
if imp.shouldLog(INFO) {
imp.log(imp.formatw(INFO, emptyTraceKey, msg, keysAndValues...))
imp.Write(imp.formatw(INFO, emptyTraceKey, msg, keysAndValues...))
}
}

Expand All @@ -321,14 +321,14 @@ func (imp *impl) CInfow(ctx context.Context, msg string, keysAndValues ...interf

// We log if the logger is configured for info, or if there's a trace key.
if imp.shouldLog(INFO) || dbgName != emptyTraceKey {
imp.log(imp.formatw(INFO, dbgName, msg, keysAndValues...))
imp.Write(imp.formatw(INFO, dbgName, msg, keysAndValues...))
}
}

func (imp *impl) Warn(args ...interface{}) {
imp.testHelper()
if imp.shouldLog(WARN) {
imp.log(imp.format(WARN, emptyTraceKey, args...))
imp.Write(imp.format(WARN, emptyTraceKey, args...))
}
}

Expand All @@ -338,14 +338,14 @@ func (imp *impl) CWarn(ctx context.Context, args ...interface{}) {

// We log if the logger is configured for warn, or if there's a trace key.
if imp.shouldLog(WARN) || dbgName != emptyTraceKey {
imp.log(imp.format(WARN, dbgName, args...))
imp.Write(imp.format(WARN, dbgName, args...))
}
}

func (imp *impl) Warnf(template string, args ...interface{}) {
imp.testHelper()
if imp.shouldLog(WARN) {
imp.log(imp.formatf(WARN, emptyTraceKey, template, args...))
imp.Write(imp.formatf(WARN, emptyTraceKey, template, args...))
}
}

Expand All @@ -355,14 +355,14 @@ func (imp *impl) CWarnf(ctx context.Context, template string, args ...interface{

// We log if the logger is configured for warn, or if there's a trace key.
if imp.shouldLog(WARN) || dbgName != emptyTraceKey {
imp.log(imp.formatf(WARN, dbgName, template, args...))
imp.Write(imp.formatf(WARN, dbgName, template, args...))
}
}

func (imp *impl) Warnw(msg string, keysAndValues ...interface{}) {
imp.testHelper()
if imp.shouldLog(WARN) {
imp.log(imp.formatw(WARN, emptyTraceKey, msg, keysAndValues...))
imp.Write(imp.formatw(WARN, emptyTraceKey, msg, keysAndValues...))
}
}

Expand All @@ -372,14 +372,14 @@ func (imp *impl) CWarnw(ctx context.Context, msg string, keysAndValues ...interf

// We log if the logger is configured for warn, or if there's a trace key.
if imp.shouldLog(WARN) || dbgName != emptyTraceKey {
imp.log(imp.formatw(WARN, dbgName, msg, keysAndValues...))
imp.Write(imp.formatw(WARN, dbgName, msg, keysAndValues...))
}
}

func (imp *impl) Error(args ...interface{}) {
imp.testHelper()
if imp.shouldLog(ERROR) {
imp.log(imp.format(ERROR, emptyTraceKey, args...))
imp.Write(imp.format(ERROR, emptyTraceKey, args...))
}
}

Expand All @@ -389,14 +389,14 @@ func (imp *impl) CError(ctx context.Context, args ...interface{}) {

// We log if the logger is configured for error, or if there's a trace key.
if imp.shouldLog(ERROR) || dbgName != emptyTraceKey {
imp.log(imp.format(ERROR, dbgName, args...))
imp.Write(imp.format(ERROR, dbgName, args...))
}
}

func (imp *impl) Errorf(template string, args ...interface{}) {
imp.testHelper()
if imp.shouldLog(ERROR) {
imp.log(imp.formatf(ERROR, emptyTraceKey, template, args...))
imp.Write(imp.formatf(ERROR, emptyTraceKey, template, args...))
}
}

Expand All @@ -406,14 +406,14 @@ func (imp *impl) CErrorf(ctx context.Context, template string, args ...interface

// We log if the logger is configured for error, or if there's a trace key.
if imp.shouldLog(ERROR) || dbgName != emptyTraceKey {
imp.log(imp.formatf(ERROR, dbgName, template, args...))
imp.Write(imp.formatf(ERROR, dbgName, template, args...))
}
}

func (imp *impl) Errorw(msg string, keysAndValues ...interface{}) {
imp.testHelper()
if imp.shouldLog(ERROR) {
imp.log(imp.formatw(ERROR, emptyTraceKey, msg, keysAndValues...))
imp.Write(imp.formatw(ERROR, emptyTraceKey, msg, keysAndValues...))
}
}

Expand All @@ -423,26 +423,26 @@ func (imp *impl) CErrorw(ctx context.Context, msg string, keysAndValues ...inter

// We log if the logger is configured for error, or if there's a trace key.
if imp.shouldLog(ERROR) || dbgName != emptyTraceKey {
imp.log(imp.formatw(ERROR, dbgName, msg, keysAndValues...))
imp.Write(imp.formatw(ERROR, dbgName, msg, keysAndValues...))
}
}

// These Fatal* methods log as errors then exit the process.
func (imp *impl) Fatal(args ...interface{}) {
imp.testHelper()
imp.log(imp.format(ERROR, emptyTraceKey, args...))
imp.Write(imp.format(ERROR, emptyTraceKey, args...))
os.Exit(1)
}

func (imp *impl) Fatalf(template string, args ...interface{}) {
imp.testHelper()
imp.log(imp.formatf(ERROR, emptyTraceKey, template, args...))
imp.Write(imp.formatf(ERROR, emptyTraceKey, template, args...))
os.Exit(1)
}

func (imp *impl) Fatalw(msg string, keysAndValues ...interface{}) {
imp.testHelper()
imp.log(imp.formatw(ERROR, emptyTraceKey, msg, keysAndValues...))
imp.Write(imp.formatw(ERROR, emptyTraceKey, msg, keysAndValues...))
os.Exit(1)
}

Expand Down
11 changes: 11 additions & 0 deletions logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package logging

import (
"context"
"fmt"
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -16,6 +18,8 @@ type Logger interface {
Sublogger(subname string) Logger
AddAppender(appender Appender)
AsZap() *zap.SugaredLogger
// Unconditionally logs a LogEntry object. Specifically any configured log level is ignored.
Write(*LogEntry)
Copy link
Member

Choose a reason for hiding this comment

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

No idea why the linter doesn't complain about any of these. Can we explicitly document this with:

Unconditionally logs a LogEntry object. Specifically any configured log level is ignored.

I might follow up with adding a Log(*LogEntry) that conditionally logs. So we can replace this switch statement usage.


CDebug(ctx context.Context, args ...interface{})
CDebugf(ctx context.Context, template string, args ...interface{})
Expand Down Expand Up @@ -163,3 +167,10 @@ func (logger zLogger) CErrorf(ctx context.Context, template string, args ...inte
func (logger zLogger) CErrorw(ctx context.Context, msg string, keysAndValues ...interface{}) {
logger.Errorw(msg, keysAndValues...)
}

func (logger zLogger) Write(entry *LogEntry) {
err := logger.Desugar().Core().Write(entry.Entry, entry.fields)
if err != nil {
fmt.Fprint(os.Stderr, err)
}
}
Loading