A colorful logger with the ability to log "Processing ... done".
Basic logging and formatted output for your application:
import { format, Logger } from "@m234/logger";
const logger = new Logger({ prefix: "MyApp" });
logger.print("Hello, World!");
logger.println("Hello, World!");
logger.print(format("Hello, World! %o", true));
logger.info("This is an informational message.");
logger.warn("This is a warning.");
logger.error("This is an error.");
logger.success("This is a success message.");Track the progress and result of long-running or multi-step operations. Task printing provides visual feedback for ongoing, successful, skipped, failed, or aborted tasks:
import { Logger } from "@m234/logger";
const logger = new Logger({ prefix: "MyApp" });
using task = logger.task({
text: "Operating",
disposeState: "completed",
}).start();
// Output: - MyApp Operating ...
task.end("completed");
// Output: ✓ MyApp Operating ... done
task.end("skipped");
// Output: ✓ MyApp Operating ... skipped
task.end("failed");
// Output: ✗ MyApp Operating ... failed
task.end("aborted");
// Output: ⚠ MyApp Operating ... abortedAutomate task execution and handle asynchronous operations with task runners. This feature allows you to run a function as a task, automatically updating the task status based on the function's result:
import { Logger } from "@m234/logger";
import { delay } from "@std/async/delay";
const logger = new Logger({ prefix: "MyApp" });
logger.task({
text: "Operating",
}).startRunner(async ({ task }) => {
await delay(1000);
return "completed";
});Warning
Any exceptions thrown within the task runner aren't printed by default.
If you want to log exceptions, you can use the printErrors wrapper:
import { Logger, printErrors } from "@m234/logger";
import { delay } from "@std/async/delay";
const logger = new Logger({ prefix: "MyApp" });
logger.task({
text: "Operating",
}).startRunner(printErrors(
logger,
async ({ task }) => {
await delay(1000);
throw new Error("test");
},
"warn", // optional, defaults to "error"
));- scripts/example-ora.ts:
Demonstrates integrating the logger with the
oraspinner for animated task progress, updating the spinner frame in real time and marking the task as completed after a delay. - scripts/example-ora-suffix.ts:
Shows a custom
Taskclass that appends anoraspinner animation as a suffix to the task text, including multiple tasks and subtasks with different completion states. - scripts/example-stress.ts: Runs a stress test by creating and updating the state of 20 tasks in rapid succession, randomly changing their states to simulate heavy usage and concurrent task updates.
