Skip to content

Commit 0b3b47a

Browse files
committed
chore: replace loglevel with minimalistic internal logger implementation
1 parent eee6b0a commit 0b3b47a

File tree

4 files changed

+150
-21
lines changed

4 files changed

+150
-21
lines changed

packages/client/package.json

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,23 @@
3939
},
4040
"typesVersions": {
4141
"*": {
42-
"actions": ["./dist/actions/index.d.ts"],
43-
"ethers": ["./dist/ethers/index.d.ts"],
44-
"viem": ["./dist/viem/index.d.ts"],
45-
"test-utils": ["./dist/test-utils.d.ts"]
42+
"actions": [
43+
"./dist/actions/index.d.ts"
44+
],
45+
"ethers": [
46+
"./dist/ethers/index.d.ts"
47+
],
48+
"viem": [
49+
"./dist/viem/index.d.ts"
50+
],
51+
"test-utils": [
52+
"./dist/test-utils.d.ts"
53+
]
4654
}
4755
},
48-
"files": ["dist"],
56+
"files": [
57+
"dist"
58+
],
4959
"sideEffects": false,
5060
"scripts": {
5161
"build": "tsup",
@@ -59,8 +69,7 @@
5969
"@urql/core": "^5.0.8",
6070
"@urql/exchange-auth": "^2.2.0",
6171
"graphql": "^16.9.0",
62-
"jwt-decode": "^4.0.0",
63-
"loglevel": "^1.9.2"
72+
"jwt-decode": "^4.0.0"
6473
},
6574
"peerDependencies": {
6675
"@lens-chain/sdk": "^1.0.0",

packages/client/src/clients.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
fetchExchange,
3030
} from '@urql/core';
3131
import { type AuthConfig, authExchange } from '@urql/exchange-auth';
32-
import { type Logger, getLogger } from 'loglevel';
3332

3433
import { CredentialsStorage } from '@lens-protocol/storage';
3534
import { type AuthenticatedUser, authenticatedUser } from './AuthenticatedUser';
@@ -45,6 +44,7 @@ import {
4544
UnexpectedError,
4645
hasExtensionCode,
4746
} from './errors';
47+
import { LogLevel, Logger } from './logger';
4848
import { decodeAccessToken, decodeIdToken } from './tokens';
4949
import { delay } from './utils';
5050

@@ -88,8 +88,10 @@ abstract class AbstractClient<TContext extends Context, TError> {
8888
*/
8989
public readonly context: TContext,
9090
) {
91-
this.logger = getLogger(this.constructor.name);
92-
this.logger.setLevel(context.debug ? 'DEBUG' : 'SILENT', false);
91+
this.logger = Logger.named(
92+
this.constructor.name,
93+
context.debug ? LogLevel.DEBUG : LogLevel.SILENT,
94+
);
9395

9496
this.urql = createClient({
9597
url: context.environment.backend,

packages/client/src/logger.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
type LogMethod = (...args: unknown[]) => void;
2+
3+
export enum LogLevel {
4+
TRACE = 0,
5+
DEBUG = 1,
6+
INFO = 2,
7+
WARN = 3,
8+
ERROR = 4,
9+
SILENT = 5,
10+
}
11+
12+
export class Logger {
13+
private level: LogLevel;
14+
private name: string;
15+
16+
private constructor(name: string, level: LogLevel = LogLevel.WARN) {
17+
this.name = name;
18+
this.level = level;
19+
this.replaceMethods();
20+
}
21+
22+
static named(name: string, level: LogLevel = LogLevel.WARN): Logger {
23+
return new Logger(name, level);
24+
}
25+
26+
trace: LogMethod = () => {};
27+
debug: LogMethod = () => {};
28+
info: LogMethod = () => {};
29+
warn: LogMethod = () => {};
30+
error: LogMethod = () => {};
31+
log: LogMethod = () => {}; // alias for debug
32+
33+
private replaceMethods() {
34+
(['trace', 'debug', 'info', 'warn', 'error'] as const).forEach((methodName, index) => {
35+
if (index >= this.level) {
36+
this[methodName] = () => {};
37+
} else {
38+
this[methodName] = (...args) =>
39+
console[methodName === 'debug' ? 'log' : methodName](`[${this.name}]`, ...args);
40+
}
41+
});
42+
this.log = this.debug;
43+
}
44+
}

pnpm-lock.yaml

Lines changed: 85 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)