Getting Started
Introduction
A Logger implementation that implements Sapphire's ILogger
interface and implements timestamp and style formatting
with the blazing fast colorette
library.
Installation
- npm
- yarn
- pnpm
npm install @sapphire/plugin-logger @sapphire/framework
yarn add @sapphire/plugin-logger @sapphire/framework
pnpm add @sapphire/plugin-logger @sapphire/framework
Usage
This registers the necessary options and methods in the Sapphire client to be able to use the log plugin.
- CommonJS
- ESM
- TypeScript
// Main bot file
// Be sure to register the plugin before instantiating the client.
require('@sapphire/plugin-logger/register');
// Main bot file
// Be sure to register the plugin before instantiating the client.
import '@sapphire/plugin-logger/register';
// Main bot file
// Be sure to register the plugin before instantiating the client.
import '@sapphire/plugin-logger/register';
In order to use the Logger in any place other than a piece (commands, arguments, preconditions, etc.), you must first
import the container
property of @sapphire/framework
. For pieces, you can simply use this.container.logger
to
access Logger methods.
- CommonJS
- ESM
- TypeScript
const { container } = require('@sapphire/framework');
class MyAwesomeService {
printAwesomeLog() {
container.logger.info('log message');
}
}
module.exports = {
MyAwesomeService
};
import { container } from '@sapphire/framework';
export class MyAwesomeService {
printAwesomeLog() {
container.logger.info('log message');
}
}
import { container } from '@sapphire/framework';
export class MyAwesomeService {
public printAwesomeLog() {
container.logger.info('log message');
}
}
Here is an example ping command, demonstrating the use of this.container.logger
from within a piece by omitting the
explicit import.
- CommonJS
- ESM
- TypeScript
// ping command
const { Command } = require('@sapphire/framework');
class PingCommand extends Command {
constructor(context, options) {
super(context, {
...options,
description: 'ping pong'
});
}
async messageRun(message) {
this.container.logger.warn('warning message');
}
}
module.exports = {
PingCommand
};
// ping command
import { Command } from '@sapphire/framework';
export class PingCommand extends Command {
constructor(context, options) {
super(context, {
...options,
description: 'ping pong'
});
}
async messageRun(message) {
this.container.logger.warn('warning message');
}
}
// ping command
import { Command } from '@sapphire/framework';
import type { Message } from 'discord.js';
export class PingCommand extends Command {
public constructor(context: Command.LoaderContext, options: Command.Options) {
super(context, {
...options,
description: 'ping pong'
});
}
public async messageRun(message: Message) {
this.container.logger.warn('warning message');
}
}
Types of logs
trace
debug
info
warn
error
fatal
Example:
container.logger.debug('log debug message');