@sapphire/plugin-logger
Version:
Plugin for @sapphire/framework to have pretty console output
384 lines (377 loc) • 10 kB
text/typescript
import { LogLevel, Logger as Logger$1 } from '@sapphire/framework';
import * as Colorette from 'colorette';
import { Timestamp } from '@sapphire/timestamp';
/**
* Logger utility that applies a style to a string.
* @since 1.0.0
*/
declare class LoggerStyle {
readonly style: Colorette.Color;
constructor(resolvable?: LoggerStyleResolvable);
/**
* Applies the style to a string.
* @since 1.0.0
* @param string The value to apply the style to.
*/
run(string: string | number): string;
}
/**
* The options for {@link LoggerStyle}.
* @since 1.0.0
*/
interface LoggerStyleOptions {
/**
* The text effects, e.g. `italic`, `strikethrough`, etc.
* @since 1.0.0
*/
effects?: LoggerStyleEffect[];
/**
* The text color, e.g. `red` or `yellow`.
* @since 1.0.0
*/
text?: LoggerStyleText;
/**
* The background color, e.g. `magenta` or `red`.
* @since 1.0.0
*/
background?: LoggerStyleBackground;
}
/**
* The value accepted by {@link LoggerStyle}'s constructor. Read `colorette`'s documentation for more information.
* @since 1.0.0
* @seealso https://www.npmjs.com/package/colorette
*/
type LoggerStyleResolvable = Colorette.Color | LoggerStyleOptions;
/**
* The text styles.
* @since 1.0.0
*/
declare enum LoggerStyleEffect {
Reset = "reset",
Bold = "bold",
Dim = "dim",
Italic = "italic",
Underline = "underline",
Inverse = "inverse",
Hidden = "hidden",
Strikethrough = "strikethrough"
}
/**
* The text colors.
* @since 1.0.0
*/
declare enum LoggerStyleText {
Black = "black",
Red = "red",
Green = "green",
Yellow = "yellow",
Blue = "blue",
Magenta = "magenta",
Cyan = "cyan",
White = "white",
Gray = "gray",
BlackBright = "blackBright",
RedBright = "redBright",
GreenBright = "greenBright",
YellowBright = "yellowBright",
BlueBright = "blueBright",
MagentaBright = "magentaBright",
CyanBright = "cyanBright",
WhiteBright = "whiteBright"
}
/**
* The background colors.
* @since 1.0.0
*/
declare enum LoggerStyleBackground {
Black = "bgBlack",
Red = "bgRed",
Green = "bgGreen",
Yellow = "bgYellow",
Blue = "bgBlue",
Magenta = "bgMagenta",
Cyan = "bgCyan",
White = "bgWhite",
BlackBright = "bgBlackBright",
RedBright = "bgRedBright",
GreenBright = "bgGreenBright",
YellowBright = "bgYellowBright",
BlueBright = "bgBlueBright",
MagentaBright = "bgMagentaBright",
CyanBright = "bgCyanBright",
WhiteBright = "bgWhiteBright"
}
/**
* Logger utility that formats a timestamp.
* @since 1.0.0
*/
declare class LoggerTimestamp {
/**
* The timestamp used to format the current date.
* @since 1.0.0
*/
timestamp: Timestamp;
/**
* Whether or not the logger will show a timestamp in UTC.
* @since 1.0.0
*/
utc: boolean;
/**
* The logger style to apply the color to the timestamp.
* @since 1.0.0
*/
color: LoggerStyle | null;
/**
* The final formatter.
* @since 1.0.0
*/
formatter: LoggerTimestampFormatter;
constructor(options?: LoggerTimestampOptions);
/**
* Formats the current time.
* @since 1.0.0
*/
run(): string;
}
/**
* The options for {@link LoggerTimestamp}.
* @since 1.0.0
*/
interface LoggerTimestampOptions {
/**
* The {@link Timestamp} pattern.
* @since 1.0.0
* @default 'YYYY-MM-DD HH:mm:ss'
* @example
* ```typescript
* 'YYYY-MM-DD HH:mm:ss'
* // 2020-12-23 22:01:10
* ```
*/
pattern?: string;
/**
* Whether or not the date should be UTC.
* @since 1.0.0
* @default false
*/
utc?: boolean;
/**
* The color to use.
* @since 1.0.0
* @default colorette.reset
*/
color?: LoggerStyleResolvable | null;
/**
* The formatter. See {@link LoggerTimestampFormatter} for more information.
* @since 1.0.0
* @default (value) => `${value} - `
*/
formatter?: LoggerTimestampFormatter;
}
/**
* The formatter used for {@link LoggerTimestampOptions}. This will be run **after** applying the color to the formatter.
* @since 1.0.0
*/
interface LoggerTimestampFormatter {
/**
* @param timestamp The output of {@link LoggerStyle.run} on {@link Timestamp.display}/{@link Timestamp.displayUTC}.
* @since 1.0.0
*/
(timestamp: string): string;
}
/**
* Logger utility that stores and applies a full style into the message.
* @since 1.0.0
*/
declare class LoggerLevel {
/**
* The timestamp formatter.
* @since 1.0.0
*/
timestamp: LoggerTimestamp | null;
/**
* The infix, added between the timestamp and the message.
* @since 1.0.0
*/
infix: string;
/**
* The style formatter for the message.
* @since 1.0.0
*/
message: LoggerStyle | null;
constructor(options?: LoggerLevelOptions);
run(content: string): string;
}
/**
* The options for {@link LoggerLevel}.
* @since 1.0.0
*/
interface LoggerLevelOptions {
/**
* The timestamp options. Set to `null` to disable timestamp parsing.
* @since 1.0.0
* @default {}
*/
timestamp?: LoggerTimestampOptions | null;
/**
* The infix to be included between the timestamp and the message.
* @since 1.0.0
* @default ''
*/
infix?: string;
/**
* The style options for the message.
* @since 1.0.0
* @default colorette.clear
*/
message?: LoggerStyleResolvable | null;
}
/**
* The logger class.
* @since 1.0.0
*/
declare class Logger extends Logger$1 {
/**
* The console this writes to.
* @since 1.0.0
*/
readonly console: Console;
/**
* The formats supported by the logger.
* @since 1.0.0
*/
readonly formats: Map<LogLevel, LoggerLevel>;
/**
* The string `write` will join values by.
* @since 1.0.0
*/
readonly join: string;
/**
* The inspect depth when logging objects.
* @since 1.0.0
*/
readonly depth: number;
constructor(options?: LoggerOptions);
/**
* Writes the log message given a level and the value(s).
* @param level The log level.
* @param values The values to log.
*/
write(level: LogLevel, ...values: readonly unknown[]): void;
/**
* Pre-processes an array of values.
* @since 1.0.0
* @param values The values to pre-process.
*/
protected preprocess(values: readonly unknown[]): string;
private get levels();
/**
* Gets whether or not colorette is enabled.
* @since 1.0.0
*/
static get stylize(): boolean;
private static createFormatMap;
private static ensureDefaultLevel;
}
/**
* The logger options.
* @since 1.0.0
*/
interface LoggerOptions {
/**
* A writable stream for the output logs.
* @since 1.0.0
* @default process.stdout
*/
stdout?: NodeJS.WritableStream;
/**
* A writable stream for the error logs.
* @since 1.0.0
* @default process.stderr
*/
stderr?: NodeJS.WritableStream;
/**
* The default options used to fill all the possible values for {@link LoggerOptions.format}.
* @since 1.0.0
* @default options.format.none ?? {}
*/
defaultFormat?: LoggerLevelOptions;
/**
* The options for each log level. LogLevel.None serves to set the default for all keys, where only
* {@link LoggerTimestampOptions.timestamp} and {@link LoggerLevelOptions.prefix} would be overridden.
* @since 1.0.0
* @default {}
*/
format?: LoggerFormatOptions;
/**
* The minimum log level.
* @since 1.0.0
* @default LogLevel.Info
*/
level?: LogLevel;
/**
* The string that joins different messages.
* @since 1.0.0
* @default ' '
*/
join?: string;
/**
* The inspect depth when logging objects.
* @since 1.0.0
* @default 0
*/
depth?: number;
}
/**
* The logger format options.
* @since 1.0.0
*/
interface LoggerFormatOptions {
/**
* The logger options for the lowest log level, used when calling {@link ILogger.trace}.
* @since 1.0.0
*/
trace?: LoggerLevelOptions;
/**
* The logger options for the debug level, used when calling {@link ILogger.debug}.
* @since 1.0.0
*/
debug?: LoggerLevelOptions;
/**
* The logger options for the info level, used when calling {@link ILogger.info}.
* @since 1.0.0
*/
info?: LoggerLevelOptions;
/**
* The logger options for the warning level, used when calling {@link ILogger.warn}.
* @since 1.0.0
*/
warn?: LoggerLevelOptions;
/**
* The logger options for the error level, used when calling {@link ILogger.error}.
* @since 1.0.0
*/
error?: LoggerLevelOptions;
/**
* The logger options for the critical level, used when calling {@link ILogger.fatal}.
* @since 1.0.0
*/
fatal?: LoggerLevelOptions;
/**
* The logger options for an unknown or uncategorised level.
* @since 1.0.0
*/
none?: LoggerLevelOptions;
}
declare module '@sapphire/framework' {
interface ClientLoggerOptions extends LoggerOptions {
}
}
/**
* The [@sapphire/plugin-logger](https://github.com/sapphiredev/plugins/blob/main/packages/logger) version that you are currently using.
* An example use of this is showing it of in a bot information command.
*
* Note to Sapphire developers: This needs to explicitly be `string` so it is not typed as the string that gets replaced by esbuild
*/
declare const version: string;
export { Logger, type LoggerFormatOptions, LoggerLevel, type LoggerLevelOptions, type LoggerOptions, LoggerStyle, LoggerStyleBackground, LoggerStyleEffect, type LoggerStyleOptions, type LoggerStyleResolvable, LoggerStyleText, LoggerTimestamp, type LoggerTimestampFormatter, type LoggerTimestampOptions, version };