76 lines
2.2 KiB
Markdown
76 lines
2.2 KiB
Markdown
# @nixkrystik/logger
|
|
A heavily stripped down logging library for JavaScript & TypeScript.
|
|
|
|
## Introduction
|
|
This is a rather lightweight, bare-bones package. It's designed to be as frictionless as possible while abstracting multiple transports into simple functions.
|
|
|
|
### Example Transport (Using [chalk](https://github.com/chalk/chalk))
|
|
```typescript
|
|
import { Logger, LogLevel, Transport } from "@nixkrystik/logger";
|
|
import chalk, { ChalkInstance } from "chalk";
|
|
|
|
class ConsoleTransport implements Transport {
|
|
#colors: Record<LogLevel, ChalkInstance> = {
|
|
[LogLevel.Debug]: chalk.cyan,
|
|
[LogLevel.Information]: chalk.green,
|
|
[LogLevel.Warning]: chalk.yellow,
|
|
[LogLevel.Error]: chalk.red,
|
|
[LogLevel.Fatal]: chalk.bgRed.white,
|
|
};
|
|
|
|
#names: Record<LogLevel, string> = {
|
|
[LogLevel.Debug]: "DEBUG",
|
|
[LogLevel.Information]: "INFO",
|
|
[LogLevel.Warning]: "WARN",
|
|
[LogLevel.Error]: "ERROR",
|
|
[LogLevel.Fatal]: "FATAL",
|
|
};
|
|
|
|
async log(level: LogLevel, module: string, content: string) {
|
|
console.log(
|
|
`[ ${new Date().toISOString().replace("T", " ").replace("Z", "")} ] [ ${this.#colors[level](this.#names[level])} ] >> [ ${chalk.magenta(module)} ]: ${content}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
export default new Logger([new ConsoleTransport()]);
|
|
```
|
|
|
|
---
|
|
|
|
### Transport.minLevel
|
|
Each transport can restrict the minimum required level before handling logs.
|
|
|
|
```typescript
|
|
import { LogLevel, Transport } from "@nixkrystik/logger";
|
|
|
|
class ExampleTransport implements Transport {
|
|
minLevel = LogLevel.Warning;
|
|
|
|
async log(level: LogLevel, module: string, content: string) {
|
|
console.log('I only print on "warning" logs!');
|
|
}
|
|
}
|
|
```
|
|
|
|
### Transport.log(level, module, content)
|
|
When handling transports, each logger is able to run asynchronously.
|
|
|
|
```typescript
|
|
import { LogLevel, Transport } from "@nixkrystik/logger";
|
|
|
|
class ExampleTransport implements Transport {
|
|
minLevel = LogLevel.Warning;
|
|
|
|
async log(level: LogLevel, module: string, content: string) {
|
|
console.log('I only print on "warning" logs!');
|
|
}
|
|
}
|
|
```
|
|
|
|
## Contributing
|
|
Keep issues and PRs in English.
|
|
|
|
I don't entirely mind what contributions are made. Unless it's extremely important to add an extenal package, please keep them out :)
|
|
|
|
If you're unsure about adding a new feature, you can create an issue. |