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 the configuration.
Install this package as a dependency in the project:
# npm
npm i terminok
# Yarn
yarn add terminok
# pnpm
pnpm add terminokCreate 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 a custom 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");For more information, please refer to the documentation.
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` settingsWhile 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 configThis project is licensed under the terms of the MIT license.