Bot / structs/Logger / Logger
structs/Logger.Logger
-
Logger↳
Logger
• new Logger(options?)
Initializes a new Logger instance.
| Name | Type | Description |
|---|---|---|
options? |
Partial<Options> |
Options for the logger. |
BaseLogger.constructor
• options: DeepRequired<Options>
Options to reference when logging. Represents default options along with specific options overridden within the constructor.
BaseLogger.options
node_modules/.pnpm/@norviah+logger@7.0.0/node_modules/@norviah/logger/lib/structs/Logger.d.ts:34
▸ debug(content, options?): void
Logs the provided content as debug information.
Debug information are information that isn't necessarily important, but can be useful for debugging purposes.
| Name | Type | Description |
|---|---|---|
content |
string | string[] |
The content to log. |
options? |
Partial<LoggingOptions> |
Options for the log. |
void
▸ error(content, options?): void
A shortcut method for logging an error event.
| Name | Type | Description |
|---|---|---|
content |
string | string[] |
The content to log. |
options? |
Partial<LoggingOptions> |
Options for the log. |
void
BaseLogger.error
▸ exit(content, options?): never
A utility method for logging an error event and exiting the process.
| Name | Type | Description |
|---|---|---|
content |
string | string[] |
The content to log. |
options? |
Partial<LoggingOptions> & { code?: number } |
Options for the log. |
never
▸ generate(content, options?): string
A helper method for printing logs.
As logs are split into three separate functions, generate helps combine
them, ultimately generating a string representing all three sections. Each
section:
- the date,
- the title,
- and the contents
are all ran through its specific formatter. For example, for dates, a new
instance of spacetime is initialized. For titles, any instances of %t
within Format.title is replaced with the specified title, or an empty
string if no title is set.
After generating the three sections, generate combines them using the
general format within Format and returns the result.
| Name | Type | Description |
|---|---|---|
content |
string | string[] |
The contents of the log. |
options? |
Partial<LoggingOptions> |
Options for logging. |
string
A generated string adhering to the desired format of a log.
Example
// With `generate`, it takes the desired `Format` instance, replaces the
// special characters with the date, title, and content, applies any colors
// and markdown syntax, and finally returns the result.
// As an example, let's say we have set the desired format into:
// date: '[ MM-dd-yyyy h:mm a ]',
// {
// title: '%t: ',
// general: '%d %t%c',
// }
// If we were to call the `print` method as:
// print('world', { title: 'hello' }), it would call `generate` with the
// same parameters, and would go through each property within `format` and
// generate a string regarding that property.
// With the given options, `generate` would return:
// `[ 01-01-1970 12:00 AM ] hello: world`BaseLogger.generate
node_modules/.pnpm/@norviah+logger@7.0.0/node_modules/@norviah/logger/lib/structs/Logger.d.ts:109
▸ info(content, options?): void
A helper method to print information.
The title is set to info with the color set to blue.
| Name | Type | Description |
|---|---|---|
content |
string | string[] |
The content of the log. |
options? |
Partial<LoggingOptions> |
Options for logging. |
void
Example
import { Logger } from '@norviah/logger';
new Logger().info('sample text');
// => [ 01-01-1970 12:00 AM ] info: sample textBaseLogger.info
node_modules/.pnpm/@norviah+logger@7.0.0/node_modules/@norviah/logger/lib/structs/Logger.d.ts:251
▸ print(content, options?): void
Prints the given content to the console.
The main function for Logger, print is the main entry point to
logging into the console. Outputted logs are formatted to adhere to the
format specified within Format in Options during initialization,
or the default format.
Through options, you're able to change certain output of the log,
for example, colors. As logs are separated within three sections, if
desired, you can specify the colors for each section within
options.colors.
Speaking of sections of a log, the sections of a log are:
- the date,
- the title,
- and the content
As the date is handled within Options during the constructor, we don't
have to worry about that. As for the title, that can be set within
options. Titles for logs are always optional and is ignored within the
output if a title isn't set.
Additionally, markdown syntax are supported.
Within content or options.title, you can specify one or more of the
following markdown syntax:
- bold:
**[string]**, strikethrough:~~[string]~~,- italics:
*[string]*, - underline:
__[string]__, and !![string]!!, which inverses the foreground and background color.
The desired syntax is applied to the wrapped strings.
| Name | Type | Description |
|---|---|---|
content |
string | string[] |
The content of the log. |
options? |
Partial<LoggingOptions> |
Options for logging. |
void
BaseLogger.print
node_modules/.pnpm/@norviah+logger@7.0.0/node_modules/@norviah/logger/lib/structs/Logger.d.ts:146
▸ success(content, options?): void
A helper method to print a success.
The title is set to ok with the color set to green.
| Name | Type | Description |
|---|---|---|
content |
string | string[] |
The content of the log. |
options? |
Partial<LoggingOptions> |
Options for logging. |
void
Example
import { Logger } from '@norviah/logger';
new Logger().success('sample text');
// => [ 01-01-1970 12:00 AM ] ok: sample textBaseLogger.success
node_modules/.pnpm/@norviah+logger@7.0.0/node_modules/@norviah/logger/lib/structs/Logger.d.ts:223
▸ warn(content, options?): void
A helper method to print a warning.
The title is set to warning with the color set to yellow.
| Name | Type | Description |
|---|---|---|
content |
string | string[] |
The content of the log. |
options? |
Partial<LoggingOptions> |
Options for logging. |
void
Example
import { Logger } from '@norviah/logger';
new Logger().warn('sample text');
// => [ 01-01-1970 12:00 AM ] warning: sample textBaseLogger.warn
node_modules/.pnpm/@norviah+logger@7.0.0/node_modules/@norviah/logger/lib/structs/Logger.d.ts:237
▸ write(content, options?): void
Writes the given contents into a file.
As Logger can optionally save logs into a file, determined by the write
property within Options, this method is called under the hood to save the
specified log into a file. Given the contents and options, the generated
log that would be printed, is rather saved into the specified file.
As always, this method exists if you would like to save a log into a file,
regardless if the write property is set to true within the constructor.
| Name | Type | Description |
|---|---|---|
content |
string | string[] |
The contents of the log. |
options? |
Partial<LoggingOptions> |
Options for logging. |
void
Example
import { Logger } from '@norviah/logger';
// For this example, we'll initialize a new instance of `Logger` without
// providing any options. By default, any printed logs won't be saved into
// a file.
const logger: Logger = new Logger();
logger.print('world', { title: 'hello' });
// This would simply print out:
// [ 01-01-1970 12:00 AM ] hello: world
// No logs are saved due to `write` being false.
// However, if `write` were to be set to `true`, it would save files by
// calling the `write` method under the hood.
// This method is always available to you, allowing you to save logs into
// files regardless of what `write` was set to during the constructor.
// `write` accepts the same parameters as the `print` method, calling the
// same generator method but instead saving the log into a file.
logger.write('world', { title: 'hello' });
// By default, logs are saved into the `logs` subdirectory within your
// projects root directory under the file `MM-DD-YYYY.txt`. If wanted, you
// can change the filename using the `name` property:
logger.write('hello world', { name: 'hello world' });
// This log would be saved under `logs/hello world.txt`.
// There's other options available regarding writing logs within the
// `LoggingOptions` interface, as it inherits `WriteOptions`.BaseLogger.write
node_modules/.pnpm/@norviah+logger@7.0.0/node_modules/@norviah/logger/lib/structs/Logger.d.ts:195
▸ Static Colorize(color, string): string
Applies the specified color to the string.
In addition, any markdown syntax specified is also applied to the provided string.
| Name | Type | Description |
|---|---|---|
color |
Color |
The desired color to apply. |
string |
string |
The string to apply the color to. |
string
The string with the desired color applied.
Example
import { Logger } from '@norviah/logger';
Logger.Colorize('red', 'hello world');
// => <red>hello world</red>BaseLogger.Colorize
node_modules/.pnpm/@norviah+logger@7.0.0/node_modules/@norviah/logger/lib/structs/Logger.d.ts:268
▸ Static Plain(string): string
Removes all ANSI codes from the provided string.
Logger uses chalk to generate strings with custom colors and types,
which implements ANSI codes to achieve this. Plain can take in a string
generated from Logger and remove all ANSI codes from it, resulting in a
plain text version of the string.
| Name | Type | Description |
|---|---|---|
string |
string |
The string to remove ANSI codes from. |
string
The string with all ANSI codes removed.
Example
let ansiText = "\x1B[34mThis text is blue\x1B[39m";Here, ansiText is a string with the ANSI code for blue color and the
ANSI code for resetting the color. Plain can be used to remove these
ANSI codes, resulting in a plain text version of the string.
console.log(Plain(ansiText));While the change isn't noticable here in markdown, the output of the
above code will be This text is blue without any ANSI codes.
BaseLogger.Plain
node_modules/.pnpm/@norviah+logger@7.0.0/node_modules/@norviah/logger/lib/structs/Logger.d.ts:291