Logger
Last updated:
Toolbox: Logger
The logger provides methods for outputting messages at different log levels.
Basic Usage
cli.addCommand({
name: "example",
execute: ({ logger }) => {
logger.info("Information message");
logger.error("Error message");
logger.warn("Warning message");
logger.debug("Debug message");
},
});Log Levels
Info
General information messages:
logger.info("Building project...");
logger.info("Files processed:", fileCount);Error
Error messages:
logger.error("Failed to build");
logger.error("Error details:", error);Warn
Warning messages:
logger.warn("Deprecated option used");
logger.warn("This feature will be removed in v2.0");Debug
Debug messages (only shown with --debug flag):
logger.debug("Processing file:", fileName);
logger.debug("Options:", options);Logging Objects
Logger methods accept multiple arguments:
logger.info("User:", user.name, "Age:", user.age);
logger.error("Error:", error.message, error.stack);Verbosity Control
Log levels are controlled by verbosity flags:
cli command # Shows info, warn, error
cli command --verbose # Shows debug too
cli command --debug # Shows all debug messages
cli command --quiet # Minimal outputCustom Logger
Provide a custom logger when creating the CLI:
const customLogger = {
info: (...args) => console.log("[INFO]", ...args),
error: (...args) => console.error("[ERROR]", ...args),
warn: (...args) => console.warn("[WARN]", ...args),
debug: (...args) => {
if (process.env.DEBUG) {
console.debug("[DEBUG]", ...args);
}
},
};
const cli = new Cerebro("my-app", {
logger: customLogger,
});Conditional Logging
Use log levels appropriately:
execute: ({ logger, options }) => {
if (options.verbose) {
logger.debug("Detailed processing information");
}
logger.info("Starting operation...");
try {
// Operation
} catch (error) {
logger.error("Operation failed:", error);
}
};Best Practices
- Use appropriate levels - info for normal output, error for errors
- Don't overuse debug - Reserve for troubleshooting
- Include context - Log relevant information
- Format consistently - Use consistent message formats
- Respect verbosity - Don't force output in quiet mode