2023-02-22 21:58:17 +00:00
|
|
|
// Copyright (c) 2021 Uber Technologies, Inc.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
package fxevent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2023-05-19 20:23:55 +00:00
|
|
|
"go.uber.org/zap/zapcore"
|
2023-02-22 21:58:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ZapLogger is an Fx event logger that logs events to Zap.
|
|
|
|
type ZapLogger struct {
|
|
|
|
Logger *zap.Logger
|
2023-05-19 20:23:55 +00:00
|
|
|
|
|
|
|
logLevel zapcore.Level // default: zapcore.InfoLevel
|
|
|
|
errorLevel *zapcore.Level
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ Logger = (*ZapLogger)(nil)
|
|
|
|
|
2023-05-19 20:23:55 +00:00
|
|
|
// UseErrorLevel sets the level of error logs emitted by Fx to level.
|
|
|
|
func (l *ZapLogger) UseErrorLevel(level zapcore.Level) {
|
|
|
|
l.errorLevel = &level
|
|
|
|
}
|
|
|
|
|
|
|
|
// UseLogLevel sets the level of non-error logs emitted by Fx to level.
|
|
|
|
func (l *ZapLogger) UseLogLevel(level zapcore.Level) {
|
|
|
|
l.logLevel = level
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *ZapLogger) logEvent(msg string, fields ...zap.Field) {
|
|
|
|
l.Logger.Log(l.logLevel, msg, fields...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *ZapLogger) logError(msg string, fields ...zap.Field) {
|
|
|
|
lvl := zapcore.ErrorLevel
|
|
|
|
if l.errorLevel != nil {
|
|
|
|
lvl = *l.errorLevel
|
|
|
|
}
|
|
|
|
l.Logger.Log(lvl, msg, fields...)
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
// LogEvent logs the given event to the provided Zap logger.
|
|
|
|
func (l *ZapLogger) LogEvent(event Event) {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *OnStartExecuting:
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("OnStart hook executing",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.String("callee", e.FunctionName),
|
|
|
|
zap.String("caller", e.CallerName),
|
|
|
|
)
|
|
|
|
case *OnStartExecuted:
|
|
|
|
if e.Err != nil {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("OnStart hook failed",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.String("callee", e.FunctionName),
|
|
|
|
zap.String("caller", e.CallerName),
|
|
|
|
zap.Error(e.Err),
|
|
|
|
)
|
|
|
|
} else {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("OnStart hook executed",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.String("callee", e.FunctionName),
|
|
|
|
zap.String("caller", e.CallerName),
|
|
|
|
zap.String("runtime", e.Runtime.String()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
case *OnStopExecuting:
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("OnStop hook executing",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.String("callee", e.FunctionName),
|
|
|
|
zap.String("caller", e.CallerName),
|
|
|
|
)
|
|
|
|
case *OnStopExecuted:
|
|
|
|
if e.Err != nil {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("OnStop hook failed",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.String("callee", e.FunctionName),
|
|
|
|
zap.String("caller", e.CallerName),
|
|
|
|
zap.Error(e.Err),
|
|
|
|
)
|
|
|
|
} else {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("OnStop hook executed",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.String("callee", e.FunctionName),
|
|
|
|
zap.String("caller", e.CallerName),
|
|
|
|
zap.String("runtime", e.Runtime.String()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
case *Supplied:
|
2023-05-19 20:23:55 +00:00
|
|
|
if e.Err != nil {
|
|
|
|
l.logError("error encountered while applying options",
|
|
|
|
zap.String("type", e.TypeName),
|
2023-08-22 10:32:01 +00:00
|
|
|
zap.Strings("stacktrace", e.StackTrace),
|
2024-05-15 23:15:00 +00:00
|
|
|
zap.Strings("moduletrace", e.ModuleTrace),
|
2023-05-19 20:23:55 +00:00
|
|
|
moduleField(e.ModuleName),
|
|
|
|
zap.Error(e.Err))
|
|
|
|
} else {
|
|
|
|
l.logEvent("supplied",
|
|
|
|
zap.String("type", e.TypeName),
|
2023-08-22 10:32:01 +00:00
|
|
|
zap.Strings("stacktrace", e.StackTrace),
|
2024-05-15 23:15:00 +00:00
|
|
|
zap.Strings("moduletrace", e.ModuleTrace),
|
2023-05-19 20:23:55 +00:00
|
|
|
moduleField(e.ModuleName),
|
|
|
|
)
|
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
case *Provided:
|
|
|
|
for _, rtype := range e.OutputTypeNames {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("provided",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.String("constructor", e.ConstructorName),
|
2023-08-22 10:32:01 +00:00
|
|
|
zap.Strings("stacktrace", e.StackTrace),
|
2024-05-15 23:15:00 +00:00
|
|
|
zap.Strings("moduletrace", e.ModuleTrace),
|
2023-02-22 21:58:17 +00:00
|
|
|
moduleField(e.ModuleName),
|
|
|
|
zap.String("type", rtype),
|
2023-05-19 20:23:55 +00:00
|
|
|
maybeBool("private", e.Private),
|
2023-02-22 21:58:17 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if e.Err != nil {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("error encountered while applying options",
|
2023-02-22 21:58:17 +00:00
|
|
|
moduleField(e.ModuleName),
|
2023-08-22 10:32:01 +00:00
|
|
|
zap.Strings("stacktrace", e.StackTrace),
|
2024-05-15 23:15:00 +00:00
|
|
|
zap.Strings("moduletrace", e.ModuleTrace),
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.Error(e.Err))
|
|
|
|
}
|
|
|
|
case *Replaced:
|
|
|
|
for _, rtype := range e.OutputTypeNames {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("replaced",
|
2023-08-22 10:32:01 +00:00
|
|
|
zap.Strings("stacktrace", e.StackTrace),
|
2024-05-15 23:15:00 +00:00
|
|
|
zap.Strings("moduletrace", e.ModuleTrace),
|
2023-02-22 21:58:17 +00:00
|
|
|
moduleField(e.ModuleName),
|
|
|
|
zap.String("type", rtype),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if e.Err != nil {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("error encountered while replacing",
|
2023-08-22 10:32:01 +00:00
|
|
|
zap.Strings("stacktrace", e.StackTrace),
|
2024-05-15 23:15:00 +00:00
|
|
|
zap.Strings("moduletrace", e.ModuleTrace),
|
2023-02-22 21:58:17 +00:00
|
|
|
moduleField(e.ModuleName),
|
|
|
|
zap.Error(e.Err))
|
|
|
|
}
|
|
|
|
case *Decorated:
|
|
|
|
for _, rtype := range e.OutputTypeNames {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("decorated",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.String("decorator", e.DecoratorName),
|
2023-08-22 10:32:01 +00:00
|
|
|
zap.Strings("stacktrace", e.StackTrace),
|
2024-05-15 23:15:00 +00:00
|
|
|
zap.Strings("moduletrace", e.ModuleTrace),
|
2023-02-22 21:58:17 +00:00
|
|
|
moduleField(e.ModuleName),
|
|
|
|
zap.String("type", rtype),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if e.Err != nil {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("error encountered while applying options",
|
2023-08-22 10:32:01 +00:00
|
|
|
zap.Strings("stacktrace", e.StackTrace),
|
2024-05-15 23:15:00 +00:00
|
|
|
zap.Strings("moduletrace", e.ModuleTrace),
|
2023-02-22 21:58:17 +00:00
|
|
|
moduleField(e.ModuleName),
|
|
|
|
zap.Error(e.Err))
|
|
|
|
}
|
2023-08-22 10:32:01 +00:00
|
|
|
case *Run:
|
|
|
|
if e.Err != nil {
|
|
|
|
l.logError("error returned",
|
|
|
|
zap.String("name", e.Name),
|
|
|
|
zap.String("kind", e.Kind),
|
|
|
|
moduleField(e.ModuleName),
|
|
|
|
zap.Error(e.Err),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
l.logEvent("run",
|
|
|
|
zap.String("name", e.Name),
|
|
|
|
zap.String("kind", e.Kind),
|
|
|
|
moduleField(e.ModuleName),
|
|
|
|
)
|
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
case *Invoking:
|
|
|
|
// Do not log stack as it will make logs hard to read.
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("invoking",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.String("function", e.FunctionName),
|
|
|
|
moduleField(e.ModuleName),
|
|
|
|
)
|
|
|
|
case *Invoked:
|
|
|
|
if e.Err != nil {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("invoke failed",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.Error(e.Err),
|
|
|
|
zap.String("stack", e.Trace),
|
|
|
|
zap.String("function", e.FunctionName),
|
|
|
|
moduleField(e.ModuleName),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
case *Stopping:
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("received signal",
|
2023-02-22 21:58:17 +00:00
|
|
|
zap.String("signal", strings.ToUpper(e.Signal.String())))
|
|
|
|
case *Stopped:
|
|
|
|
if e.Err != nil {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("stop failed", zap.Error(e.Err))
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
|
|
|
case *RollingBack:
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("start failed, rolling back", zap.Error(e.StartErr))
|
2023-02-22 21:58:17 +00:00
|
|
|
case *RolledBack:
|
|
|
|
if e.Err != nil {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("rollback failed", zap.Error(e.Err))
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
|
|
|
case *Started:
|
|
|
|
if e.Err != nil {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("start failed", zap.Error(e.Err))
|
2023-02-22 21:58:17 +00:00
|
|
|
} else {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("started")
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
|
|
|
case *LoggerInitialized:
|
|
|
|
if e.Err != nil {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logError("custom logger initialization failed", zap.Error(e.Err))
|
2023-02-22 21:58:17 +00:00
|
|
|
} else {
|
2023-05-19 20:23:55 +00:00
|
|
|
l.logEvent("initialized custom fxevent.Logger", zap.String("function", e.ConstructorName))
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func moduleField(name string) zap.Field {
|
|
|
|
if len(name) == 0 {
|
|
|
|
return zap.Skip()
|
|
|
|
}
|
|
|
|
return zap.String("module", name)
|
|
|
|
}
|
2023-05-19 20:23:55 +00:00
|
|
|
|
|
|
|
func maybeBool(name string, b bool) zap.Field {
|
|
|
|
if b {
|
|
|
|
return zap.Bool(name, true)
|
|
|
|
}
|
|
|
|
return zap.Skip()
|
|
|
|
}
|