@okrahq/logger
v1.1.1
Published
Custom logger based on winston logging library. This library empowers projects to define their own format for printing the logs.
Downloads
4
Readme
logger
Logger is Okra's custom logger built on top of Winston logger. Logger is written in typescript and is available for the typescript and javascript repositories. It allows developers to define a log transformation function to transform the logs in a particular format. This format can include metadata like requestId, customerId, partnerId, and many more.
Usage
Without log transform function:
// import logger
const Logger = require('@okra/logger').default;
const logger = new Logger(logTransform, metadata);
logger.info('We welcome you to Okra!'); // logs We welcome you to Okra! to terminal in green color.
logger.debug('We welcome you to Okra!'); // logs We welcome you to Okra! to terminal in blue color.
logger.warn('We welcome you to Okra!'); // logs We welcome you to Okra! to terminal in yellow color.
logger.error('We welcome you to Okra!'); // logs We welcome you to Okra! to terminal in red color.
With log transform function:
// import logger
const Logger = require('@okra/logger').default;
// declare metadata. Metadata can be any JSON object that holds the necessary details required in log format.
const metadata = {
customerId: 121321324,
requestId: OKR-JN2323424,
};
// define logTransform() to return the desired format for logs.
const logTransform = (message: string, metadata: Metadata): string =>
`customerId: ${metadata.customerId} | requestId: ${metadata.requestId} | message: ${message}`;
const logger = new Logger(logTransform, metadata);
logger.info('We welcome you to Okra!'); // 2022-02-02T14:19:08+05:30 - INFO: timestamp: 2022-02-02T14:19:08+05:30 | customerId: 121321324 | requestId: OKR-JN2323424 | message: We welcome you to Okra! (in green color)
logger.debug('We welcome you to Okra!'); // 2022-02-02T14:19:08+05:30 - INFO: timestamp: 2022-02-02T14:19:08+05:30 | customerId: 121321324 | requestId: OKR-JN2323424 | message: We welcome you to Okra! (in blue color)
logger.warn('We welcome you to Okra!'); // 2022-02-02T14:19:08+05:30 - INFO: timestamp: 2022-02-02T14:19:08+05:30 | customerId: 121321324 | requestId: OKR-JN2323424 | message: We welcome you to Okra! (in yellow color)
logger.error('We welcome you to Okra!'); // 2022-02-02T14:19:08+05:30 - INFO: timestamp: 2022-02-02T14:19:08+05:30 | customerId: 121321324 | requestId: OKR-JN2323424 | message: We welcome you to Okra! (in red color)