Skip to content

Commit db4cf48

Browse files
authored
feat(javascript): add logger-console package from v4 (#3823)
1 parent ab1b02f commit db4cf48

28 files changed

+364
-19
lines changed
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1-
import { test, expect } from 'vitest';
1+
/* eslint no-console: 0 */
22

3-
import { algoliasearch, apiClientVersion } from '../builds/browser';
3+
import { vi, test, expect } from 'vitest';
44

5-
const client = algoliasearch('APP_ID', 'API_KEY');
5+
import { LogLevelEnum } from '../../client-common/src/types';
6+
import { createConsoleLogger } from '../../logger-console/src/logger';
7+
import { algoliasearch, apiClientVersion } from '../builds/browser';
68

79
test('sets the ua', () => {
10+
const client = algoliasearch('APP_ID', 'API_KEY');
11+
812
expect(client.transporter.algoliaAgent).toEqual({
913
add: expect.any(Function),
1014
value: expect.stringContaining(
1115
`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Browser`,
1216
),
1317
});
1418
});
19+
20+
test('with logger', () => {
21+
vi.spyOn(console, 'debug');
22+
vi.spyOn(console, 'info');
23+
vi.spyOn(console, 'error');
24+
25+
const client = algoliasearch('APP_ID', 'API_KEY', {
26+
logger: createConsoleLogger(LogLevelEnum.Debug),
27+
});
28+
29+
expect(async () => {
30+
await client.setSettings({ indexName: 'foo', indexSettings: {} });
31+
expect(console.debug).toHaveBeenCalledTimes(1);
32+
expect(console.info).toHaveBeenCalledTimes(1);
33+
expect(console.error).toHaveBeenCalledTimes(1);
34+
}).not.toThrow();
35+
});

clients/algoliasearch-client-javascript/packages/algoliasearch/__tests__/algoliasearch.common.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ describe('api', () => {
103103
url: 'APP_ID-2.algolianet.com',
104104
},
105105
]),
106+
logger: {
107+
debug: expect.any(Function),
108+
error: expect.any(Function),
109+
info: expect.any(Function),
110+
},
106111
hostsCache: {
107112
clear: expect.any(Function),
108113
delete: expect.any(Function),
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,42 @@
1-
import { test, expect } from 'vitest';
1+
/* eslint no-console: 0 */
22

3-
import { algoliasearch, apiClientVersion } from '../builds/fetch';
3+
import { vi, test, expect } from 'vitest';
44

5-
const client = algoliasearch('APP_ID', 'API_KEY');
5+
import { LogLevelEnum } from '../../client-common/src/types';
6+
import { createConsoleLogger } from '../../logger-console/src/logger';
7+
import { algoliasearch, apiClientVersion } from '../builds/fetch';
68

79
test('sets the ua', () => {
10+
const client = algoliasearch('APP_ID', 'API_KEY');
811
expect(client.transporter.algoliaAgent).toEqual({
912
add: expect.any(Function),
1013
value: expect.stringContaining(`Algolia for JavaScript (${apiClientVersion}); Search (${apiClientVersion}); Fetch`),
1114
});
1215
});
1316

1417
test('forwards node search helpers', () => {
18+
const client = algoliasearch('APP_ID', 'API_KEY');
1519
expect(client.generateSecuredApiKey).not.toBeUndefined();
1620
expect(client.getSecuredApiKeyRemainingValidity).not.toBeUndefined();
1721
expect(() => {
1822
const resp = client.generateSecuredApiKey({ parentApiKey: 'foo', restrictions: { validUntil: 200 } });
1923
client.getSecuredApiKeyRemainingValidity({ securedApiKey: resp });
2024
}).not.toThrow();
2125
});
26+
27+
test('with logger', () => {
28+
vi.spyOn(console, 'debug');
29+
vi.spyOn(console, 'info');
30+
vi.spyOn(console, 'error');
31+
32+
const client = algoliasearch('APP_ID', 'API_KEY', {
33+
logger: createConsoleLogger(LogLevelEnum.Debug),
34+
});
35+
36+
expect(async () => {
37+
await client.setSettings({ indexName: 'foo', indexSettings: {} });
38+
expect(console.debug).toHaveBeenCalledTimes(1);
39+
expect(console.info).toHaveBeenCalledTimes(1);
40+
expect(console.error).toHaveBeenCalledTimes(1);
41+
}).not.toThrow();
42+
});
Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import { test, expect } from 'vitest';
1+
/* eslint no-console: 0 */
22

3-
import { algoliasearch, apiClientVersion } from '../builds/node';
3+
import { vi, test, expect } from 'vitest';
44

5-
const client = algoliasearch('APP_ID', 'API_KEY');
5+
import { LogLevelEnum } from '../../client-common/src/types';
6+
import { createConsoleLogger } from '../../logger-console/src/logger';
7+
import { algoliasearch, apiClientVersion } from '../builds/node';
68

79
test('sets the ua', () => {
10+
const client = algoliasearch('APP_ID', 'API_KEY');
811
expect(client.transporter.algoliaAgent).toEqual({
912
add: expect.any(Function),
1013
value: expect.stringContaining(
@@ -14,10 +17,28 @@ test('sets the ua', () => {
1417
});
1518

1619
test('forwards node search helpers', () => {
20+
const client = algoliasearch('APP_ID', 'API_KEY');
1721
expect(client.generateSecuredApiKey).not.toBeUndefined();
1822
expect(client.getSecuredApiKeyRemainingValidity).not.toBeUndefined();
1923
expect(() => {
2024
const resp = client.generateSecuredApiKey({ parentApiKey: 'foo', restrictions: { validUntil: 200 } });
2125
client.getSecuredApiKeyRemainingValidity({ securedApiKey: resp });
2226
}).not.toThrow();
2327
});
28+
29+
test('with logger', () => {
30+
vi.spyOn(console, 'debug');
31+
vi.spyOn(console, 'info');
32+
vi.spyOn(console, 'error');
33+
34+
const client = algoliasearch('APP_ID', 'API_KEY', {
35+
logger: createConsoleLogger(LogLevelEnum.Debug),
36+
});
37+
38+
expect(async () => {
39+
await client.setSettings({ indexName: 'foo', indexSettings: {} });
40+
expect(console.debug).toHaveBeenCalledTimes(1);
41+
expect(console.info).toHaveBeenCalledTimes(1);
42+
expect(console.error).toHaveBeenCalledTimes(1);
43+
}).not.toThrow();
44+
});

clients/algoliasearch-client-javascript/packages/client-common/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
export * from './src/createAuth';
2-
export * from './src/createIterablePromise';
31
export * from './src/cache';
4-
export * from './src/transporter';
2+
export * from './src/constants';
53
export * from './src/createAlgoliaAgent';
4+
export * from './src/createAuth';
5+
export * from './src/createIterablePromise';
66
export * from './src/getAlgoliaAgent';
7+
export * from './src/logger';
8+
export * from './src/transporter';
79
export * from './src/types';
8-
export * from './src/constants';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* eslint no-console: 0 */
2+
3+
import { vi, describe, test, expect } from 'vitest';
4+
5+
import { createNullLogger } from '../../logger';
6+
7+
describe('null logger', () => {
8+
test('has a null behavior', async () => {
9+
vi.resetAllMocks();
10+
vi.spyOn(console, 'debug');
11+
vi.spyOn(console, 'info');
12+
vi.spyOn(console, 'error');
13+
14+
const logger = createNullLogger();
15+
16+
await logger.debug('foo', {});
17+
await logger.info('foo', {});
18+
await logger.error('foo', {});
19+
20+
expect(console.debug).toHaveBeenCalledTimes(0);
21+
expect(console.info).toHaveBeenCalledTimes(0);
22+
expect(console.error).toHaveBeenCalledTimes(0);
23+
});
24+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { Logger } from '../types/logger';
2+
3+
export function createNullLogger(): Logger {
4+
return {
5+
debug(_message: string, _args?: any): Promise<void> {
6+
return Promise.resolve();
7+
},
8+
info(_message: string, _args?: any): Promise<void> {
9+
return Promise.resolve();
10+
},
11+
error(_message: string, _args?: any): Promise<void> {
12+
return Promise.resolve();
13+
},
14+
};
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './createNullLogger';

clients/algoliasearch-client-javascript/packages/client-common/src/transporter/createTransporter.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function createTransporter({
2525
hosts,
2626
hostsCache,
2727
baseHeaders,
28+
logger,
2829
baseQueryParameters,
2930
algoliaAgent,
3031
timeouts,
@@ -174,8 +175,7 @@ export function createTransporter({
174175
* the end user to debug / store stack frames even
175176
* when a retry error does not happen.
176177
*/
177-
// eslint-disable-next-line no-console -- this will be fixed by exposing a `logger` to the transporter
178-
console.log('Retryable failure', stackFrameWithoutCredentials(stackFrame));
178+
logger.info('Retryable failure', stackFrameWithoutCredentials(stackFrame));
179179

180180
/**
181181
* We also store the state of the host in failure cases. If the host, is
@@ -304,6 +304,7 @@ export function createTransporter({
304304
hostsCache,
305305
requester,
306306
timeouts,
307+
logger,
307308
algoliaAgent,
308309
baseHeaders,
309310
baseQueryParameters,

clients/algoliasearch-client-javascript/packages/client-common/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ export * from './cache';
22
export * from './createClient';
33
export * from './createIterablePromise';
44
export * from './host';
5+
export * from './logger';
56
export * from './requester';
67
export * from './transporter';
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export const LogLevelEnum: Readonly<Record<string, LogLevelType>> = {
2+
Debug: 1,
3+
Info: 2,
4+
Error: 3,
5+
};
6+
7+
export type LogLevelType = 1 | 2 | 3;
8+
9+
export type Logger = {
10+
/**
11+
* Logs debug messages.
12+
*/
13+
debug: (message: string, args?: any) => Promise<void>;
14+
15+
/**
16+
* Logs info messages.
17+
*/
18+
info: (message: string, args?: any) => Promise<void>;
19+
20+
/**
21+
* Logs error messages.
22+
*/
23+
error: (message: string, args?: any) => Promise<void>;
24+
};

clients/algoliasearch-client-javascript/packages/client-common/src/types/transporter.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Cache } from './cache';
22
import type { Host } from './host';
3+
import type { Logger } from './logger';
34
import type { Request, Requester, EndRequest, Response } from './requester';
45

56
export type Headers = Record<string, string>;
@@ -87,6 +88,11 @@ export type TransporterOptions = {
8788
*/
8889
hostsCache: Cache;
8990

91+
/**
92+
* The logger instance to send events of the transporter.
93+
*/
94+
logger: Logger;
95+
9096
/**
9197
* The underlying requester used. Should differ
9298
* depending of the environment where the client

clients/algoliasearch-client-javascript/packages/client-common/vitest.workspace.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default defineWorkspace([
88
'src/__tests__/cache/null-cache.test.ts',
99
'src/__tests__/cache/memory-cache.test.ts',
1010
'src/__tests__/create-iterable-promise.test.ts',
11+
'src/__tests__/logger/null-logger.test.ts',
1112
],
1213
name: 'node',
1314
environment: 'node',
@@ -20,6 +21,7 @@ export default defineWorkspace([
2021
'src/__tests__/cache/fallbackable-cache.test.ts',
2122
'src/__tests__/cache/null-cache.test.ts',
2223
'src/__tests__/create-iterable-promise.test.ts',
24+
'src/__tests__/logger/null-logger.test.ts',
2325
],
2426
name: 'jsdom',
2527
environment: 'jsdom',
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './src/logger';
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "@algolia/logger-console",
3+
"version": "5.5.3",
4+
"description": "Promise-based log library using console log.",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/algolia/algoliasearch-client-javascript.git"
8+
},
9+
"license": "MIT",
10+
"author": "Algolia",
11+
"type": "module",
12+
"files": [
13+
"dist",
14+
"src",
15+
"index.ts"
16+
],
17+
"exports": {
18+
".": {
19+
"types": {
20+
"import": "./dist/logger.d.ts",
21+
"module": "./dist/logger.d.ts",
22+
"require": "./dist/logger.d.cts"
23+
},
24+
"import": "./dist/logger.js",
25+
"module": "./dist/logger.js",
26+
"require": "./dist/logger.cjs"
27+
},
28+
"./src/*": "./src/*.ts"
29+
},
30+
"scripts": {
31+
"build": "yarn clean && yarn tsup",
32+
"clean": "rm -rf ./dist || true",
33+
"test": "vitest --run",
34+
"test:bundle": "publint . && attw --pack ."
35+
},
36+
"devDependencies": {
37+
"@arethetypeswrong/cli": "0.16.4",
38+
"@types/node": "22.5.5",
39+
"jsdom": "25.0.0",
40+
"publint": "0.2.11",
41+
"ts-node": "10.9.2",
42+
"tsup": "8.3.0",
43+
"typescript": "5.6.2",
44+
"vitest": "2.1.1"
45+
},
46+
"dependencies": {
47+
"@algolia/client-common": "5.5.3"
48+
},
49+
"engines": {
50+
"node": ">= 14.0.0"
51+
}
52+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint no-console: 0 */
2+
3+
import { LogLevelEnum } from '@algolia/client-common';
4+
import { vi, beforeEach, describe, test, expect } from 'vitest';
5+
6+
import { createConsoleLogger } from '../logger';
7+
8+
describe('console logger', () => {
9+
beforeEach(() => {
10+
vi.resetAllMocks();
11+
vi.spyOn(console, 'debug');
12+
vi.spyOn(console, 'info');
13+
vi.spyOn(console, 'error');
14+
});
15+
16+
test('respects log level type debug', async () => {
17+
const logger = createConsoleLogger(LogLevelEnum.Debug);
18+
19+
await logger.debug('foo', {});
20+
await logger.info('foo', {});
21+
await logger.error('foo', {});
22+
23+
expect(console.debug).toHaveBeenCalledTimes(1);
24+
expect(console.info).toHaveBeenCalledTimes(1);
25+
expect(console.error).toHaveBeenCalledTimes(1);
26+
});
27+
28+
test('respects log level type info', async () => {
29+
const logger = createConsoleLogger(LogLevelEnum.Info);
30+
31+
await logger.debug('foo', {});
32+
await logger.info('foo', {});
33+
await logger.error('foo', {});
34+
35+
expect(console.debug).toHaveBeenCalledTimes(0);
36+
expect(console.info).toHaveBeenCalledTimes(1);
37+
expect(console.error).toHaveBeenCalledTimes(1);
38+
});
39+
40+
test('respects log level type error', async () => {
41+
const logger = createConsoleLogger(LogLevelEnum.Error);
42+
43+
await logger.debug('foo', {});
44+
await logger.info('foo', {});
45+
await logger.error('foo', {});
46+
47+
expect(console.debug).toHaveBeenCalledTimes(0);
48+
expect(console.info).toHaveBeenCalledTimes(0);
49+
expect(console.error).toHaveBeenCalledTimes(1);
50+
});
51+
});

0 commit comments

Comments
 (0)