genie-logger
v0.1.0
Published
Reusable Node.js logger for platform Lambda services.
Downloads
6
Readme
Genie logger Library for Node.js services
Reusable Node.js logger for platform Lambda services.
Installing
To install this library into your project
$ npm i @genie-solutions/genie-logger
Dependencies
This library is built using uuid, winston and winston-transport
Basic Examples
import { createLoggerFromContext } from "./services/logging";
const createLogger = createLoggerFromContext(APP_NAME, STAGE, LOG_LEVEL);
const logger = createLogger(context.awsRequestId);
logger.info({
type: EventType.CreditAssessed,
payload: {
threadId,
tenantId,
creditBalance,
calculatedCost: cost
}
});
logger.error({
type: EventType.CreditServiceError,
error: { message: error.message }
});
logger.warn({
type: EventType.CreditServiceWarning,
error: { message: error.message }
});
logger.debug({
type: EventType.CreditServiceDebug,
error: { message: error.message }
});
Profiling Examples
//
// Start profile of 'test'
//
logger.profile('test');
setTimeout(function () {
//
// Stop profile of 'test'. Logging will now take place:
// '17 Jan 21:00:00 - info: test duration=1000ms'
//
logger.profile('test');
}, 1000);
This will output {"level":"info","durationMs":2311,"message":"test"}
in the log
Profiling Timer example
// Returns an object corresponding to a specific timing. When done
// is called the timer will finish and log the duration. e.g.:
//
const profiler = logger.startTimer();
setTimeout(function () {
profiler.done({ message: 'Logging message' });
},
This will output {"message":"Logging message","level":"info","durationMs":14990}
in the log
Extra metadata inthe log
The default logger will include the three parameter metadata(APP_NAME, STAGE, LOG_LEVEL) in the log, to extend this with extra metadata, use the extend function
const logger = createLogger(context.awsRequestId).extend(
pick({ requestId: "451" })
);
logger.info("Child logging");
This will output following log:
{"requestId":"451","message":"Child logging","level":"info"}