Package Exports
- @potentii/logger-js
- @potentii/logger-js/logger.mjs
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@potentii/logger-js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
LoggerJS
A logger wrapper around winston.
Usage
Basic usage out of the box:
import Logger from '@potentii/logger-js';
//...
Logger.global.reset();
Logger.global
.withLevel('info') // Possible levels: debug, verbose, info, warn, error
.withService('my-app')
.addTransport(new Logger.WINSTON.transports.Console({ format: Logger.FORMAT_TEMPLATES.human })); // Possible formats: Logger.FORMAT_TEMPLATES.human, Logger.FORMAT_TEMPLATES.json
//...
Logger.info('USER_FETCH_SUCCESS', 'User fetch successfully', { userId: '1234' });
// ...
try{
// ...
} catch (err){
Logger.error('PROCESS_FAILED', 'Processing has failed', err, { userId: '1234', processType: '...' });
}
Using Dynamic Logging within an express
request:
// Define a handler to inject the dynamic logger (before the actual handlers):
app.use((req, res, next) => {
res.locals.logger = Logger.dynamic({ /* opitional context data associated with this request (like correlationIds, or tracking info) */ });
});
// An example of using the dynamic logging to inject more data into the log:
app.get('/users/:userId', (req, res, next) => {
res.locals.logger.set({ userId: req.params.userId }); // Setting the userId on the request context (this will be logged on every log of this request)
res.locals.logger.info('FETCH_USER_STARTED', 'Fetching user...'); // This log will have userId on its payload (you also could pass in more data in the third argument)
// ...
});