@kmhgmbh/logger
v2.0.0
Published
Standardized JSON logger
Readme
@kmhgmbh/logger
This package provides a standardized logger for all projects. It features error serialization, runtime log level evaluation and JSON output.
Installation
npm install @kmhgmbh/loggerLog Levels
Supported log levels are, from highest to lowest priority:
fatal- Fatal errors. The process will exit after logging the message.error- Errors. The process will continue after logging the message.warning- Warnings. The process will continue after logging the message.info- Informational messages. The process will continue after logging the message.debug- Debug messages. The process will continue after logging the message.
The initial log level is set from the environment variable LOG_LEVEL.
If it's not found or cannot be parsed, the default log level setting is debug.
Example Usage
const logger = require('@kmhgmbh/logger').default;
const { LogLevel } = require('@kmhgmbh/logger');
logger.appVersion = '[email protected]';
// restrict log level to all above INFO
logger.logLevel = LogLevel.INFO;
// basic logging
logger.info('Hello World!');
// log a primitive value as context
logger.debug('currentVal', value);
// log content with context information
logger.info('Unauthorized access attempt', {
user: 'john.doe',
ip: '127.0.0.1',
});
// log errors
logger.error('Error while processing request', new Error('Something went wrong'));
// log fatal errors
logger.fatal('Fatal error', new Error('Something went wrong'));
Anonymization
The logger can be configured to anonymize the log output. This is useful for production environments where you don't want to log sensitive data.
Sensitive data will be replaced with the string anonymized.
Nested objects and arrays are supported.
To enable anonymization, set the list of keys to anonymize globally:
logger.anonymize = ['password'];
logger.info('User authentication', {
username,
password,
});The console output will be:
{
"logLevel": "info",
"message": "User authentication",
"appVersion": "[email protected]",
"context": {
"username": "john.doe",
"password": "anonymized"
}
}The list of keys to anonymize is project-specific.
To help you compile a complete list, hand docs/BUILD_anonymize_list.prompt.md to a coding agent (e.g. Claude Code): it researches your codebase and produces the logger.anonymize array for you.
Pretty Print
The logger can be configured to pretty print the log output. This is useful for development environments where you want to read the log output in a structured and colorful way. The Pretty Print option is disabled by default.
To enable pretty print, set the prettyPrint option to true
// set prettyPrint globally
logger.prettyPrint = true;The console output will be:

