Logit API Reference
Complete API documentation for @vielzeug/logit.
Core Logging Methods
All logging methods accept multiple arguments of any type and return void.
Logit.debug(...args)
Logs debug-level messages. Useful for detailed debugging information.
Level: debug (lowest)
Example:
Logit.debug('Variable value:', myVar);
Logit.debug('Debug info', { state: appState, config: settings });Logit.info(...args)
Logs informational messages about application flow and state.
Level: info
Example:
Logit.info('Application started');
Logit.info('User action', { userId: '123', action: 'login' });Logit.success(...args)
Logs success messages, typically for completed operations.
Level: success
Example:
Logit.success('Data saved successfully');
Logit.success('Upload complete', { files: 5, size: '2.3MB' });Logit.warn(...args)
Logs warning messages for potentially problematic situations.
Level: warn
Example:
Logit.warn('Deprecated API used');
Logit.warn('High memory usage', { usage: '85%', threshold: '80%' });Logit.error(...args)
Logs error messages for failures and exceptions.
Level: error
Example:
Logit.error('Failed to fetch data', new Error('Network timeout'));
Logit.error('Validation failed', { errors: validationErrors });Logit.trace(...args)
Logs trace-level messages with detailed execution information.
Level: trace
Example:
Logit.trace('Function called', { args: arguments, caller: functionName });Utility Methods
Logit.table(...args)
Displays data in a table format in the console.
Level: table
Parameters:
...args: any[]- Data to display (typically arrays of objects)
Example:
const users = [
{ id: 1, name: 'Alice', role: 'Admin' },
{ id: 2, name: 'Bob', role: 'User' },
];
Logit.table(users);
// Can also pass multiple tables
Logit.table(users, permissions);Logit.time(label)
Starts a timer with the specified label.
Level: time
Parameters:
label: string- Unique identifier for the timer
Example:
Logit.time('database-query');
await db.query('SELECT * FROM users');
Logit.timeEnd('database-query'); // Logs elapsed timeLogit.timeEnd(label)
Stops a timer and logs the elapsed time.
Level: time
Parameters:
label: string- Timer label (must match thetime()call)
Example:
Logit.time('operation');
performExpensiveOperation();
Logit.timeEnd('operation'); // Output: "operation: 142ms"Logit.groupCollapsed(text, label?, time?)
Creates a collapsed group in the console for organizing related logs.
Level: Uses success level for filtering
Parameters:
text: string- Main group label textlabel?: string- Optional prefix label (default:'GROUP')time?: number- Optional start time for elapsed time display (default:Date.now())
Example:
Logit.groupCollapsed('User Details', 'INFO');
Logit.info('Name:', user.name);
Logit.info('Email:', user.email);
Logit.info('Roles:', user.roles);
Logit.groupEnd();
// With timing
const startTime = Date.now();
Logit.groupCollapsed('API Response', 'DEBUG', startTime);
Logit.debug('Status:', response.status);
Logit.debug('Data:', response.data);
Logit.groupEnd();Logit.groupEnd()
Ends the current console group.
Level: Uses success level for filtering
Example:
Logit.groupCollapsed('Processing');
Logit.info('Step 1 complete');
Logit.info('Step 2 complete');
Logit.groupEnd(); // Ends the groupLogit.assert(valid, message, context)
Logs an assertion error if the condition is false.
Parameters:
valid: boolean- Condition to checkmessage: string- Error message if condition failscontext: Record<string, any>- Additional context data
Example:
const isAuthenticated = checkAuth();
Logit.assert(isAuthenticated, 'User must be authenticated', {
userId: user?.id,
endpoint: '/protected-route',
});
// If isAuthenticated is false, logs to console:
// Assertion failed: User must be authenticated
// { userId: undefined, endpoint: '/protected-route' }Configuration Methods
Logit.initialise(options)
Initializes or updates Logit configuration with multiple options at once.
Parameters:
options: LogitOptions- Configuration object
Example:
Logit.initialise({
logLevel: 'info',
namespace: 'MyApp',
variant: 'symbol',
timestamp: true,
environment: true,
remote: {
handler: async (type, ...args) => {
await sendToServer(type, args);
},
logLevel: 'error',
},
});Logit.setLogLevel(level)
Sets the minimum log level to display.
Parameters:
level: LogitLevel- Minimum level to show
Example:
Logit.setLogLevel('warn'); // Only show warn and error
Logit.setLogLevel('debug'); // Show all logs
Logit.setLogLevel('off'); // Disable all logsLogit.setPrefix(namespace)
Sets a namespace prefix for all subsequent logs.
Parameters:
namespace: string- Prefix text
Example:
Logit.setPrefix('API');
Logit.info('Request started'); // Shows [API] in output
Logit.setPrefix('Database');
Logit.info('Connected'); // Shows [Database] in outputLogit.setRemote(remote)
Configures remote logging handler.
Parameters:
remote: LogitRemoteOptions- Remote logging configuration
Example:
Logit.setRemote({
handler: async (type, ...args) => {
if (type === 'error') {
await fetch('/api/logs', {
method: 'POST',
body: JSON.stringify({ type, args }),
});
}
},
logLevel: 'error', // Only send errors remotely
});Logit.setRemoteLogLevel(level)
Updates the remote logging level without changing the handler.
Parameters:
level: LogitLevel- Minimum level for remote logging
Example:
// Initially send errors only
Logit.setRemote({
handler: sendToServer,
logLevel: 'error',
});
// Later, also send warnings
Logit.setRemoteLogLevel('warn');Logit.setVariant(variant)
Sets the display variant for log messages.
Parameters:
variant: 'text' | 'icon' | 'symbol'- Display style
Options:
'symbol'- Emoji-like symbols (🅳, 🅸, etc.)'icon'- Unicode icons (☕, ℹ, ✔, etc.)'text'- Plain text (DEBUG, INFO, etc.)
Example:
Logit.setVariant('symbol'); // [🅸] Message
Logit.setVariant('icon'); // [ℹ] Message
Logit.setVariant('text'); // [INFO] MessageLogit.showEnvironment(value)
Shows or hides the environment indicator (🅿 for production, 🅳 for development).
Parameters:
value: boolean- Show environment indicator
Example:
Logit.showEnvironment(true); // Show indicator
Logit.showEnvironment(false); // Hide indicatorLogit.showTimestamp(value)
Shows or hides timestamps in log output.
Parameters:
value: boolean- Show timestamps
Example:
Logit.showTimestamp(true); // Show timestamps
Logit.showTimestamp(false); // Hide timestampsGetter Methods
Logit.getLevel()
Returns the current minimum log level.
Returns: LogitLevel
Example:
const currentLevel = Logit.getLevel();
console.log(currentLevel); // 'info'Logit.getPrefix()
Returns the current namespace prefix.
Returns: string
Example:
Logit.setPrefix('MyModule');
const prefix = Logit.getPrefix();
console.log(prefix); // 'MyModule'Logit.getTimestamp()
Returns whether timestamps are currently enabled.
Returns: boolean
Example:
const timestampsEnabled = Logit.getTimestamp();
console.log(timestampsEnabled); // true or falseTypes
LogitOptions
Configuration options for initializing Logit.
interface LogitOptions {
environment?: boolean; // Show environment indicator (default: true)
variant?: 'text' | 'symbol' | 'icon'; // Display variant (default: 'symbol')
logLevel?: LogitLevel; // Minimum log level (default: 'debug')
namespace?: string; // Prefix for logs (default: '')
remote?: LogitRemoteOptions; // Remote logging configuration
timestamp?: boolean; // Show timestamps (default: true)
}LogitRemoteOptions
Remote logging handler configuration.
interface LogitRemoteOptions {
handler?: (type: LogitType, ...args: any[]) => void;
logLevel: LogitLevel;
}Properties:
handler- Function called for remote loggingtype: LogitType- The log level/type...args: any[]- Original log arguments
logLevel- Minimum level to trigger remote logging
LogitLevel
Available log levels (ordered by severity).
type LogitLevel =
| 'debug' // Lowest - most verbose
| 'trace'
| 'time'
| 'table'
| 'info'
| 'success'
| 'warn'
| 'error' // Highest - least verbose
| 'off'; // Disables all loggingLogitType
Available log methods.
type LogitType = 'debug' | 'trace' | 'time' | 'table' | 'info' | 'success' | 'warn' | 'error';LogitColors
Colors used in themes (internal type).
type LogitColors = Exclude<LogitType, 'table'> | 'group' | 'ns';LogitTheme
Theme definition for log styling.
interface LogitTheme {
color: string; // Text color
bg: string; // Background color
border: string; // Border color
icon?: string; // Icon character
symbol?: string; // Symbol character
}LogitInstance
Type representing the Logit object.
type LogitInstance = typeof Logit;Advanced Usage
Custom Remote Handler
Logit.setRemote({
handler: async (type, ...args) => {
// Format the log entry
const entry = {
timestamp: new Date().toISOString(),
level: type,
prefix: Logit.getPrefix(),
messages: args.map((arg) => {
if (arg instanceof Error) {
return {
message: arg.message,
stack: arg.stack,
name: arg.name,
};
}
return arg;
}),
};
// Send to multiple destinations
try {
await Promise.all([
fetch('/api/logs', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(entry),
}),
sendToSentry(entry),
writeToLocalStorage(entry),
]);
} catch (error) {
console.error('Remote logging failed:', error);
}
},
logLevel: 'warn',
});Conditional Configuration
// Development
if (process.env.NODE_ENV === 'development') {
Logit.initialise({
logLevel: 'debug',
variant: 'symbol',
timestamp: true,
environment: true,
});
}
// Production
if (process.env.NODE_ENV === 'production') {
Logit.initialise({
logLevel: 'error',
variant: 'text',
timestamp: true,
environment: false,
remote: {
handler: sendToAnalytics,
logLevel: 'error',
},
});
}
// Testing
if (process.env.NODE_ENV === 'test') {
Logit.setLogLevel('off'); // Silent during tests
}Browser vs Node.js
Logit automatically adapts to the environment:
Browser:
- Uses CSS styling for colored output
- Supports all visual variants (symbol, icon, text)
- Respects log levels for filtering
Node.js:
- Uses plain text output
- Shows variant symbols/icons/text in output
- Respects log levels for filtering
Both environments support all features including remote logging, timing, grouping, and tables.