Skip to content

Instantly share code, notes, and snippets.

@rohit267
Created January 31, 2022 07:24
Show Gist options
  • Save rohit267/8e180b9b4b605a765f5755c06747317c to your computer and use it in GitHub Desktop.
Save rohit267/8e180b9b4b605a765f5755c06747317c to your computer and use it in GitHub Desktop.
Winston logger with custom Azure File/Blob Storage
import winston from 'winston';
import { AzureBlobTransport } from 'winston-azure-transport';
import moment from 'moment-timezone';
const { combine, printf, colorize } = winston.format;
/**
*
* RSGI-LOGGER WITH AZURE FILE STORAGE TRANSPORT
* Will store logs in journey wise and date wise.
*
* Installation:
* npm i winston winston-azure-transport chunk moment-timezone
*
* Usage:
* import Logger, { journey } from 'RSGI-LOGGER';
*
* const logger = new Logger(true, azureContainerUrl, journey.AROGYA_SANJEEVANI);
*
* logger.info('SOME INFO', { data: 'SOME DATA' });
* logger.error('SOME ERROR', { stack: 'SOME STACK' });
* logger.success('PAYMENT SUCCESS', { data: 'ADA' });
*/
const myFormat = winston.format.combine(
winston.format.align(),
printf((info) => {
const { level, message, ...args } = info;
const ts = moment().tz('Asia/Kolkata').format('DD-MM-YYYYThh:mm:ss:SSSA');
const mMeta = args.meta;
return `${ts} [${level}]: ${message} ${mMeta ? ': ' + (typeof mMeta === 'object' ? JSON.stringify(mMeta, null, 2) : mMeta) : ''}`;
})
);
class Logger {
transports = [
new winston.transports.Console({
format: combine(
colorize({
colors: {
error: 'red',
info: 'blue',
success: 'green',
},
}),
myFormat
),
}),
];
/**
*
* @param {boolean} useAzureBlogTransport (false for only console output)
* @param {Azure container URL: String} azureContainerUrl (if useAzureBlogTransport is true than mandatory)
* @param {Constant from this module only: string} journey (if useAzureBlogTransport is true than mandatory)
*/
constructor(useAzureBlogTransport, azureContainerUrl, journey) {
this.useAzureBlogTransport = useAzureBlogTransport;
if (useAzureBlogTransport) {
if (!azureContainerUrl || !journey) throw new Error('Cannot initialize logger without azure container url and journey');
this.azureContainerUrl = azureContainerUrl;
this.transports.push(
new AzureBlobTransport({
containerUrl: this.azureContainerUrl,
nameFormat: journey + '/' + moment().tz('Asia/Kolkata').format('DD-MM-YYYY') + '.log',
})
);
}
this.logger = winston.createLogger({
levels: {
error: 0,
info: 2,
success: 1,
},
transports: this.transports,
format: myFormat,
});
this.info = (message, meta) => {
this.logger.info({ message, meta });
};
this.error = (message, meta) => {
this.logger.error({ message, meta });
};
this.success = (message, meta) => {
this.logger.log({ level: 'success', message, meta });
};
}
}
export default Logger;
export const journey = {
CAR_INSURANCE: 'CAR_INSURANCE',
TWO_WHEELER: 'TWO_WHEELER',
HOME_INSURANCE: 'HOME_INSURANCE',
PERSONAL_ACCIDENT: 'PERSONAL_ACCIDENT',
LIFELINE_INSURANCE: 'LIFELINE_INSURANCE',
AROGYA_SANJEEVANI: 'AROGYA_SANJEEVANI',
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment