[Kibana logging system] Add conditional evaluation based on level for logging APIs (#187225)

## Summary

*(Yeah, the title is pretty bad I apologize, I couldn't find something
sexy. OTOH, "sexy" and "logging" are usually antonyms, like "sport car"
and "fiat panda", or "server language" and "javascript")*

### 1. Provide a more developer-friendly alternative to
`Logger.isLevelEnabled`.


**With `isLevelEnabled`**
```ts
if(logger.isLevelEnabled('info')) {
  const message = someExpensiveMessageProbablyBasedOnJsonStringifyOrSomething(); 
  logger.info(message);
}
```

**With this PR:**
```ts
logger.info(() => someExpensiveMessageProbablyBasedOnJsonStringifyOrSomething());
``` 

### 2. Adapt calls to `log.debug` (arguably) costly to use this syntax

Aka any call relying on `JSON.stringify` or function calls.

I used the new syntax for those, except when the tests were too
complicated to fix or when the code did not allow it (e.g. untyped let
variables infered from return from assignations don't play well with
closures)
This commit is contained in:
Pierre Gayvallet 2024-07-08 15:53:02 +02:00 committed by GitHub
parent a711efa1b2
commit b6fcfac9c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
116 changed files with 812 additions and 483 deletions

View file

@ -10,8 +10,12 @@ import { Logger } from '@kbn/logging';
import { Log } from './log';
export const convertToLogger = (cliLog: Log): Logger => {
const getErrorMessage = (msgOrError: string | Error): string => {
return typeof msgOrError === 'string' ? msgOrError : msgOrError.message;
const getErrorMessage = (msgOrError: string | (() => string) | Error): string => {
return typeof msgOrError === 'function'
? msgOrError()
: typeof msgOrError === 'string'
? msgOrError
: msgOrError.message;
};
const adapter: Logger = {