-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added boilerplate for service logger configuration
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), | ||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
### Added | ||
- Nothing | ||
|
||
### Changed | ||
- Nothing | ||
|
||
### Deprecated | ||
- Nothing | ||
|
||
### Removed | ||
- Nothing | ||
|
||
### Fixed | ||
- Nothing | ||
|
||
### Security | ||
- Nothing | ||
|
||
## [1.3.0] - 2019-02-27 | ||
### Added | ||
- `ConfigureDefaultLogger` boilerplate for logger configuration | ||
- `measuredLoggingHandler` to wrap loggers with metrics | ||
- `CHANGELOG.md`: this file |
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,55 @@ | ||
package log | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// Config defines the logging configuration | ||
type Config struct { | ||
Level string `default:"info"` | ||
Output string `default:"stdout"` | ||
} | ||
|
||
// ConfigureDefaultLogger configures loggers for your service, optionally adding log message counters with your favorite | ||
// metrics system | ||
func ConfigureDefaultLogger(name string, cfg Config, logCounters ...CountLogMessage) { | ||
if logLevel, ok := logLevelMap[cfg.Level]; ok { | ||
SetLevel(logLevel) // This sets the default level for all future | ||
DefaultLevel = logLevel | ||
} else { | ||
Warningf("Unknown log level configured: %s", cfg.Level) | ||
} | ||
|
||
var handler Handler = NewFileHandler(getLoggerOutput(cfg.Output)) | ||
|
||
if len(logCounters) > 0 { | ||
handler = &metricsAgentLoggingHandler{ | ||
Handler: handler, | ||
logCounters: logCounters, | ||
} | ||
} | ||
handler.SetFormatter(DefaultFormatter) | ||
|
||
logger := NewLogger(name) | ||
if cfg.Level != logLevelDebug { | ||
logger = NoDebugLogger{ | ||
Logger: logger, | ||
} | ||
} | ||
logger.SetHandler(handler) | ||
|
||
DefaultLogger = logger | ||
Infof("Configured default logger %s with log level %s", name, cfg.Level) | ||
} | ||
|
||
func getLoggerOutput(outputName string) *os.File { | ||
switch outputName { | ||
case "stdout": | ||
return os.Stdout | ||
case "stderr": | ||
return os.Stderr | ||
default: | ||
Warningf("Unknown logger output defined in the config: '%s'", outputName) | ||
return os.Stderr | ||
} | ||
} |
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,21 @@ | ||
package log | ||
|
||
// CountLogMessage offers a method to count log messages logged by the logger | ||
type CountLogMessage func(level string) | ||
|
||
type metricsAgentLoggingHandler struct { | ||
Handler | ||
logCounters []CountLogMessage | ||
} | ||
|
||
// Handle decorates Handler's Handle method counting the log messages | ||
func (h *metricsAgentLoggingHandler) Handle(record *Record) { | ||
level := "unknown" | ||
if levelName, ok := logLevelNameMap[record.Level]; ok { | ||
level = levelName | ||
} | ||
for _, countLogMessage := range h.logCounters { | ||
countLogMessage(level) | ||
} | ||
h.Handler.Handle(record) | ||
} |