CompositeLogger
Aggregates all loggers from component references under a single component.
Implements: Logger
Description
The CompositeLogger class allows you to aggregate loggers from component references into a single component.
Important points
- It allows to log messages and conveniently send them to multiple destinations.
References
- *:logger:*:*:1.0 - (optional) ILogger components to pass log messages
Constructors
NewCompositeLoggerFromReferences
Creates a new instance of the logger.
NewCompositeLoggerFromReferences(ctx context.Context, references refer.IReferences) *CompositeLogger
- ctx: context.Context - operation context.
- references: refer.IReferences - references to locate the component dependencies.
NewCompositeLogger
Creates a new instance of the logger.
NewCompositeLogger() *CompositeLogger
Methods
SetReferences
Sets references to dependent components.
(c *CompositeLogger) SetReferences(ctx context.Context, references refer.IReferences)
- ctx: context.Context - operation context.
- references: refer.IReferences - references to locate the component dependencies.
Write
Writes a log message to the logger destination.
(c *CompositeLogger) Write(ctx context.Context, level LevelType, context IContext, err error, message string)
- ctx: context.Context - operation context.
- level: LevelType - log level.
- context: IContext - (optional) a context to trace execution through a call chain.
- err: error - error object associated with this message.
- message: string - human-readable message to log.
Examples
type MyComponent {
_logger CompositeLogger
}
func (mc* MyComponent) Configure(ctx context.Context, config ConfigParams) {
mc._logger.Configure(ctx, config)
...
}
func (mc* MyComponent) SetReferences(ctx context.Context, references IReferences) {
mc._logger.SetReferences(ctx, references)
...
}
func (mc* MyComponent) myMethod(ctx context.Context, IContext context) {
mc._logger.Debug(ctx context.Context, context, "Called method mycomponent.mymethod");
...
}
var mc MyComponent = MyComponent{}
mc._logger = NewCompositeLogger()