Skip to content

Commit

Permalink
[FAB-12769] Rename ModuleLevels to LoggerLevels
Browse files Browse the repository at this point in the history
Change-Id: I9c582c24041d6b4ce9ea0bde1b6a92f94c0fd598
Signed-off-by: Matthew Sykes <[email protected]>
  • Loading branch information
sykesm committed Nov 7, 2018
1 parent 1789f4f commit 71e975e
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 67 deletions.
2 changes: 1 addition & 1 deletion common/flogging/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type EncodingSelector interface {
// enabled levels.
type Core struct {
zapcore.LevelEnabler
Levels *ModuleLevels
Levels *LoggerLevels
Encoders map[Encoding]zapcore.Encoder
Selector EncodingSelector
Output zapcore.WriteSyncer
Expand Down
2 changes: 1 addition & 1 deletion common/flogging/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestCoreWith(t *testing.T) {

func TestCoreCheck(t *testing.T) {
var enabledArgs []zapcore.Level
levels := &flogging.ModuleLevels{}
levels := &flogging.LoggerLevels{}
err := levels.ActivateSpec("warning")
assert.NoError(t, err)
core := &flogging.Core{
Expand Down
15 changes: 8 additions & 7 deletions common/flogging/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,16 @@ func Reset() {
Global.Apply(Config{})
}

// GetModuleLevel gets the current logging level for the specified module.
func GetModuleLevel(module string) string {
return strings.ToUpper(Global.Level(module).String())
// GetLoggerLevel gets the current logging level for the logger with the
// provided name.
func GetLoggerLevel(loggerName string) string {
return strings.ToUpper(Global.Level(loggerName).String())
}

// MustGetLogger is used in place of `logging.MustGetLogger` to allow us to
// store a map of all modules and submodules that have loggers in the logging.
func MustGetLogger(module string) *FabricLogger {
return Global.Logger(module)
// MustGetLogger creates a logger with the specified name. If an invalid name
// is provided, the operation will panic.
func MustGetLogger(loggerName string) *FabricLogger {
return Global.Logger(loggerName)
}

// ActivateSpec is used to activate a logging specification.
Expand Down
10 changes: 5 additions & 5 deletions common/flogging/global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ func TestGlobalReset(t *testing.T) {

system, err := flogging.New(flogging.Config{})
assert.NoError(t, err)
assert.NotEqual(t, flogging.Global.ModuleLevels, system.ModuleLevels)
assert.NotEqual(t, flogging.Global.LoggerLevels, system.LoggerLevels)
assert.NotEqual(t, flogging.Global.Encoding(), system.Encoding())

flogging.Reset()
assert.Equal(t, flogging.Global.ModuleLevels, system.ModuleLevels)
assert.Equal(t, flogging.Global.LoggerLevels, system.LoggerLevels)
assert.Equal(t, flogging.Global.Encoding(), system.Encoding())
}

Expand Down Expand Up @@ -81,15 +81,15 @@ func TestGlobalDefaultLevel(t *testing.T) {
assert.Equal(t, "INFO", flogging.DefaultLevel())
}

func TestGlobalGetModuleLevel(t *testing.T) {
func TestGlobalGetLoggerLevel(t *testing.T) {
flogging.Reset()
assert.Equal(t, "INFO", flogging.GetModuleLevel("some.logger"))
assert.Equal(t, "INFO", flogging.GetLoggerLevel("some.logger"))
}

func TestGlobalMustGetLogger(t *testing.T) {
flogging.Reset()

l := flogging.MustGetLogger("module-name")
l := flogging.MustGetLogger("logger-name")
assert.NotNil(t, l)
}

Expand Down
2 changes: 1 addition & 1 deletion common/flogging/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestLegacyInitFromSpec(t *testing.T) {
assert.Equal(t, tc.expectedResult, l)

for k, v := range tc.expectedLevels {
assert.Equal(t, v, flogging.GetModuleLevel(k))
assert.Equal(t, v, flogging.GetLoggerLevel(k))
}
})
}
Expand Down
64 changes: 32 additions & 32 deletions common/flogging/modulelevels.go → common/flogging/loggerlevels.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ import (
"go.uber.org/zap/zapcore"
)

// ModuleLevels tracks the logging level of logging modules.
type ModuleLevels struct {
// LoggerLevels tracks the logging level of named loggers.
type LoggerLevels struct {
defaultLevel zapcore.Level

mutex sync.RWMutex
levelCache map[string]zapcore.Level
specs map[string]zapcore.Level
}

// DefaultLevel returns the default logging level for modules that do not have
// DefaultLevel returns the default logging level for loggers that do not have
// an explicit level set.
func (m *ModuleLevels) DefaultLevel() zapcore.Level {
m.mutex.RLock()
l := m.defaultLevel
m.mutex.RUnlock()
return l
func (l *LoggerLevels) DefaultLevel() zapcore.Level {
l.mutex.RLock()
lvl := l.defaultLevel
l.mutex.RUnlock()
return lvl
}

// ActivateSpec is used to modify logging levels.
//
// The logging specification has the following form:
// [<logger>[,<logger>...]=]<level>[:[<logger>[,<logger>...]=]<level>...]
func (m *ModuleLevels) ActivateSpec(spec string) error {
m.mutex.Lock()
defer m.mutex.Unlock()
func (l *LoggerLevels) ActivateSpec(spec string) error {
l.mutex.Lock()
defer l.mutex.Unlock()

defaultLevel := zapcore.InfoLevel
specs := map[string]zapcore.Level{}
Expand Down Expand Up @@ -80,9 +80,9 @@ func (m *ModuleLevels) ActivateSpec(spec string) error {
}
}

m.defaultLevel = defaultLevel
m.specs = specs
m.levelCache = map[string]zapcore.Level{}
l.defaultLevel = defaultLevel
l.specs = specs
l.levelCache = map[string]zapcore.Level{}

return nil
}
Expand All @@ -101,57 +101,57 @@ func isValidLoggerName(loggerName string) bool {
// Level returns the effective logging level for a logger. If a level has not
// been explicitly set for the logger, the default logging level will be
// returned.
func (m *ModuleLevels) Level(loggerName string) zapcore.Level {
if level, ok := m.cachedLevel(loggerName); ok {
func (l *LoggerLevels) Level(loggerName string) zapcore.Level {
if level, ok := l.cachedLevel(loggerName); ok {
return level
}

m.mutex.Lock()
level := m.calculateLevel(loggerName)
m.levelCache[loggerName] = level
m.mutex.Unlock()
l.mutex.Lock()
level := l.calculateLevel(loggerName)
l.levelCache[loggerName] = level
l.mutex.Unlock()

return level
}

// calculateLevel walks the logger name back to find the appropriate
// log level from the current spec.
func (m *ModuleLevels) calculateLevel(loggerName string) zapcore.Level {
func (l *LoggerLevels) calculateLevel(loggerName string) zapcore.Level {
candidate := loggerName + "."
for {
if lvl, ok := m.specs[candidate]; ok {
if lvl, ok := l.specs[candidate]; ok {
return lvl
}

idx := strings.LastIndex(candidate, ".")
if idx <= 0 {
return m.defaultLevel
return l.defaultLevel
}
candidate = candidate[:idx]
}
}

// cachedLevel attempts to retrieve the effective log level for a logger from the
// cache. If the logger is not found, ok will be false.
func (m *ModuleLevels) cachedLevel(loggerName string) (lvl zapcore.Level, ok bool) {
m.mutex.RLock()
level, ok := m.levelCache[loggerName]
m.mutex.RUnlock()
func (l *LoggerLevels) cachedLevel(loggerName string) (lvl zapcore.Level, ok bool) {
l.mutex.RLock()
level, ok := l.levelCache[loggerName]
l.mutex.RUnlock()
return level, ok
}

// Spec returns a normalized version of the active logging spec.
func (m *ModuleLevels) Spec() string {
m.mutex.RLock()
defer m.mutex.RUnlock()
func (l *LoggerLevels) Spec() string {
l.mutex.RLock()
defer l.mutex.RUnlock()

var fields []string
for k, v := range m.specs {
for k, v := range l.specs {
fields = append(fields, fmt.Sprintf("%s=%s", k, v))
}

sort.Strings(fields)
fields = append(fields, m.defaultLevel.String())
fields = append(fields, l.defaultLevel.String())

return strings.Join(fields, ":")
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"go.uber.org/zap/zapcore"
)

func TestModuleLevelsActivateSpec(t *testing.T) {
func TestLoggerLevelsActivateSpec(t *testing.T) {
var tests = []struct {
spec string
expectedLevels map[string]zapcore.Level
Expand Down Expand Up @@ -94,19 +94,19 @@ func TestModuleLevelsActivateSpec(t *testing.T) {

for _, tc := range tests {
t.Run(tc.spec, func(t *testing.T) {
ml := &flogging.ModuleLevels{}
ll := &flogging.LoggerLevels{}

err := ml.ActivateSpec(tc.spec)
err := ll.ActivateSpec(tc.spec)
assert.NoError(t, err)
assert.Equal(t, tc.expectedDefaultLevel, ml.DefaultLevel())
assert.Equal(t, tc.expectedDefaultLevel, ll.DefaultLevel())
for name, lvl := range tc.expectedLevels {
assert.Equal(t, lvl, ml.Level(name))
assert.Equal(t, lvl, ll.Level(name))
}
})
}
}

func TestModuleLevelsActivateSpecErrors(t *testing.T) {
func TestLoggerLevelsActivateSpecErrors(t *testing.T) {
var tests = []struct {
spec string
err error
Expand All @@ -120,19 +120,19 @@ func TestModuleLevelsActivateSpecErrors(t *testing.T) {
}
for _, tc := range tests {
t.Run(tc.spec, func(t *testing.T) {
ml := &flogging.ModuleLevels{}
err := ml.ActivateSpec("fatal:a=warn")
ll := &flogging.LoggerLevels{}
err := ll.ActivateSpec("fatal:a=warn")

err = ml.ActivateSpec(tc.spec)
err = ll.ActivateSpec(tc.spec)
assert.EqualError(t, err, tc.err.Error())

assert.Equal(t, zapcore.FatalLevel, ml.DefaultLevel(), "default should not change")
assert.Equal(t, zapcore.WarnLevel, ml.Level("a.b"), "log levels should not change")
assert.Equal(t, zapcore.FatalLevel, ll.DefaultLevel(), "default should not change")
assert.Equal(t, zapcore.WarnLevel, ll.Level("a.b"), "log levels should not change")
})
}
}

func TestModuleLevelSpec(t *testing.T) {
func TestSpec(t *testing.T) {
var tests = []struct {
input string
output string
Expand All @@ -148,10 +148,10 @@ func TestModuleLevelSpec(t *testing.T) {
}

for _, tc := range tests {
ml := &flogging.ModuleLevels{}
err := ml.ActivateSpec(tc.input)
ll := &flogging.LoggerLevels{}
err := ll.ActivateSpec(tc.input)
assert.NoError(t, err)

assert.Equal(t, tc.output, ml.Spec())
assert.Equal(t, tc.output, ll.Spec())
}
}
8 changes: 4 additions & 4 deletions common/flogging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Config struct {
// intended to bridge between the legacy logging infrastructure built around
// go-logging and the structured, level logging provided by zap.
type Logging struct {
*ModuleLevels
*LoggerLevels

mutex sync.RWMutex
encoding Encoding
Expand All @@ -61,7 +61,7 @@ func New(c Config) (*Logging, error) {
encoderConfig.NameKey = "name"

s := &Logging{
ModuleLevels: &ModuleLevels{
LoggerLevels: &LoggerLevels{
defaultLevel: defaultLevel,
},
encoderConfig: encoderConfig,
Expand All @@ -86,7 +86,7 @@ func (s *Logging) Apply(c Config) error {
c.LogSpec = "INFO"
}

err = s.ModuleLevels.ActivateSpec(c.LogSpec)
err = s.LoggerLevels.ActivateSpec(c.LogSpec)
if err != nil {
return err
}
Expand Down Expand Up @@ -198,7 +198,7 @@ func (s *Logging) ZapLogger(name string) *zap.Logger {
s.mutex.RLock()
core := &Core{
LevelEnabler: levelEnabler,
Levels: s.ModuleLevels,
Levels: s.LoggerLevels,
Encoders: map[Encoding]zapcore.Encoder{
JSON: zapcore.NewJSONEncoder(s.encoderConfig),
CONSOLE: fabenc.NewFormatEncoder(s.multiFormatter),
Expand Down
2 changes: 1 addition & 1 deletion core/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (s *ServerAdmin) GetModuleLogLevel(ctx context.Context, env *common.Envelop
if request == nil {
return nil, errors.New("request is nil")
}
logLevelString := flogging.GetModuleLevel(request.LogModule)
logLevelString := flogging.GetLoggerLevel(request.LogModule)
logResponse := &pb.LogLevelResponse{LogModule: request.LogModule, LogLevel: logLevelString}
return logResponse, nil
}
Expand Down

0 comments on commit 71e975e

Please sign in to comment.