-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #366 from Icinga/child-loggers
Child loggers
- Loading branch information
Showing
7 changed files
with
195 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,28 @@ | ||
# This is the configuration file for Icinga DB. | ||
|
||
database: | ||
host: icingadb | ||
port: 3306 | ||
database: icingadb | ||
user: icingadb | ||
password: icingadb | ||
|
||
redis: | ||
address: redis:6380 | ||
|
||
logging: | ||
# Default logging level. Can be set to 'fatal', 'error', 'warning', 'info' or 'debug'. | ||
# If not set, defaults to 'info'. | ||
level: | ||
|
||
# Map of component-logging level pairs to define a different log level than the default value for each component. | ||
options: | ||
database: | ||
redis: | ||
heartbeat: | ||
high-availability: | ||
config-sync: | ||
history-sync: | ||
runtime-updates: | ||
overdue-sync: | ||
dump-signals: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package logging | ||
|
||
import ( | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"os" | ||
"sync" | ||
) | ||
|
||
// Logging implements access to a default logger and named child loggers. | ||
// Log levels can be configured per named child via Options which, if not configured, | ||
// fall back on a default log level. | ||
type Logging struct { | ||
level zap.AtomicLevel | ||
logger *zap.SugaredLogger | ||
// encoder defines the zapcore.Encoder, | ||
// which is used to create the default logger and the child loggers | ||
encoder zapcore.Encoder | ||
// syncer defines the zapcore.WriterSyncer, | ||
// which is used to create the default logger and the child loggers | ||
syncer zapcore.WriteSyncer | ||
mu sync.Mutex | ||
loggers map[string]*zap.SugaredLogger | ||
options Options | ||
} | ||
|
||
// defaultEncConfig stores default zapcore.EncoderConfig for this package. | ||
var defaultEncConfig = zapcore.EncoderConfig{ | ||
TimeKey: "ts", | ||
LevelKey: "level", | ||
NameKey: "logger", | ||
CallerKey: "caller", | ||
MessageKey: "msg", | ||
StacktraceKey: "stacktrace", | ||
LineEnding: zapcore.DefaultLineEnding, | ||
EncodeLevel: zapcore.CapitalLevelEncoder, | ||
EncodeTime: zapcore.ISO8601TimeEncoder, | ||
EncodeDuration: zapcore.StringDurationEncoder, | ||
EncodeCaller: zapcore.ShortCallerEncoder, | ||
} | ||
|
||
// Options define child loggers with their desired log level. | ||
type Options map[string]zapcore.Level | ||
|
||
// NewLogging takes log level for default logger, output where log messages are written to | ||
// and options having log levels for named child loggers and initializes a new Logging. | ||
func NewLogging(level zapcore.Level, options Options) (*Logging, error) { | ||
atom := zap.NewAtomicLevelAt(level) | ||
|
||
encoder := zapcore.NewConsoleEncoder(defaultEncConfig) | ||
syncer := zapcore.Lock(os.Stderr) | ||
|
||
logger := zap.New(zapcore.NewCore( | ||
encoder, | ||
syncer, | ||
atom, | ||
)) | ||
|
||
return &Logging{ | ||
level: atom, | ||
logger: logger.Sugar(), | ||
encoder: encoder, | ||
syncer: syncer, | ||
loggers: map[string]*zap.SugaredLogger{}, | ||
options: options, | ||
}, | ||
nil | ||
} | ||
|
||
// GetChildLogger returns a named child logger. | ||
// Log levels for named child loggers are obtained from the logging options and, if not found, | ||
// set to the default log level. | ||
func (l *Logging) GetChildLogger(name string) *zap.SugaredLogger { | ||
l.mu.Lock() | ||
defer l.mu.Unlock() | ||
|
||
if logger, ok := l.loggers[name]; ok { | ||
return logger | ||
} | ||
|
||
if level, found := l.options[name]; found { | ||
atom := zap.NewAtomicLevelAt(level) | ||
|
||
logger := l.logger.Desugar().WithOptions( | ||
zap.WrapCore(func(c zapcore.Core) zapcore.Core { | ||
return zapcore.NewCore( | ||
l.encoder, | ||
l.syncer, | ||
atom, | ||
) | ||
})).Sugar().Named(name) | ||
|
||
l.loggers[name] = logger | ||
|
||
return logger | ||
} | ||
|
||
logger := l.logger.Named(name) | ||
l.loggers[name] = logger | ||
|
||
return logger | ||
} | ||
|
||
// GetLogger returns the default logger. | ||
func (l *Logging) GetLogger() *zap.SugaredLogger { | ||
return l.logger | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/icinga/icingadb/internal/logging" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
// Logging defines Logger configuration. | ||
type Logging struct { | ||
// zapcore.Level at 0 is for info level. | ||
Level zapcore.Level `yaml:"level" default:"0"` | ||
|
||
logging.Options `yaml:"options"` | ||
} |