package fxevent
Import Path
go.uber.org/fx/fxevent (on go.dev)
Dependency Relation
imports 10 packages, and imported by 4 packages
Involved Source Files
console.go
Package fxevent defines a means of changing how Fx logs internal events.
# Changing the Logger
By default, the [ConsoleLogger] is used, writing readable logs to stderr.
You can use the fx.WithLogger option to change this behavior
by providing a constructor that returns an alternative implementation
of the [Logger] interface.
fx.WithLogger(func(cfg *Config) Logger {
return &MyFxLogger{...}
})
Because WithLogger accepts a constructor,
you can pull any other types and values from inside the container,
allowing use of the same logger that your application uses.
For example, if you're using Zap inside your application,
you can use the [ZapLogger] implementation of the interface.
fx.New(
fx.Provide(
zap.NewProduction, // provide a *zap.Logger
),
fx.WithLogger(
func(log *zap.Logger) fxevent.Logger {
return &fxevent.ZapLogger{Logger: log}
},
),
)
# Implementing a Custom Logger
To implement a custom logger, you need to implement the [Logger] interface.
The Logger.LogEvent method accepts an [Event] object.
[Event] is a union type that represents all the different events that Fx can emit.
You can use a type switch to handle each event type.
See 'event.go' for a list of all the possible events.
func (l *MyFxLogger) LogEvent(e fxevent.Event) {
switch e := e.(type) {
case *fxevent.OnStartExecuting:
// ...
// ...
}
}
The events contain enough information for observability and debugging purposes.
If you need more information in them,
feel free to open an issue to discuss the addition.
event.go
logger.go
slog.go
zap.go
Package-Level Type Names (total 23)
BeforeRun is emitted before a constructor, decorator, or supply/replace stub is run by Fx.
When complete, a Run will be emitted.
Kind indicates which Fx option was used to pass along the function.
It is either "provide", "decorate", "supply", or "replace".
ModuleName is the name of the module in which the function belongs.
Name is the name of the function that will be run.
*BeforeRun : Event
ConsoleLogger is an Fx event logger that attempts to write human-readable
messages to the console.
Use this during development.
W io.Writer
LogEvent logs the given event to the provided Zap logger.
*ConsoleLogger : Logger
Decorated is emitted when a decorator is executed in Fx.
DecoratorName is the name of the decorator function that was
provided to Fx.
Err is non-nil if we failed to run this decorator.
ModuleName is the name of the module in which the value was added to.
ModuleTrace contains the module locations through which this value was added.
OutputTypeNames is a list of names of types that are decorated by
this decorator.
StackTrace is the stack trace of where the decorator was given to Fx.
*Decorated : Event
Event defines an event emitted by fx.
*BeforeRun
*Decorated
*Invoked
*Invoking
*LoggerInitialized
*OnStartExecuted
*OnStartExecuting
*OnStopExecuted
*OnStopExecuting
*Provided
*Replaced
*RolledBack
*RollingBack
*Run
*Started
*Stopped
*Stopping
*Supplied
func (*ConsoleLogger).LogEvent(event Event)
func Logger.LogEvent(Event)
func (*SlogLogger).LogEvent(event Event)
func (*ZapLogger).LogEvent(event Event)
func go.uber.org/fx/internal/fxlog.(*Spy).LogEvent(event Event)
Invoked is emitted after we invoke a function specified with fx.Invoke,
whether it succeeded or failed.
Err is non-nil if the function failed to execute.
Functionname is the name of the function that was invoked.
ModuleName is the name of the module in which the value was added to.
Trace records information about where the fx.Invoke call was made.
Note that this is NOT a stack trace of the error itself.
*Invoked : Event
Invoking is emitted before we invoke a function specified with fx.Invoke.
FunctionName is the name of the function that will be invoked.
ModuleName is the name of the module in which the value was added to.
*Invoking : Event
Logger defines interface used for logging.
LogEvent is called when a logging event is emitted.
*ConsoleLogger
*SlogLogger
*ZapLogger
*go.uber.org/fx/internal/fxlog.Spy
func go.uber.org/fx/internal/fxlog.DefaultLogger(w io.Writer) Logger
func go.uber.org/fx/internal/lifecycle.New(logger Logger, clock fxclock.Clock) *lifecycle.Lifecycle
LoggerInitialized is emitted when a logger supplied with fx.WithLogger is
instantiated, or if it fails to instantiate.
ConstructorName is the name of the constructor that builds this
logger.
Err is non-nil if the logger failed to build.
*LoggerInitialized : Event
OnStartExecuted is emitted after an OnStart hook has been executed.
CallerName is the name of the function that scheduled the hook for
execution.
Err is non-nil if the hook failed to execute.
FunctionName is the name of the function that was executed.
Method specifies the kind of the hook. This is one of "OnStart" and
"OnStop".
Runtime specifies how long it took to run this hook.
*OnStartExecuted : Event
OnStartExecuting is emitted before an OnStart hook is executed.
CallerName is the name of the function that scheduled the hook for
execution.
FunctionName is the name of the function that will be executed.
*OnStartExecuting : Event
OnStopExecuted is emitted after an OnStop hook has been executed.
CallerName is the name of the function that scheduled the hook for
execution.
Err is non-nil if the hook failed to execute.
FunctionName is the name of the function that was executed.
Runtime specifies how long it took to run this hook.
*OnStopExecuted : Event
OnStopExecuting is emitted before an OnStop hook is executed.
CallerName is the name of the function that scheduled the hook for
execution.
FunctionName is the name of the function that will be executed.
*OnStopExecuting : Event
Provided is emitted when a constructor is provided to Fx.
ConstructorName is the name of the constructor that was provided to
Fx.
Err is non-nil if we failed to provide this constructor.
ModuleName is the name of the module in which the constructor was
provided to.
ModuleTrace contains the module locations through which this was provided to Fx.
OutputTypeNames is a list of names of types that are produced by
this constructor.
Private denotes whether the provided constructor is a [Private] constructor.
StackTrace is the stack trace of where the constructor was provided to Fx.
*Provided : Event
Replaced is emitted when a value replaces a type in Fx.
Err is non-nil if we failed to supply the value.
ModuleName is the name of the module in which the value was added to.
ModuleTrace contains the module locations through which this value was added.
OutputTypeNames is a list of names of types that were replaced.
StackTrace is the stack trace of the call to Replace.
*Replaced : Event
RolledBack is emitted after a service has been rolled back, whether it
succeeded or not.
Err error
*RolledBack : Event
RollingBack is emitted when the application failed to start up due to an
error, and is being rolled back.
StartErr is the error that caused this rollback.
*RollingBack : Event
Run is emitted after a constructor, decorator, or supply/replace stub is run by Fx.
Err is non-nil if the function returned an error.
If fx.RecoverFromPanics is used, this will include panics.
Kind indicates which Fx option was used to pass along the function.
It is either "provide", "decorate", "supply", or "replace".
ModuleName is the name of the module in which the function belongs.
Name is the name of the function that was run.
Runtime specifies how long it took to run this function.
*Run : Event
SlogLogger an Fx event logger that logs events using a slog logger.
Logger *slog.Logger
LogEvent logs the given event to the provided Zap logger.
UseContext sets the context that will be used when logging to slog.
UseErrorLevel sets the level of error logs emitted by Fx to level.
UseLogLevel sets the level of non-error logs emitted by Fx to level.
*SlogLogger : Logger
Started is emitted when an application is started successfully and/or it
errored.
Err error
*Started : Event
Stopped is emitted when the application has finished shutting down, whether
successfully or not.
Err error
*Stopped : Event
Stopping is emitted when the application receives a signal to shut down
after starting. This may happen with fx.Shutdowner or by sending a signal to
the application on the command line.
Signal is the signal that caused this shutdown.
*Stopping : Event
Supplied is emitted after a value is added with fx.Supply.
Err is non-nil if we failed to supply the value.
ModuleName is the name of the module in which the value was added to.
ModuleTrace contains the module locations through which this value was added.
StackTrace is the stack trace of the call to Supply.
TypeName is the name of the type of value that was added.
*Supplied : Event
Package-Level Variables (only one)
NopLogger is an Fx event logger that ignores all messages.
![]() |
The pages are generated with Golds v0.8.2. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |