Context for tooling logs (#190766)

## Summary
Allows to set context for the tooling logger. The context would be
logged before the log output, like so:

```
info [My-Tool] Hello!
```

Split from: https://github.com/elastic/kibana/pull/190401

---------

Co-authored-by: Tiago Costa <tiago.costa@elastic.co>
This commit is contained in:
Alex Szabo 2024-08-21 20:47:29 +02:00 committed by GitHub
parent f3142bacae
commit 748326d1e2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 61 additions and 23 deletions

View file

@ -31,18 +31,24 @@ export interface RunOptions {
description?: string;
log?: {
defaultLevel?: LogLevel;
context?: string;
};
flags?: FlagOptions;
}
export async function run<T>(fn: RunFn<T>, options: RunOptions = {}): Promise<T | undefined> {
const flags = getFlags(process.argv.slice(2), options.flags, options.log?.defaultLevel);
const log = new ToolingLog({
level: pickLevelFromFlags(flags, {
default: options.log?.defaultLevel,
}),
writeTo: process.stdout,
});
const log = new ToolingLog(
{
level: pickLevelFromFlags(flags, {
default: options.log?.defaultLevel,
}),
writeTo: process.stdout,
},
{
context: options.log?.context,
}
);
const metrics = new Metrics(log);
const helpText = getHelp({

View file

@ -9,6 +9,7 @@ Array [
"bar",
"baz",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "debug",
@ -24,6 +25,7 @@ Array [
"args": Array [
[Error: error message],
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "error",
@ -34,6 +36,7 @@ Array [
"args": Array [
"string message",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "error",
@ -52,6 +55,7 @@ Array [
"args": Array [
"foo",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "debug",
@ -60,6 +64,7 @@ Array [
"args": Array [
"bar",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "info",
@ -68,6 +73,7 @@ Array [
"args": Array [
"baz",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "verbose",
@ -81,6 +87,7 @@ Array [
"args": Array [
"foo",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "debug",
@ -89,6 +96,7 @@ Array [
"args": Array [
"bar",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "info",
@ -97,6 +105,7 @@ Array [
"args": Array [
"baz",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "verbose",
@ -111,6 +120,7 @@ Array [
"args": Array [
"foo",
],
"context": undefined,
"indent": 1,
"source": undefined,
"type": "debug",
@ -121,6 +131,7 @@ Array [
"args": Array [
"bar",
],
"context": undefined,
"indent": 3,
"source": undefined,
"type": "debug",
@ -131,6 +142,7 @@ Array [
"args": Array [
"baz",
],
"context": undefined,
"indent": 6,
"source": undefined,
"type": "debug",
@ -141,6 +153,7 @@ Array [
"args": Array [
"box",
],
"context": undefined,
"indent": 4,
"source": undefined,
"type": "debug",
@ -151,6 +164,7 @@ Array [
"args": Array [
"foo",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "debug",
@ -168,6 +182,7 @@ Array [
"bar",
"baz",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "info",
@ -185,6 +200,7 @@ Array [
"bar",
"baz",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "success",
@ -202,6 +218,7 @@ Array [
"bar",
"baz",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "verbose",
@ -219,6 +236,7 @@ Array [
"bar",
"baz",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "warning",
@ -236,6 +254,7 @@ Array [
"bar",
"baz",
],
"context": undefined,
"indent": 0,
"source": undefined,
"type": "write",

View file

@ -20,4 +20,6 @@ export interface Message {
source?: string;
/** args passed to the logging method */
args: any[];
/** an identifier of the logging entity */
context?: string;
}

View file

@ -25,6 +25,13 @@ export interface ToolingLogOptions {
* writers on either log will update the other too.
*/
parent?: ToolingLog;
/**
* A string, conveniently the name of the script,
* that will be prepended to log messages.
* Can be useful to identify which entity is emitting the log.
*/
context?: string;
}
export class ToolingLog implements SomeDevLog {
@ -32,6 +39,7 @@ export class ToolingLog implements SomeDevLog {
private writers$: Rx.BehaviorSubject<Writer[]>;
private readonly written$: Rx.Subject<Message>;
private readonly type: string | undefined;
private readonly context: string | undefined;
constructor(writerConfig?: ToolingLogTextWriterConfig, options?: ToolingLogOptions) {
this.indentWidth$ = options?.parent ? options.parent.indentWidth$ : new Rx.BehaviorSubject(0);
@ -45,6 +53,7 @@ export class ToolingLog implements SomeDevLog {
this.written$ = options?.parent ? options.parent.written$ : new Rx.Subject();
this.type = options?.type;
this.context = options?.context;
}
/**
@ -93,31 +102,31 @@ export class ToolingLog implements SomeDevLog {
}
public verbose(...args: any[]) {
this.sendToWriters('verbose', args);
this.sendToWriters({ type: 'verbose', context: this.context }, args);
}
public debug(...args: any[]) {
this.sendToWriters('debug', args);
this.sendToWriters({ type: 'debug', context: this.context }, args);
}
public info(...args: any[]) {
this.sendToWriters('info', args);
this.sendToWriters({ type: 'info', context: this.context }, args);
}
public success(...args: any[]) {
this.sendToWriters('success', args);
this.sendToWriters({ type: 'success', context: this.context }, args);
}
public warning(...args: any[]) {
this.sendToWriters('warning', args);
this.sendToWriters({ type: 'warning', context: this.context }, args);
}
public error(error: Error | string) {
this.sendToWriters('error', [error]);
this.sendToWriters({ type: 'error', context: this.context }, [error]);
}
public write(...args: any[]) {
this.sendToWriters('write', args);
this.sendToWriters({ type: 'write' }, args);
}
public getWriters() {
@ -143,7 +152,7 @@ export class ToolingLog implements SomeDevLog {
});
}
private sendToWriters(type: MessageTypes, args: any[]) {
private sendToWriters({ type, context }: { type: MessageTypes; context?: string }, args: any[]) {
const indent = this.indentWidth$.getValue();
const writers = this.writers$.getValue();
const msg: Message = {
@ -151,6 +160,7 @@ export class ToolingLog implements SomeDevLog {
indent,
source: this.type,
args,
context,
};
let written = false;

View file

@ -26,16 +26,16 @@ export class ToolingLogCollectingWriter extends ToolingLogTextWriter {
}
/**
* Called by ToolingLog, extends messages with the source if message includes one.
* Called by ToolingLog, extends messages with the source and context if message include it.
*/
write(msg: Message) {
if (msg.source) {
return super.write({
...msg,
args: [`source[${msg.source}]`, ...msg.args],
});
}
const args = [
msg.source ? `source[${msg.source}]` : null,
msg.context ? `context[${msg.context}]` : null,
]
.filter(Boolean)
.concat(msg.args);
return super.write(msg);
return super.write({ ...msg, args });
}
}

View file

@ -103,7 +103,8 @@ export class ToolingLogTextWriter implements Writer {
}
}
const prefix = has(MSG_PREFIXES, msg.type) ? MSG_PREFIXES[msg.type] : '';
let prefix = has(MSG_PREFIXES, msg.type) ? MSG_PREFIXES[msg.type] : '';
prefix = msg.context ? prefix + `[${msg.context}] ` : prefix;
ToolingLogTextWriter.write(this.writeTo, prefix, msg);
return true;
}