Skip to content

Commit 35e7a14

Browse files
committed
fix: logging enhancements
1 parent 80b4bf4 commit 35e7a14

File tree

13 files changed

+45
-66
lines changed

13 files changed

+45
-66
lines changed

packages/ai/src/configure.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ const AIConfig: Required<ConfigureAI> = {
168168
}
169169
},
170170
onError: async (_ctx, message, error) => {
171-
Logger.error(`Error processing AI message: ${error}`);
171+
Logger.error`Error processing AI message: ${error}`;
172172
const channel = message.channel as TextChannel;
173173

174174
if (channel.isSendable()) {
@@ -177,7 +177,7 @@ const AIConfig: Required<ConfigureAI> = {
177177
content: 'An error occurred while processing your request.',
178178
allowedMentions: { parse: [] },
179179
})
180-
.catch((e) => Logger.error(`Failed to send error message: ${e}`));
180+
.catch((e) => Logger.error`Failed to send error message: ${e}`);
181181
}
182182
},
183183
};

packages/commandkit/src/analytics/analytics-engine.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,7 @@ export class AnalyticsEngine {
8383
try {
8484
await this.#provider!.identify?.(this, event);
8585
} catch (error) {
86-
Logger.error(
87-
`Error identifying with provider ${this.#provider!.name}`,
88-
error,
89-
);
86+
Logger.error`Error identifying with provider ${this.#provider!.name} ${error}`;
9087
}
9188
}
9289

@@ -102,10 +99,7 @@ export class AnalyticsEngine {
10299
if (await this.#doNotTrack(event)) return;
103100
await this.#provider!.track(this, event);
104101
} catch (error) {
105-
Logger.error(
106-
`Error tracking ${event.name} event with provider ${this.#provider!.name}`,
107-
error,
108-
);
102+
Logger.error`Error tracking ${event.name} event with provider ${this.#provider!.name} ${error}`;
109103
}
110104
}
111105

packages/commandkit/src/app/commands/AppCommandRunner.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ export class AppCommandRunner {
158158
const time = `${env.getExecutionTime().toFixed(2)}ms`;
159159

160160
if (error) {
161-
Logger.error(
162-
`[${marker} - ${time}] Error executing command: ${error.stack || error}`,
163-
);
161+
Logger.error`[${marker} - ${time}] Error executing command: ${error}`;
164162

165163
const commandName =
166164
prepared.command?.data?.command?.name ??
@@ -237,7 +235,7 @@ export class AppCommandRunner {
237235
if (shouldThrowOnError) {
238236
throw e;
239237
}
240-
Logger.error(e);
238+
Logger.error`${e}`;
241239
}
242240
}
243241
}

packages/commandkit/src/app/handlers/AppCommandHandler.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,7 @@ export class AppCommandHandler {
462462
COMMANDKIT_IS_DEV &&
463463
this.commandkit.config.showUnknownPrefixCommandsWarning
464464
) {
465-
Logger.error(
466-
`Prefix command "${command}" was not found.\nNote: This warning is only shown in development mode as an alert to help you find the command. If you wish to remove this warning, set \`showUnknownPrefixCommandsWarning\` to \`false\` in your commandkit config.`,
467-
);
465+
Logger.error`Prefix command "${command}" was not found.\nNote: This warning is only shown in development mode as an alert to help you find the command. If you wish to remove this warning, set \`showUnknownPrefixCommandsWarning\` to \`false\` in your commandkit config.`;
468466
}
469467
return null;
470468
}
@@ -502,7 +500,7 @@ export class AppCommandHandler {
502500
if (isErrorType(e, CommandKitErrorCodes.InvalidCommandPrefix)) {
503501
return null;
504502
}
505-
Logger.error(e);
503+
Logger.error`${e}`;
506504
return null;
507505
}
508506
} else {
@@ -729,7 +727,7 @@ export class AppCommandHandler {
729727

730728
this.loadedMiddlewares.set(id, { middleware, data });
731729
} catch (error) {
732-
Logger.error(`Failed to load middleware ${id}`, error);
730+
Logger.error`Failed to load middleware ${id}\n${error}`;
733731
}
734732
}
735733

@@ -865,7 +863,7 @@ export class AppCommandHandler {
865863
},
866864
});
867865
} catch (error) {
868-
Logger.error(`Failed to load command ${command.name} (${id})`, error);
866+
Logger.error`Failed to load command ${command.name} (${id})\n${error}`;
869867
}
870868
}
871869

packages/commandkit/src/app/handlers/AppEventsHandler.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ export class AppEventsHandler {
105105
const handler = await import(toFileURL(listener, true));
106106

107107
if (!handler.default || typeof handler.default !== 'function') {
108-
Logger.error(
109-
`Event handler for ${event.event}${event.namespace ? ` of namespace ${event.namespace}` : ''} does not have a default export or is not a function`,
110-
);
108+
Logger.error`Event handler for ${event.event}${event.namespace ? ` of namespace ${event.namespace}` : ''} does not have a default export or is not a function`;
111109
}
112110

113111
listeners.push({
@@ -217,12 +215,9 @@ export class AppEventsHandler {
217215
}
218216

219217
// Otherwise log the error as usual
220-
Logger.error(
221-
`Error handling event ${name}${
222-
namespace ? ` of namespace ${namespace}` : ''
223-
}`,
224-
e,
225-
);
218+
Logger.error`Error handling event ${name}${
219+
namespace ? ` of namespace ${namespace}` : ''
220+
} ${e}`;
226221
}
227222
}
228223
},
@@ -284,12 +279,9 @@ export class AppEventsHandler {
284279
}
285280

286281
// Otherwise log the error as usual
287-
Logger.error(
288-
`Error handling event ${name}${
289-
namespace ? ` of namespace ${namespace}` : ''
290-
}`,
291-
e,
292-
);
282+
Logger.error`Error handling event ${name}${
283+
namespace ? ` of namespace ${namespace}` : ''
284+
} ${e}`;
293285
}
294286
},
295287
);

packages/commandkit/src/app/middlewares/permissions.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ export async function beforeExecute(ctx: MiddlewareContext) {
4444
}
4545
}
4646
} catch (error) {
47-
Logger.error(
48-
`Could not send 'Server-only command' DM to user ${interaction?.user.id ?? message?.author.id} for command ${command.command.name}.`,
49-
error,
50-
);
47+
Logger.error`Could not send 'Server-only command' DM to user ${interaction?.user.id ?? message?.author.id} for command ${command.command.name}. ${error}`;
5148
}
5249

5350
stopMiddlewares(); // Stop the command from executing
@@ -154,10 +151,7 @@ export async function beforeExecute(ctx: MiddlewareContext) {
154151
});
155152
}
156153
} catch (error) {
157-
Logger.error(
158-
`Could not send 'Not enough permissions' reply to user ${interaction?.user.id ?? message?.author.id} for command ${command.command.name}.`,
159-
error,
160-
);
154+
Logger.error`Could not send 'Not enough permissions' reply to user ${interaction?.user.id ?? message?.author.id} for command ${command.command.name}. ${error}`;
161155
}
162156

163157
stopMiddlewares(); // Stop the command from executing

packages/commandkit/src/app/register/CommandRegistrar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export class CommandRegistrar {
172172
`✨ Refreshed ${data.length} global application (/) commands`,
173173
);
174174
} catch (e) {
175-
Logger.error('Failed to update global application (/) commands', e);
175+
Logger.error`Failed to update global application (/) commands ${e}`;
176176
}
177177
}
178178

@@ -253,7 +253,7 @@ export class CommandRegistrar {
253253

254254
Logger.info(`✨ Refreshed ${count} guild application (/) commands`);
255255
} catch (e) {
256-
Logger.error('Failed to update guild application (/) commands', e);
256+
Logger.error`Failed to update guild application (/) commands ${e}`;
257257
}
258258
}
259259
}

packages/commandkit/src/commandkit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ export class CommandKit extends EventEmitter {
207207
try {
208208
await hook(this);
209209
} catch (e) {
210-
Logger.error('Error while executing bootstrap hook: ', e);
210+
Logger.error`Error while executing bootstrap hook: ${e}`;
211211
} finally {
212212
bootstrapHooks.delete(hook);
213213
}
@@ -222,7 +222,7 @@ export class CommandKit extends EventEmitter {
222222
try {
223223
await hook(this);
224224
} catch (e) {
225-
Logger.error('Error while executing application bootstrap hook: ', e);
225+
Logger.error`Error while executing application bootstrap hook: ${e}`;
226226
} finally {
227227
onApplicationBootstrapHooks.delete(hook);
228228
}
@@ -254,7 +254,7 @@ export class CommandKit extends EventEmitter {
254254
} catch (e) {
255255
// ignore
256256
if (process.env.COMMANDKIT_DEBUG_TYPEGEN) {
257-
Logger.error(e);
257+
Logger.error`${e}`;
258258
}
259259
}
260260
}

packages/commandkit/src/flags/feature-flags.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,7 @@ export class FeatureFlag<R, T> {
352352
}
353353
}
354354
} catch (error) {
355-
Logger.error(
356-
`Error fetching flag provider configuration for "${this.options.key}": ${error}`,
357-
);
355+
Logger.error`Error fetching flag provider configuration for "${this.options.key}": ${error}`;
358356
// continue with local decision if provider fails
359357
}
360358
}

packages/commandkit/src/logger/DefaultLogger.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,20 @@ export class DefaultLogger implements ILogger {
139139
const context = this._getContext();
140140
const colorFn = TextColorMap[level];
141141

142+
let processedMessage = message;
143+
if (message instanceof Error) {
144+
processedMessage = `${message.message}\n${message.stack}`;
145+
}
146+
142147
if (context) {
143148
this.logger.log(
144149
`${prefix}\n${context} ${colors.dim(BoxChars.corner)}`,
145-
colorFn(message),
150+
colorFn(processedMessage),
146151
);
147152
} else {
148153
this.logger.log(
149154
`${prefix} ${colors.dim(BoxChars.corner)}`,
150-
colorFn(message),
155+
colorFn(processedMessage),
151156
);
152157
}
153158
}
@@ -165,10 +170,14 @@ export class DefaultLogger implements ILogger {
165170
for (let i = 0; i < strings.length; i++) {
166171
result += strings[i];
167172
if (i < values.length) {
168-
result += inspect(values[i], {
169-
colors: COMMANDKIT_IS_DEV,
170-
depth: 2,
171-
});
173+
const value = values[i];
174+
if (value instanceof Error) {
175+
result += `${value.message}\n${value.stack}`;
176+
} else if (value !== null && typeof value === 'object') {
177+
result += inspect(value, { colors: true, depth: 2 });
178+
} else {
179+
result += value;
180+
}
172181
}
173182
}
174183

0 commit comments

Comments
 (0)