Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ILogger): add #has method, auto-register Store#logger #221

Merged
merged 2 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/lib/SapphireClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { container } from '@sapphire/pieces';
import { container, Store } from '@sapphire/pieces';
import type { Awaited } from '@sapphire/utilities';
import { Client, ClientOptions, Message } from 'discord.js';
import { join } from 'path';
Expand Down Expand Up @@ -96,6 +96,13 @@ export interface SapphireClientOptions {
*/
logger?: ClientLoggerOptions;

/**
* Whether or not trace logging should be enabled.
* @since 2.0.0
* @default container.logger.has(LogLevel.Trace)
*/
enableLoaderTraceLoggings?: boolean;

/**
* If Sapphire should load our pre-included error event listeners that log any encountered errors to the {@link SapphireClient.logger} instance
* @since 1.0.0
Expand Down Expand Up @@ -212,6 +219,9 @@ export class SapphireClient extends Client {

this.logger = options.logger?.instance ?? new Logger(options.logger?.level ?? LogLevel.Info);
container.logger = this.logger;
if (options.enableLoaderTraceLoggings ?? container.logger.has(LogLevel.Trace)) {
Store.logger = container.logger.trace.bind(container.logger);
}

this.stores = new StoreRegistry();
container.stores = this.stores;
Expand Down
6 changes: 6 additions & 0 deletions src/lib/utils/logger/ILogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export const enum LogLevel {
}

export interface ILogger {
/**
* Checks whether a level is supported.
* @param level The level to check.
*/
has(level: LogLevel): boolean;

/**
* Alias of {@link ILogger.write} with {@link LogLevel.Trace} as level.
* @param values The values to log.
Expand Down
6 changes: 5 additions & 1 deletion src/lib/utils/logger/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export class Logger implements ILogger {
this.level = level;
}

public has(level: LogLevel): boolean {
return level >= this.level;
}

public trace(...values: readonly unknown[]): void {
this.write(LogLevel.Trace, ...values);
}
Expand All @@ -32,7 +36,7 @@ export class Logger implements ILogger {
}

public write(level: LogLevel, ...values: readonly unknown[]): void {
if (level < this.level) return;
if (!this.has(level)) return;
const method = Logger.levels.get(level);
if (typeof method === 'string') console[method](...values);
}
Expand Down