opentelemetry-js
opentelemetry-js copied to clipboard
[Diag-Logger] Provide the ability to check if a specific diagnostic log level is enabled before calling and constructing DiagLog messages
trafficstars
Construction of Diagnostic log message strings is expensive
As part of the adding the diag.xxx messages as part of #1877 is was dicussed during the SIG that we should have some way to check whether a specific level is enabled before attempting to log a diagnostic message, especially when creating complex strings.
Splitting this out as a separate issue as this can be added after the API is stable.
Describe the solution you'd like
Provide a helper function or additional function on the DiagLogger to enable checking if called whether the message would be logged or dropped.
Possible options include
- New method on the interface, could be optional to avoid breaking API and it's only relevant for the LogLevel Diagnostic logger anyway. This would then also be available on the global
diaginstance.
interface DiagLogger {
...
isLevelEnabled: (level: DiagLogLevel) => bool
...
Helper Method check
isDiagLogLevelEnabled(logger: DiagLoger, level: DiagLogLevel): bool;
if (isDiagLogLevelEnabled(myLogger, DiagLogLevel.DEBUG)) {
myLogger.debug(...);
}
Or a Helper with a callback
diagLogIfEnabled(myLogger, DiagLogLevel.DEBUG, callback: (logger) -> void);
diagLogIfEnabled(myLogger, DiagLogLevel.DEBUG, (logger) => {
logger.debug(...);
});