This repository has been archived by the owner on Dec 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
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 #6 from MrAlias/log-level
Unify logging
- Loading branch information
Showing
15 changed files
with
238 additions
and
49 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
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
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,89 @@ | ||
package log | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/newrelic/newrelic-telemetry-sdk-go/telemetry" | ||
iLog "istio.io/pkg/log" | ||
) | ||
|
||
const ( | ||
// AdapterScopeName is the default logging scope name for this adapter. | ||
AdapterScopeName = "newrelic" | ||
defaultOutputLevel = ErrorLevel | ||
defaultStackTraceLevel = NoneLevel | ||
) | ||
|
||
var adapterScope = iLog.RegisterScope(AdapterScopeName, "New Relic adapter logging messages.", 0) | ||
|
||
func init() { | ||
adapterScope.SetOutputLevel(defaultOutputLevel.istioLevel()) | ||
adapterScope.SetStackTraceLevel(defaultStackTraceLevel.istioLevel()) | ||
|
||
// Configure the default Istio logger with our default options. | ||
// This ensures things like gRPC logging (which is overriden by the | ||
// Istio log package) is configured at the appropriate level. | ||
opts := iLog.DefaultOptions() | ||
opts.SetOutputLevel(iLog.DefaultScopeName, defaultOutputLevel.istioLevel()) | ||
opts.SetStackTraceLevel(iLog.DefaultScopeName, defaultStackTraceLevel.istioLevel()) | ||
_ = iLog.Configure(opts) | ||
} | ||
|
||
// Fatalf uses fmt.Sprintf to construct and log a message at fatal level. | ||
func Fatalf(template string, args ...interface{}) { | ||
adapterScope.Fatalf(template, args...) | ||
} | ||
|
||
// Errorf uses fmt.Sprintf to construct and log a message at error level. | ||
func Errorf(template string, args ...interface{}) { | ||
adapterScope.Errorf(template, args...) | ||
} | ||
|
||
// Warnf uses fmt.Sprintf to construct and log a message at warn level. | ||
func Warnf(template string, args ...interface{}) { | ||
adapterScope.Warnf(template, args...) | ||
} | ||
|
||
// Infof uses fmt.Sprintf to construct and log a message at info level. | ||
func Infof(template string, args ...interface{}) { | ||
adapterScope.Infof(template, args...) | ||
} | ||
|
||
// Debugf uses fmt.Sprintf to construct and log a message at debug level. | ||
func Debugf(template string, args ...interface{}) { | ||
adapterScope.Debugf(template, args...) | ||
} | ||
|
||
// SetOutputLevel adjusts the output level associated with the adapter scope. | ||
func SetOutputLevel(l Level) { | ||
adapterScope.SetOutputLevel(l.istioLevel()) | ||
} | ||
|
||
// SetStackTraceLevel adjusts the stack tracing level associated with the adapter scope. | ||
func SetStackTraceLevel(l Level) { | ||
adapterScope.SetStackTraceLevel(l.istioLevel()) | ||
} | ||
|
||
// telemetryLogger returns a suitable telemetry.Harvester logging function | ||
func telemetryLogger(logf func(string, ...interface{})) func(map[string]interface{}) { | ||
return func(fields map[string]interface{}) { | ||
if js, err := json.Marshal(fields); nil != err { | ||
logf("%s", err.Error()) | ||
} else { | ||
logf("%s", string(js)) | ||
} | ||
} | ||
} | ||
|
||
// HarvesterConfigFunc returns a configuration function for the telemetry Harvester initialization. | ||
// | ||
// There is not a one-to-one mapping of our/Istio logging levels to the telemetry package. | ||
// The mapping made here is that error logging is at the ErrorLevel, debug logging is at | ||
// the InfoLevel, and audit logging is at the DebugLevel. | ||
func HarvesterConfigFunc() func(*telemetry.Config) { | ||
return func(cfg *telemetry.Config) { | ||
cfg.ErrorLogger = telemetryLogger(adapterScope.Errorf) | ||
cfg.DebugLogger = telemetryLogger(adapterScope.Infof) | ||
cfg.AuditLogger = telemetryLogger(adapterScope.Debugf) | ||
} | ||
} |
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,86 @@ | ||
package log | ||
|
||
import ( | ||
"fmt" | ||
|
||
iLog "istio.io/pkg/log" | ||
) | ||
|
||
// Level of logging severity | ||
type Level int | ||
|
||
const ( | ||
// InvalidLevel represents an invalid logging level returned when parsing errors occur. | ||
InvalidLevel Level = iota | ||
|
||
// DebugLevel signifies all messages with debug level and above should be logged. | ||
DebugLevel | ||
|
||
// InfoLevel signifies all messages with info level and above should be logged. | ||
InfoLevel | ||
|
||
// WarnLevel signifies all messages with warn level and above should be logged. | ||
WarnLevel | ||
|
||
// ErrorLevel signifies all messages with error level and above should be logged. | ||
ErrorLevel | ||
|
||
// FatalLevel signifies only fatal level messages should be logged. | ||
FatalLevel | ||
|
||
// NoneLevel signifies no messages be logged. | ||
NoneLevel | ||
) | ||
|
||
var stringToLevel = map[string]Level{ | ||
"debug": DebugLevel, | ||
"info": InfoLevel, | ||
"warn": WarnLevel, | ||
"error": ErrorLevel, | ||
"fatal": FatalLevel, | ||
"none": NoneLevel, | ||
} | ||
|
||
var levelToString = map[Level]string{ | ||
DebugLevel: "debug", | ||
InfoLevel: "info", | ||
WarnLevel: "warn", | ||
ErrorLevel: "error", | ||
FatalLevel: "fatal", | ||
NoneLevel: "none", | ||
} | ||
|
||
var levelToIstioLevel = map[Level]iLog.Level{ | ||
DebugLevel: iLog.DebugLevel, | ||
InfoLevel: iLog.InfoLevel, | ||
WarnLevel: iLog.WarnLevel, | ||
ErrorLevel: iLog.ErrorLevel, | ||
FatalLevel: iLog.FatalLevel, | ||
NoneLevel: iLog.NoneLevel, | ||
} | ||
|
||
// ParseLevel interprets level name as a Level. | ||
func ParseLevel(name string) (Level, error) { | ||
if s, ok := stringToLevel[name]; ok { | ||
return s, nil | ||
} | ||
return InvalidLevel, fmt.Errorf("invalid log level: %s", name) | ||
} | ||
|
||
// String returns Level string representation. | ||
func (l Level) String() string { | ||
return levelToString[l] | ||
} | ||
|
||
func (l Level) istioLevel() iLog.Level { | ||
return levelToIstioLevel[l] | ||
} | ||
|
||
// Levels returns all valid level names. | ||
func Levels() []string { | ||
l := make([]string, 0, len(stringToLevel)) | ||
for k := range stringToLevel { | ||
l = append(l, k) | ||
} | ||
return l | ||
} |
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
Oops, something went wrong.