Simple logger for textChannels.
See NPM
npm i discord-textchannel-logger
import { initLogger, Logger } from 'discord-textchannel-logger';
initLogger(getClient());//INIT ONLY ONCE
let loggerOne: Logger = new Logger(
{
textChannelID: "idTextChannel",
consoleLog: true,
customHeaderConsole: "=== CONSOLE CUSTOM HEADER ===",
printCurrentTimeConsole:true,
}
);
loggerOne.log('Hello World');
let loggerTwo: Logger = new Logger(
{
textChannelID: "idTextChannel",
customHeader: "=== TEXT CHANNEL CUSTOM HEADER ===",
}
);
loggerTwo.log('Hello World 2');
export {loggerOne,loggerTwo};
discord.js: ^14.2.0
circular-reference-remover: ^1.0.5
-
Function
initLogger(client:Client):void
- Accepts the Client your bot is connected to. Need to init once. -
Class
Logger
- Class to send messages to textChannels and console. NeedsInitOptions
on constructor, if it's an invalid value, it'll throw aTextChannelNotFound
. -
- Function
log(value:any):void
- Sendvalue
(if notundefined
) to configured textChannel onInitOptions
and print onconsole
(if configured onInitOptions
). Ifvalue
it's a typeofobject
, it'll try toJSON.stringify
it, unless, if it's anError
instance, it'll print its stack trace. In case ofJSON.stringify
failure it'll use theobject.toString
.
- Function
-
- Function
trace(value:any):void
- Sendvalue
(if notundefined
) to configured textChannel onInitOptions
and print onconsole
withconsole.trace
(if configured onInitOptions
). It'll do the same value conversion onlog
to send toTextChannel
, except it'll trace the raw value — passed to this function — onconsole
.
- Function
-
Options
Logger.InitOptions
- -
console.log?:boolean
- If true print the value on console. Valid for bothlog
andtrace
-
printCurrentTimeConsole?:boolean
- If true print the current time astoLocaleString
on console. If it's true, prints after the custom headers. Example:'customHeaderIfExists\n' + 14/12/2021 10:39:54 ==> 'message'
-
jsonSpace?:number
- The space indent of JSON.stringify onlog
.Default: 2
-
textChannelID:string
- (Obligatory) ID of the textChannel to print on.
-
customHeader?:string
- Custom log header to print along before the message
-
customHeaderConsole?:string
- Custom log header to print on console before the message
-
Errors
- -
NoClientError
- In case there's no client configured withinitLogger
-
TextChannelNotFound
- If the ID of thetextChannel
does not resolve to an existent channel
-
InvalidTextChannel
- If the ID of thetextChannel
resolve to a non-textChannel
Pull requests are welcome, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.