A logger for HTTP application servers built with JavaScript (Node.js, Deno, etc).
Created by Kris Walker 2017 - 2025.
- No dependencies: A logger is a low level primitive component which systems depend on and should NOT complicate matters by having dependencies itself.
- Provide rich and indexable information: Logs should be output in structured data formats which can be leveraged by other tools for analysis.
- Multilevel logging (DEBUG, INFO, WARN, ERROR)
- Named child loggers with inheritance
- Dynamic log level and mode configuration
- Multiple output modes (console, stdout, silent)
- Structured logging with support for additional info and error objects
npm install kixx-logger| Env | Version |
|---|---|
| ECMA | >= ES2022 |
| Node.js | >= 16.13.2 |
| Deno | >= 1.0.0 |
This library is designed for use in an ES6 module environment requiring Node.js >= 16.13.2 or Deno >= 1.0.0. You could use it in a browser, but there are no plans to offer CommonJS or AMD modules. It targets at least ES2022 and uses the optional chaining operator ?..
If you're curious: Node.js >= 16.13.2 is required for ES6 module stabilization and ES2022 support.
Note: There is no TypeScript here. It would be waste of time for a library as small as this.
import Logger from 'kixx-logger';
// Create a logger with a name
const logger = new Logger({ name: 'my-app' });
// Log messages at different levels
logger.debug('Debug message');
logger.info('Info message');
logger.warn('Warning message');
logger.error('Error message');You can include additional information and error objects with your log messages:
// Log with additional info
logger.info('User action', { userId: 123, action: 'login' });
// Log with error
try {
// ... some code that might throw
} catch (error) {
logger.error('Operation failed', null, error);
}
// Log with both info and error
logger.error('Failed to process user', { userId: 123 }, error);The logger supports four levels of logging:
| Level Name | Level Integer | Description |
|---|---|---|
| DEBUG | 10 | Detailed debugging information |
| INFO | 20 | General operational information |
| WARN | 30 | Warning messages for potentially harmful situations |
| ERROR | 40 | Error events that might still allow the application to continue running |
When creating a logger, you can specify the following options:
const logger = new Logger({
name: 'my-app', // Required: A string identifier for the logger
level: 'INFO', // Optional: Logging level (default: 'INFO')
mode: 'stdout' // Optional: Output mode (default: 'stdout')
});Create child loggers that inherit settings from their parent:
const parent = new Logger({ name: 'parent' });
const child = parent.createChild('child');
// Child logger inherits parent's level and mode
child.info('Message from child logger'); // Output: "parent:child - Message from child logger"
// Changes to parent logger affect all children
parent.level = 'DEBUG'; // This also changes child's level to DEBUG
parent.mode = 'console'; // This also changes child's mode to consoleThe logger supports three output modes:
-
stdout(default): Structured output suitable for production environments- Format:
datetime - level - pid - name - message - infoString - errorString - ISO timestamp
- Log level (padded to 5 characters)
- Process ID
- Logger name
- Message
- Additional info (JSON string or 'null')
- Error details (JSON string or 'null')
- Format:
-
console: Human-readable output suitable for development- Human-readable timestamp (HH:mm:ss.SSS)
- Log level (padded to 5 characters)
- Logger name
- Message
- Additional info
- Error details
-
silent: Disables all logging output (useful for testing)
When logging error objects, they are automatically formatted as:
{
name: string, // Error name or '[NO_NAME]'
code: string, // Error code or '[NO_CODE]'
message: string, // Error message or '[NO_MESSAGE]'
stack: string // Error stack trace or '[NO_STACK]'
}new Logger(options)name(string, required): Logger identifierlevel(string|number, optional): Logging levelmode(string, optional): Output mode ('stdout', 'console', or 'silent')
namemust be a non-empty string, otherwise throwsError: A logger must be provided with a namelevelmust be a valid level name or integer, otherwise throws an errormodemust be a valid mode name, otherwise throws an error
Object containing all available log levels:
{
DEBUG: 10,
INFO: 20,
WARN: 30,
ERROR: 40
}Object containing all available output modes:
{
CONSOLE: 'console',
STDOUT: 'stdout',
SILENT: 'silent'
}Get or set the logging level:
// Get current level
const currentLevel = logger.level;
// Set level using string
logger.level = 'DEBUG';
// Set level using integer
logger.level = 10; // DEBUG levelNote: Changing the level on a parent logger automatically updates all child loggers.
Get or set the output mode:
// Get current mode
const currentMode = logger.mode;
// Set mode
logger.mode = 'console';Note: Changing the mode on a parent logger automatically updates all child loggers.
Creates a child logger that inherits settings from the parent.
const child = logger.createChild('child-name');Parameters:
name(string, required): Child logger name. Must be a non-empty string.
Returns: A new Logger instance with inherited settings.
Throws: Error if name is not a string.
Log messages at different levels with optional additional information and error objects.
logger.debug('Debug message', { context: 'value' });
logger.info('Info message', { userId: 123 });
logger.warn('Warning message', null, error);
logger.error('Error message', { context: 'value' }, error);Parameters:
message(string, required): The log messageinfo(any, optional): Additional structured information (will be JSON stringified)error(Error, optional): Error object to log (will be formatted and JSON stringified)
import Logger from 'kixx-logger';
const logger = new Logger({ name: 'my-app' });
logger.info('Application started');const appLogger = new Logger({ name: 'app', level: 'INFO' });
const dbLogger = appLogger.createChild('database');
const apiLogger = appLogger.createChild('api');
dbLogger.info('Database connected');
apiLogger.warn('Rate limit approaching');try {
// Some operation that might fail
throw new Error('Database connection failed');
} catch (error) {
logger.error('Failed to connect to database', {
host: 'localhost',
port: 5432
}, error);
}const testLogger = new Logger({
name: 'test',
mode: 'silent'
});
// No output will be produced
testLogger.info('This will not be logged');Copyright: (c) 2017 - 2025 by Kris Walker (www.kriswalker.me)
Unless otherwise indicated, all source code is licensed under the MIT license. See LICENSE for details.