Skip to content

Commit 1b0f99a

Browse files
committed
feat(Logger): add tagged template support
1 parent b7f0618 commit 1b0f99a

File tree

10 files changed

+225
-55
lines changed

10 files changed

+225
-55
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { EventHandler, Logger } from 'commandkit';
2+
3+
const handler: EventHandler<'clientReady'> = (client) => {
4+
Logger.log(`Successfully logged in as ${client.user?.tag}`);
5+
};
6+
7+
export default handler;

apps/test-bot/src/app/events/ready/logger.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { type EventHandler, Logger } from 'commandkit';
2+
3+
export const once = true;
4+
5+
const handler: EventHandler<'clientReady'> = (client, c, commandkit) => {
6+
Logger.info`Ready from legacy event handler: ${client.user}`;
7+
};
8+
9+
export default handler;

apps/test-bot/src/events/ready/test.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/commandkit/src/logger/DefaultLogger.ts

Lines changed: 106 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getContext } from '../context/async-context';
22
import colors from '../utils/colors';
3+
import { COMMANDKIT_IS_DEV } from '../utils/constants';
34
import { ILogger } from './ILogger';
5+
import { inspect } from 'util';
46

57
/**
68
* Log levels for the logger.
@@ -132,61 +134,147 @@ export class DefaultLogger implements ILogger {
132134
return `${label}${colors.dim(BoxChars.vertical)} ${colors.dim(timestamp)}`;
133135
}
134136

135-
private _log(level: LogLevel, ...args: any[]): void {
137+
private _log(level: LogLevel, message: any): void {
136138
const prefix = this._getPrefix(level);
137139
const context = this._getContext();
138140
const colorFn = TextColorMap[level];
139141

140142
if (context) {
141143
this.logger.log(
142144
`${prefix}\n${context} ${colors.dim(BoxChars.corner)}`,
143-
...args.map((arg) => colorFn(arg)),
145+
colorFn(message),
144146
);
145147
} else {
146148
this.logger.log(
147149
`${prefix} ${colors.dim(BoxChars.corner)}`,
148-
...args.map((arg) => colorFn(arg)),
150+
colorFn(message),
151+
);
152+
}
153+
}
154+
155+
private _logTemplate(
156+
level: LogLevel,
157+
strings: TemplateStringsArray,
158+
...values: any[]
159+
): void {
160+
const prefix = this._getPrefix(level);
161+
const context = this._getContext();
162+
const colorFn = TextColorMap[level];
163+
164+
let result = '';
165+
for (let i = 0; i < strings.length; i++) {
166+
result += strings[i];
167+
if (i < values.length) {
168+
result += inspect(values[i], {
169+
colors: COMMANDKIT_IS_DEV,
170+
depth: 2,
171+
});
172+
}
173+
}
174+
175+
if (context) {
176+
this.logger.log(
177+
`${prefix}\n${context} ${colors.dim(BoxChars.corner)}`,
178+
colorFn(result),
179+
);
180+
} else {
181+
this.logger.log(
182+
`${prefix} ${colors.dim(BoxChars.corner)}`,
183+
colorFn(result),
149184
);
150185
}
151186
}
152187

153188
/**
154189
* Logs a debug message.
155-
* @param args The message arguments to log.
190+
* @param message The message to log.
156191
*/
157-
public debug(...args: any[]): void {
158-
this._log(LogLevel.DEBUG, ...args);
192+
public debug(message: any): void;
193+
public debug(strings: TemplateStringsArray, ...values: any[]): void;
194+
public debug(
195+
messageOrStrings: any | TemplateStringsArray,
196+
...values: any[]
197+
): void {
198+
if (this._isTemplateStringsArray(messageOrStrings)) {
199+
this._logTemplate(LogLevel.DEBUG, messageOrStrings, ...values);
200+
} else {
201+
this._log(LogLevel.DEBUG, messageOrStrings);
202+
}
159203
}
160204

161205
/**
162206
* Logs an error message.
163-
* @param args The message arguments to log.
207+
* @param message The error message to log.
164208
*/
165-
public error(...args: any[]): void {
166-
this._log(LogLevel.ERROR, ...args);
209+
public error(message: any): void;
210+
public error(strings: TemplateStringsArray, ...values: any[]): void;
211+
public error(
212+
messageOrStrings: any | TemplateStringsArray,
213+
...values: any[]
214+
): void {
215+
if (this._isTemplateStringsArray(messageOrStrings)) {
216+
this._logTemplate(LogLevel.ERROR, messageOrStrings, ...values);
217+
} else {
218+
this._log(LogLevel.ERROR, messageOrStrings);
219+
}
167220
}
168221

169222
/**
170223
* Logs a default message.
171-
* @param args The message arguments to log.
224+
* @param message The message to log.
172225
*/
173-
public log(...args: any[]): void {
174-
this._log(LogLevel.DEFAULT, ...args);
226+
public log(message: any): void;
227+
public log(strings: TemplateStringsArray, ...values: any[]): void;
228+
public log(
229+
messageOrStrings: any | TemplateStringsArray,
230+
...values: any[]
231+
): void {
232+
if (this._isTemplateStringsArray(messageOrStrings)) {
233+
this._logTemplate(LogLevel.DEFAULT, messageOrStrings, ...values);
234+
} else {
235+
this._log(LogLevel.DEFAULT, messageOrStrings);
236+
}
175237
}
176238

177239
/**
178240
* Logs an info message.
179-
* @param args The message arguments to log.
241+
* @param message The informational message to log.
180242
*/
181-
public info(...args: any[]): void {
182-
this._log(LogLevel.INFO, ...args);
243+
public info(message: any): void;
244+
public info(strings: TemplateStringsArray, ...values: any[]): void;
245+
public info(
246+
messageOrStrings: any | TemplateStringsArray,
247+
...values: any[]
248+
): void {
249+
if (this._isTemplateStringsArray(messageOrStrings)) {
250+
this._logTemplate(LogLevel.INFO, messageOrStrings, ...values);
251+
} else {
252+
this._log(LogLevel.INFO, messageOrStrings);
253+
}
183254
}
184255

185256
/**
186257
* Logs a warning message.
187-
* @param args The message arguments to log.
258+
* @param message The warning message to log.
188259
*/
189-
public warn(...args: any[]): void {
190-
this._log(LogLevel.WARN, ...args);
260+
public warn(message: any): void;
261+
public warn(strings: TemplateStringsArray, ...values: any[]): void;
262+
public warn(
263+
messageOrStrings: any | TemplateStringsArray,
264+
...values: any[]
265+
): void {
266+
if (this._isTemplateStringsArray(messageOrStrings)) {
267+
this._logTemplate(LogLevel.WARN, messageOrStrings, ...values);
268+
} else {
269+
this._log(LogLevel.WARN, messageOrStrings);
270+
}
271+
}
272+
273+
private _isTemplateStringsArray(value: any): value is TemplateStringsArray {
274+
return (
275+
Array.isArray(value) &&
276+
'raw' in value &&
277+
Array.isArray((value as any).raw)
278+
);
191279
}
192280
}

packages/commandkit/src/logger/ILogger.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,57 @@
44
export interface ILogger {
55
/**
66
* Logs a message with the default log level.
7-
* @param args The message to log.
7+
* @param message The message to log.
88
*/
9-
log(...args: any[]): void;
9+
log(message: any): void;
10+
/**
11+
* Logs a message with the default log level using template literals.
12+
* @param strings The template literal strings.
13+
* @param values The values to interpolate.
14+
*/
15+
log(strings: TemplateStringsArray, ...values: any[]): void;
1016
/**
1117
* Logs an error message.
12-
* @param args The error message to log.
18+
* @param message The error message to log.
1319
*/
14-
error(...args: any[]): void;
20+
error(message: any): void;
21+
/**
22+
* Logs an error message using template literals.
23+
* @param strings The template literal strings.
24+
* @param values The values to interpolate.
25+
*/
26+
error(strings: TemplateStringsArray, ...values: any[]): void;
1527
/**
1628
* Logs a warning message.
17-
* @param args The warning message to log.
29+
* @param message The warning message to log.
30+
*/
31+
warn(message: any): void;
32+
/**
33+
* Logs a warning message using template literals.
34+
* @param strings The template literal strings.
35+
* @param values The values to interpolate.
1836
*/
19-
warn(...args: any[]): void;
37+
warn(strings: TemplateStringsArray, ...values: any[]): void;
2038
/**
2139
* Logs an informational message.
22-
* @param args The informational message to log.
40+
* @param message The informational message to log.
41+
*/
42+
info(message: any): void;
43+
/**
44+
* Logs an informational message using template literals.
45+
* @param strings The template literal strings.
46+
* @param values The values to interpolate.
2347
*/
24-
info(...args: any[]): void;
48+
info(strings: TemplateStringsArray, ...values: any[]): void;
2549
/**
2650
* Logs a debug message.
27-
* @param args The debug message to log.
51+
* @param message The debug message to log.
52+
*/
53+
debug(message: any): void;
54+
/**
55+
* Logs a debug message using template literals.
56+
* @param strings The template literal strings.
57+
* @param values The values to interpolate.
2858
*/
29-
debug(...args: any[]): void;
59+
debug(strings: TemplateStringsArray, ...values: any[]): void;
3060
}

packages/commandkit/src/logger/Logger.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ export function createLogger(options: CommandKitLoggerOptions): LoggerImpl {
4343
} as LoggerImpl;
4444

4545
for (const method of methods) {
46-
impl[method] = (...args: any[]) => {
47-
opt.provider[method](...args);
46+
impl[method] = (...message: any) => {
47+
// @ts-ignore
48+
opt.provider[method](...message);
4849
};
4950
}
5051

packages/commandkit/src/logger/NoopLogger.ts

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,81 @@ import { ILogger } from './ILogger';
66
export class NoopLogger implements ILogger {
77
/**
88
* Logs a debug message.
9-
* @param args The message arguments to log.
9+
* @param message The message to log.
1010
*/
11-
public debug(...args: any[]): void {}
11+
public debug(message: any): void;
12+
/**
13+
* Logs a debug message using template literals.
14+
* @param strings The template literal strings.
15+
* @param values The values to interpolate.
16+
*/
17+
public debug(strings: TemplateStringsArray, ...values: any[]): void;
18+
public debug(
19+
messageOrStrings: any | TemplateStringsArray,
20+
...values: any[]
21+
): void {}
1222

1323
/**
1424
* Logs an error message.
15-
* @param args The message arguments to log.
25+
* @param message The error message to log.
1626
*/
17-
public error(...args: any[]): void {}
27+
public error(message: any): void;
28+
/**
29+
* Logs an error message using template literals.
30+
* @param strings The template literal strings.
31+
* @param values The values to interpolate.
32+
*/
33+
public error(strings: TemplateStringsArray, ...values: any[]): void;
34+
public error(
35+
messageOrStrings: any | TemplateStringsArray,
36+
...values: any[]
37+
): void {}
1838

1939
/**
2040
* Logs a default message.
21-
* @param args The message arguments to log.
41+
* @param message The message to log.
42+
*/
43+
public log(message: any): void;
44+
/**
45+
* Logs a default message using template literals.
46+
* @param strings The template literal strings.
47+
* @param values The values to interpolate.
2248
*/
23-
public log(...args: any[]): void {}
49+
public log(strings: TemplateStringsArray, ...values: any[]): void;
50+
public log(
51+
messageOrStrings: any | TemplateStringsArray,
52+
...values: any[]
53+
): void {}
2454

2555
/**
2656
* Logs an info message.
27-
* @param args The message arguments to log.
57+
* @param message The informational message to log.
58+
*/
59+
public info(message: any): void;
60+
/**
61+
* Logs an info message using template literals.
62+
* @param strings The template literal strings.
63+
* @param values The values to interpolate.
2864
*/
29-
public info(...args: any[]): void {}
65+
public info(strings: TemplateStringsArray, ...values: any[]): void;
66+
public info(
67+
messageOrStrings: any | TemplateStringsArray,
68+
...values: any[]
69+
): void {}
3070

3171
/**
3272
* Logs a warning message.
33-
* @param args The message arguments to log.
73+
* @param message The warning message to log.
74+
*/
75+
public warn(message: any): void;
76+
/**
77+
* Logs a warning message using template literals.
78+
* @param strings The template literal strings.
79+
* @param values The values to interpolate.
3480
*/
35-
public warn(...args: any[]): void {}
81+
public warn(strings: TemplateStringsArray, ...values: any[]): void;
82+
public warn(
83+
messageOrStrings: any | TemplateStringsArray,
84+
...values: any[]
85+
): void {}
3686
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)