@kmhgmbh/logger
v1.1.6
Published
Standardized JSON logger
Downloads
825
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/logger
Log 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.
Log Purposes
Supported log purposes are:
general
- normal log without any special purposeperformance
- for performance measurementsnetwork
- for network requestsprocess
- for process managementaudit
- for audit logssecurity
- for security logsunknown
- for unknown purposes or not yet implemented purposes
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, LogPurpose } = require('@kmhgmbh/logger');
logger.appVersion = '[email protected]';
// set the log level default to 'info'
logger.defaultLogLevel = LogLevel.INFO;
// restrict log level to all above INFO
logger.currentLogLevel = LogLevel.INFO;
// set the log purpose default to 'general'
logger.defaultLogPurpose = LogPurpose.GENERAL;
// basic logging
logger.log({ message: 'Hello World!' });
// with purpose
logger.log({ message: 'Hello World!', purpose: LogPurpose.GENERAL });
// example for performance logging
logger.warning({ message: 'Average API request time has increased' , purpose: LogPurpose.PERFORMANCE });
// log content with context information
logger.info({
message: 'Unauthorized access attempt',
purpose: LogPurpose.AUDIT,
context: {
user: 'john.doe',
ip: '127.0.0.1',
},
});
// log errors
logger.error({
message: 'Error while processing request',
purpose: LogPurpose.NETWORK,
context: new Error('Something went wrong'),
});
// log fatal errors
logger.fatal({
message: 'Fatal error',
purpose: LogPurpose.PROCESS,
context: 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, pass the anonymize option to the log function:
// set anonymousValue globally
logger.anonymousValue = 'anonymized'; // default
// set it globally
logger.anonymize = ['password'];
// or in the log function
logger.info({
message: 'User authentication',
purpose: LogPurpose.SECURITY,
anonymize: ['password'],
context: {
username
password,
},
});
The console output will be:
{
"logLevel": "info",
"purpose": "security",
"message": "User authentication",
"appVersion": "unknown",
"context": {
"username": "john.doe",
"password": "anonymized"
}
}
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: