@mengkodingan/consolefy
is a customizable logging library π.
npm i @mengkodingan/consolefy
# or
yarn add @mengkodingan/consolefy
# or
pnpm add @mengkodingan/consolefy
import { Consolefy } from "@mengkodingan/consolefy";
const consolefy = new Consolefy();
consolefy.log("Just a regular log.");
consolefy.info("This is an info log.");
consolefy.success("Operation was successful!");
consolefy.warn("This is a warning.");
consolefy.error("An error occurred.");
You can customize the prefixes, formats, themes, and tags in the consolefy's configuration.
import { Colors, Consolefy } from "@mengkodingan/consolefy";
const consolefy = new Consolefy({
prefixes: {
warn: "Caution",
success: "Done",
error: "Oops",
info: "Heads up",
},
theme: {
warn: (text) => Colors.bgYellow(Colors.black(text)),
success: (text) => Colors.bgGreen(Colors.black(text)),
error: (text) => Colors.bgRed(Colors.black(text)),
info: (text) => Colors.bgBlue(Colors.black(text)),
},
format: "{prefix}{tag} {message}",
tag: "APP",
});
consolefy.info("Info log with custom tag and theme.");
consolefy.success("Custom success log.");
You can also combine both setPrefix()
and setTheme()
to customize both the prefix and theme for specific log levels dynamically:
import { Colors, Consolefy } from "@mengkodingan/consolefy";
const consolefy = new Consolefy();
// Set a custom prefix and theme for the 'info' log level
consolefy.setPrefix("info", "INFORMATION");
consolefy.setTheme("info", (text) => Colors.bgMagenta(Colors.black(text)));
consolefy.info("This is an informational message with a custom prefix and theme.");
Using setPrefix()
and setTheme()
methods, you can change the behavior and appearance of your log messages at any point, giving you full flexibility to adapt the logger to different contexts during runtime.
You can group related logs together using the group()
and groupEnd()
methods.
const consolefy = new Consolefy();
consolefy.group("Initialization");
consolefy.info("Initializing the application...");
consolefy.success("Initialization complete.");
consolefy.groupEnd();
You can define new log levels dynamically by using the defineLogLevel()
method.
import { Colors, Consolefy } from "@mengkodingan/consolefy";
const consolefy = new Consolefy();
// Define a new log level "debug"
consolefy.defineLogLevel("debug", { prefix: "DEBUG", theme: (text) => Colors.bgRedBright(Colors.black(text)) });
consolefy.log("debug", "This is a debug message.");
To disable logging output entirely, you can set the logger to silent mode.
const consolefy = new Consolefy();
consolefy.silent(true);
consolefy.info("This log will not be printed because silent mode is enabled.");
consolefy.silent(false);
consolefy.info("This log will be printed.")
You can reset the format and tag back to their default values.
const consolefy = new Consolefy();
// Change the format and tag
consolefy.setFormat("{prefix} - {message}");
consolefy.setTag("CUSTOM_TAG");
// Reset to defaults
consolefy.resetFormat();
consolefy.resetTag();
For more usage examples and demonstrations of different features, please refer to the example/example.ts file.
Creates a new instance of the logger with optional configuration.
prefixes
: Custom prefixes for different log levels (warn
,success
,error
,info
, etc.)format
: Custom format for log messages (default:{prefix}{tag} {message}
)silent
: Whether to suppress all logging (default:false
)tag
: Custom tag to be appended to each log messagetheme
: Custom themes for each log level (functions that receive the log message and return the styled message)
Sets the configuration of the logger.
Sets a custom prefix for the specified log level.
Sets a custom theme (color/style) for the specified log level.
Sets a custom log message format.
Resets the log message format to the default.
Sets a custom tag to be appended to each log message.
Resets the log tag to the default (empty).
Enables or disables silent mode.
Defines a custom log level with a specific prefix and theme.
Begins a log group with the given name.
Ends the current log group.
Logs a message at the specified log level.
Logs a warning message.
Logs a success message.
Logs an error message.
Logs an informational message.