Skip to content

Commit e0d4f3a

Browse files
authored
fix(apisix): log scope typo (#253)
1 parent c958716 commit e0d4f3a

File tree

6 files changed

+45
-46
lines changed

6 files changed

+45
-46
lines changed

apps/cli/src/command/utils.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { isObject, mapValues, unset } from 'lodash';
88
import path from 'node:path';
99
import pluralize from 'pluralize';
1010

11+
import { SignaleRendererOutputType } from '../utils/listr';
1112
import { KVConfiguration } from './typing';
1213

1314
export const loadBackend = (
@@ -306,7 +307,7 @@ export const recursiveReplaceEnvVars = (
306307
export const buildReqAndRespDebugOutput = (
307308
resp: AxiosResponse,
308309
desc?: string,
309-
scope = ['API7'],
310+
scope = ['APISIX'],
310311
) => {
311312
const capitalizeFirstLetter = (str: string) =>
312313
str.charAt(0).toUpperCase() + str.slice(1);
@@ -323,7 +324,7 @@ export const buildReqAndRespDebugOutput = (
323324
`${normalizeKey ? normalizeHeaderKey(key) : key}: ${key !== 'X-API-KEY' ? value : '*****'}\n`,
324325
);
325326
return JSON.stringify({
326-
type: 'debug',
327+
type: SignaleRendererOutputType.DEBUG,
327328
messages: [
328329
`${desc ?? ''}\n`, //TODO time consumption
329330
// request
@@ -344,23 +345,29 @@ export const addBackendEventListener = (
344345
backend: ADCSDK.Backend,
345346
task: ListrTaskWrapper<object, any, any>,
346347
) => {
348+
const metadata = backend.metadata();
349+
const logScope = metadata.logScope?.length > 0 ? metadata.logScope : ['ADC'];
347350
const sub1 = backend.on(
348351
ADCSDK.BackendEventType.AXIOS_DEBUG,
349352
({ response, description }) =>
350-
(task.output = buildReqAndRespDebugOutput(response, description)),
353+
(task.output = buildReqAndRespDebugOutput(
354+
response,
355+
description,
356+
logScope,
357+
)),
351358
);
352359
const sub2 = backend.on(ADCSDK.BackendEventType.TASK_START, ({ name }) => {
353360
task.output = JSON.stringify({
354-
type: 'ts',
361+
type: SignaleRendererOutputType.LISTR_TASK_START,
355362
messages: [name],
356-
scope: ['API7'],
363+
scope: logScope,
357364
});
358365
});
359366
const sub3 = backend.on(ADCSDK.BackendEventType.TASK_DONE, ({ name }) => {
360367
task.output = JSON.stringify({
361-
type: 'td',
368+
type: SignaleRendererOutputType.LISTR_TASK_COMPLETE,
362369
messages: [name],
363-
scope: ['API7'],
370+
scope: logScope,
364371
});
365372
});
366373
return () => {

apps/cli/src/utils/listr.ts

+11-38
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ export interface SignaleRendererOptions {
2121
scope?: Array<string>;
2222
}
2323

24+
export const SignaleRendererOutputType = {
25+
DEBUG: 'DEBUG',
26+
LISTR_TASK_START: 'LISTR_TASK_START',
27+
LISTR_TASK_COMPLETE: 'LISTR_TASK_COMPLETE',
28+
} as const;
29+
2430
export interface SignaleRendererOutput {
25-
type: 'debug' | 'ts' | 'td';
31+
type: keyof typeof SignaleRendererOutputType;
2632
scope?: Array<string>;
2733
messages: Array<unknown>;
2834
}
@@ -104,21 +110,21 @@ export class SignaleRenderer implements ListrRenderer {
104110
if (!output.type || !output.messages) return;
105111

106112
switch (output.type) {
107-
case 'ts': {
113+
case SignaleRendererOutputType.LISTR_TASK_START: {
108114
this.getScopedLogger({
109115
...rendererOptions,
110116
scope: output.scope ?? rendererOptions.scope,
111117
}).start(output.messages.join(''));
112118
break;
113119
}
114-
case 'td': {
120+
case SignaleRendererOutputType.LISTR_TASK_COMPLETE: {
115121
this.getScopedLogger({
116122
...rendererOptions,
117123
scope: output.scope ?? rendererOptions.scope,
118124
}).success(output.messages.join(''));
119125
break;
120126
}
121-
case 'debug': {
127+
case SignaleRendererOutputType.DEBUG: {
122128
if (output?.messages && rendererOptions?.verbose === 2) {
123129
this.getScopedLogger({
124130
...rendererOptions,
@@ -154,7 +160,7 @@ export class ListrOutputLogger implements Logger {
154160
if (opts?.showLogEntry && !opts?.showLogEntry({ message, ...kvs })) return;
155161

156162
this.task.output = JSON.stringify({
157-
type: 'debug',
163+
type: SignaleRendererOutputType.DEBUG,
158164
messages: [
159165
`${message}\n`,
160166
Object.entries(kvs)
@@ -163,37 +169,4 @@ export class ListrOutputLogger implements Logger {
163169
],
164170
} satisfies SignaleRendererOutput);
165171
}
166-
167-
axiosDebug(resp: AxiosResponse, desc?: string): void {
168-
const config = resp.config;
169-
170-
const capitalizeFirstLetter = (str: string) =>
171-
str.charAt(0).toUpperCase() + str.slice(1);
172-
173-
// NodeJS will not keep the response header in Xxx-Xxx format, correct it
174-
const normalizeHeaderKey = (key: string) =>
175-
key.split('-').map(capitalizeFirstLetter).join('-');
176-
177-
// Transforms HTTP headers to a single line of text formatting
178-
const transformHeaders = (headers: object, normalizeKey = false) =>
179-
Object.entries(headers).map(
180-
([key, value]) =>
181-
`${normalizeKey ? normalizeHeaderKey(key) : key}: ${key !== 'X-API-KEY' ? value : '*****'}\n`,
182-
);
183-
this.task.output = JSON.stringify({
184-
type: 'debug',
185-
messages: [
186-
`${desc ?? ''}\n`, //TODO time consumption
187-
// request
188-
`${config.method.toUpperCase()} ${axios.getUri(config)}\n`,
189-
...transformHeaders(config.headers),
190-
config?.data ? `\n${config.data}\n` : '',
191-
'\n',
192-
// response
193-
`${resp.status} ${resp.statusText}\n`,
194-
...transformHeaders(resp.headers, true),
195-
`${resp?.data ? `\n${JSON.stringify(resp.data)}` : ''}\n`,
196-
],
197-
} satisfies SignaleRendererOutput);
198-
}
199172
}

libs/backend-api7/src/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ export class BackendAPI7 implements ADCSDK.Backend {
6565
this.gatewayGroupName = opts.gatewayGroup;
6666
}
6767

68+
public metadata() {
69+
return {
70+
logScope: BackendAPI7.logScope,
71+
};
72+
}
73+
6874
public async ping() {
6975
await this.client.get('/api/gateway_groups');
7076
}

libs/backend-apisix/src/index.ts

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { Fetcher } from './fetcher';
1717
import { Operator } from './operator';
1818

1919
export class BackendAPISIX implements ADCSDK.Backend {
20+
private static logScope = ['APISIX'];
2021
private readonly client: Axios;
2122
private readonly subject = new Subject<ADCSDK.BackendEvent>();
2223

@@ -58,6 +59,12 @@ export class BackendAPISIX implements ADCSDK.Backend {
5859
this.client = axios.create(config);
5960
}
6061

62+
public metadata() {
63+
return {
64+
logScope: BackendAPISIX.logScope,
65+
};
66+
}
67+
6168
public async defaultValue() {
6269
return {};
6370
}

libs/converter-openapi/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class OpenAPIConverter implements ADCSDK.Converter {
166166

167167
private buildDebugOutput(messages: Array<string>) {
168168
return JSON.stringify({
169-
type: 'debug',
169+
type: 'DEBUG',
170170
messages,
171171
});
172172
}

libs/sdk/src/backend/index.ts

+6
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ export interface BackendSyncResult {
5959
error?: Error;
6060
}
6161

62+
export interface BackendMetadata {
63+
logScope: string[];
64+
}
65+
6266
export interface Backend {
67+
metadata: () => BackendMetadata;
68+
6369
ping: () => Promise<void>;
6470

6571
version: () => Promise<SemVer>;

0 commit comments

Comments
 (0)