Consola

repository·main·Indexed 27 days ago

https://github.com/unjs/consola

An elegant console wrapper providing pluggable and consistent logging across Node.js and Browser environments. Consola 3.4.2 features various log types (info, start, warn, success, error, box), interactive prompts, and customizable reporters. It includes lightweight builds (basic, browser, core), the ability to redirect stdout/stderr, and integration support for Jest, Vitest, and jsdom.

Tokens
4K
Snippets
11
Records
34
Agent score
91%

What's inside consola

  1. Get started with Consola

    main

    Import the global consola instance or use createConsola to create a new instance. Consola provides various log types like info, start, warn, success, error, and box. It also supports interactive prompts via prompt.

    // ESM
    import { consola, createConsola } from "consola";
    
    // CommonJS
    const { consola, createConsola } = require("consola");
    
    consola.info("Using consola 3.0.0");
    consola.start("Building project...");
    consola.warn("A new version of consola is available: 3.0.1");
    consola.success("Project built!");
    consola.error(new Error("This is an example error. Everything is fine!"));
    consola.box("I am a simple box");
    await consola.prompt("Deploy to the production?", {
      type: "confirm",
    });
  2. Use lightweight Consola builds

    main

    To reduce bundle size (up to 80%), you can use smaller core builds instead of the full package. Available entry points include consola/basic, consola/browser, and consola/core.

    import { consola, createConsola } from "consola/basic";
    import { consola, createConsola } from "consola/browser";
    import { createConsola } from "consola/core";
  3. Integrate Consola with Jest or Vitest

    main

    To test Consola output, wrap the environment and mock the types in your test setup.

    describe("your-consola-mock-test", () => {
      beforeAll(() => {
        // Redirect std and console to consola too
        consola.wrapAll();
      });
    
      beforeEach(() => {
        // Re-mock consola before each test call
        // Jest
        consola.mockTypes(() => jest.fn());
        // Vitest
        consola.mockTypes(() => vi.fn());
      });
    
      test("your test", async () => {
        // ... code ...
        const consolaMessages = consola.log.mock.calls.map((c) => c[0]);
        expect(consolaMessages).toContain("your message");
      });
    });
  4. Configure log levels

    main

    Consola only shows logs with the configured level or below. The default level is 3. You can set the level via createConsola({ level: X }), consola.level = X, or the CONSOLA_LEVEL environment variable (not supported in browser/core builds).

    Available levels:

    • 0: Fatal and Error
    • 1: Warnings
    • 2: Normal logs
    • 3: Informational logs, success, fail, ready, start, ...
    • 4: Debug logs
    • 5: Trace logs
    • -999: Silent
    • +999: Verbose logs
  5. Use the prompt method

    main

    The await prompt(message, { type, cancel }) method shows an input prompt. Supported types are text, confirm, select, or multiselect.

    If the user cancels (Ctrl+C), the behavior is determined by the cancel option:

    • "default": Resolves with the default or initial value.
    • "undefined": Resolves with undefined.
    • "null": Resolves with null.
    • "symbol": Resolves with Symbol.for("cancel").
    • "reject": Rejects the promise with an error.
  6. Redirect console and stdout/stderr to Consola

    main

    Consola can globally intercept standard output methods:

    • wrapConsole(): Redirects console.log, etc., to Consola.
    • wrapStd(): Redirects stdout/stderr to Consola.
    • wrapAll(): Redirects both console and std (ensuring console.info maps to the correct type).

    Use restoreConsole(), restoreStd(), or restoreAll() to revert these changes.

  7. Create new Consola instances

    main
    Use create(options) to create a new Consola instance that inherits parent options, or withDefaults(defaults) to create an instance with specific defaults. You can also use withTag(tag) (alias withScope) to create a scoped instance with a specific tag.
  8. Use raw logging to prevent object interpretation

    main

    If you want to log an object that contains message or args properties without Consola interpreting them as log metadata, use the .raw() method chained to any log type.

    // Prints "hello"
    consola.log({ message: "hello" });
    
    // Prints "{ message: 'hello' }"
    consola.log.raw({ message: "hello" });
  9. Use Consola utility functions

    main

    Consola provides several string and color utilities via consola/utils:

    • stripAnsi
    • centerAlign, rightAlign, leftAlign, align
    • box
    • colors, getColor, colorize
    // ESM
    import {
      stripAnsi,
      centerAlign,
      rightAlign,
      leftAlign,
      align,
      box,
      colors,
      getColor,
      colorize,
    } from "consola/utils";