A terminal logging solution.
Terminok is a logging solution that outputs colorful logs to the terminal or saves them to a file. It supports both web and Node environments, with additional runtime compatibility based on your configuration.
Install Terminok as a dependency:
# NPM
npm install terminok
# Yarn
yarn add terminok
# PNPM
pnpm add terminok
You may create a client with default schema as follows:
import type { Client } from "terminok";
import { createClient, SCHEMA_DEFAULT } from "terminok";
const log: Client<typeof SCHEMA_DEFAULT> = createClient({
schema: SCHEMA_DEFAULT,
});
log.info("Hello");
Or create your own schema with the following code:
import type { Client, Schema } from "terminok";
import { createClient, COLORS } from "terminok";
const customSchema = [
{
key: "ping",
color: COLORS.red,
}
] as const satisfies Schema;
const log: Client<typeof customSchema> = createClient({
schema: customSchema,
});
log.ping("pong");
Terminok provides a highly customizable config and schema, so you can create your own logger easily. But you may meet some problems during the creation.
If the schema is empty, it will return nothing as a result. So you should import the default schema or create your own schema.
import { createClient } from "terminok";
const log = createClient();
// Nothing will be inside `log`
To customize how the log will be displayed,
you may edit format
parameter in config to customize the output.
import type { Client, FormatData } from "terminok";
import { createClient, SCHEMA_DEFAULT } from "terminok";
const log: Client<typeof SCHEMA_DEFAULT> = createClient({
schema: SCHEMA_DEFAULT,
config: {
format: (data: FormatData) => {
return `- [${data.title}] ${data.content}`;
},
}
});
log.info("Hello");
If you set onDone
/onTrigger
on both config and schema,
both of them will be executed.
And the onDone
/onTrigger
function in config
will be executed first.
import type { Client, Schema } from "terminok";
import { createClient, COLORS } from "terminok";
const customSchema = [
{
key: "ping",
color: COLORS.red,
onDone: (): void => {
// executed second
}
},
] as const satisfies Schema;
const log: Client<typeof customSchema> = createClient({
schema: customSchema,
config: {
onDone: (): void => {
// executed first
}
}
});
If you set output
on both config and schema,
the output
config in config
will be
overrided by the output
config in schema
.
import type { Client, Schema } from "terminok";
import { createClient, COLORS } from "terminok";
const customSchema = [
{
key: "ping",
color: COLORS.red,
},
{
key: "ping2",
color: COLORS.red,
output: {
// ...
}
}
] as const satisfies Schema;
const log: Client<typeof customSchema> = createClient({
schema: customSchema,
config: {
output: {
// ...
}
}
});
await log.ping("pong").toFile(); // run with config `output` settings
await log.ping2("pong").toFile(); // run with schema `output` settings
While Terminok supports both web and Node.js environments,
the toFile()
function is exclusive for Node.js environments by default.
Using toFile()
in web will lead to an error.
import type { Client } from "terminok";
import { createClient, SCHEMA_DEFAULT } from "terminok";
const log: Client<typeof SCHEMA_DEFAULT> = createClient({
schema: SCHEMA_DEFAULT,
});
await log.info("Hello").toFile();
// This will lead to an error in web with default config
This project is MIT licensed, you can find the license file here.