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

feat: Allow annotation on subsystem names after instantiation #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package log

import (
"fmt"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -58,6 +59,24 @@ type ZapEventLogger struct {
system string
}

// AnnotateName annotates the logger's system name with the given annotation, prepending
// it if specified.
func (logger *ZapEventLogger) AnnotateName(annotation string, prepend bool) {
newName := logger.system
if prepend {
newName = fmt.Sprintf("%s:%s", annotation, newName)
} else {
newName = fmt.Sprintf("%s:%s", newName, annotation)
}
// register new logger system name
loggerMutex.Lock()
defer loggerMutex.Unlock()
delete(loggers, logger.system)
loggers[newName] = &logger.SugaredLogger

logger.system = newName
}

// Warning is for compatibility
// Deprecated: use Warn(args ...interface{}) instead
func (logger *ZapEventLogger) Warning(args ...interface{}) {
Expand Down
27 changes: 27 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package log

import (
"fmt"
"testing"
)

// TestLogger_AnnotateName ensures that logger's system names are
// accurately annotated.
func TestLogger_AnnotateName(t *testing.T) {
// set up several loggers
Logger("test1")
Logger("test2")
Logger("test3")

annotation := "annotation"

for _, sub := range GetSubsystems() {
Logger(sub).AnnotateName(annotation, true)
}

for _, sub := range GetSubsystems() {
if sub[:len(annotation)] != annotation {
t.Fatalf("expected %s, got %s", fmt.Sprintf("%s:%s", annotation, sub), sub)
}
}
}