diff --git a/packages/logger/src/console.ts b/packages/logger/src/console.ts index afa35860..90ac00ea 100644 --- a/packages/logger/src/console.ts +++ b/packages/logger/src/console.ts @@ -7,10 +7,11 @@ export interface RestoreLoggerConsoleTransportOptions extends transports.Console prettyPrint?: boolean | any; colorize?: boolean | any; sourcePointer?: boolean; + fieldOptions?: RestoreFieldsOptions; } // a custom format that outputs request id -function createTracerFormat(opts: RestoreLoggerConsoleTransportOptions, fieldOpts?: RestoreFieldsOptions) { +function createTracerFormat(opts: RestoreLoggerConsoleTransportOptions) { return format.printf((info) => { const rid = rTracer.id(); const time = info.timestamp; @@ -24,11 +25,11 @@ function createTracerFormat(opts: RestoreLoggerConsoleTransportOptions, fieldOpt delete info.timestamp; let object = {}; if (splat) { - logFieldsHandler(splat, fieldOpts); + logFieldsHandler(splat, opts.fieldOptions); object = JSON.stringify(splat, getCircularReplacer()); } if (message && Object.entries(message).length !== 0 && message.constructor === Object) { - logFieldsHandler(message, fieldOpts); + logFieldsHandler(message, opts.fieldOptions); message = JSON.stringify(message, getCircularReplacer()); } let ret: string[] = []; @@ -72,11 +73,11 @@ function createTracerFormat(opts: RestoreLoggerConsoleTransportOptions, fieldOpt }); } -export function createConsoleTransport(opts: RestoreLoggerConsoleTransportOptions = {}, fieldOpts: RestoreFieldsOptions = {}) { +export function createConsoleTransport(opts: RestoreLoggerConsoleTransportOptions = {}) { let formats: any[] = [ format.simple(), format.timestamp(), - createTracerFormat(opts, fieldOpts), + createTracerFormat(opts), ] if (opts.prettyPrint !== false) { diff --git a/packages/logger/src/elasticsearch.ts b/packages/logger/src/elasticsearch.ts index fd7cf319..fccc4c2a 100644 --- a/packages/logger/src/elasticsearch.ts +++ b/packages/logger/src/elasticsearch.ts @@ -6,7 +6,7 @@ import { RestoreFieldsOptions } from './index'; export const indexTemplate = require('../elasticsearch-index-template.json'); -function createTransformer(opts: RestoreLoggerElasticsearchTransportOptions, fieldOpts?: RestoreFieldsOptions) { +function createTransformer(opts: RestoreLoggerElasticsearchTransportOptions) { /** Transformer function to transform logged data into a the message structure used in restore for storage in ES. @@ -43,13 +43,13 @@ function createTransformer(opts: RestoreLoggerElasticsearchTransportOptions, fie transformed.source_host = os.hostname(); transformed.message = logData.message; if (typeof transformed.message === 'object') { - logFieldsHandler(transformed.message, fieldOpts); + logFieldsHandler(transformed.message, opts.fieldOptions); transformed.message = JSON.stringify(transformed.message, getCircularReplacer()); } transformed.severity = logData.level; transformed.fields = logData.meta; if (typeof transformed.fields !== 'object') { - logFieldsHandler(JSON.parse(transformed.fields), fieldOpts); + logFieldsHandler(JSON.parse(transformed.fields), opts.fieldOptions); transformed.fields ={ message: transformed.fields }; } @@ -75,10 +75,11 @@ function createTransformer(opts: RestoreLoggerElasticsearchTransportOptions, fie export interface RestoreLoggerElasticsearchTransportOptions extends ElasticsearchTransportOptions { sourcePointer?: any; esTransformer?: Function; + fieldOptions?: RestoreFieldsOptions; } -export function createElasticSearchTransport(opts: RestoreLoggerElasticsearchTransportOptions, fieldOpts: RestoreFieldsOptions = {}) { - const transformer = createTransformer(opts, fieldOpts); +export function createElasticSearchTransport(opts: RestoreLoggerElasticsearchTransportOptions) { + const transformer = createTransformer(opts); return new ElasticsearchTransport({ indexTemplate, transformer, diff --git a/packages/logger/src/index.ts b/packages/logger/src/index.ts index 17bd65f6..7620b4a3 100644 --- a/packages/logger/src/index.ts +++ b/packages/logger/src/index.ts @@ -12,6 +12,7 @@ export interface RestoreLoggerOptions extends WinstonLoggerOptions { esTransformer?: Function, loggerName?: string; sourcePointer?: boolean; + fieldOptions?: RestoreFieldsOptions; } export interface BufferField { @@ -19,19 +20,15 @@ export interface BufferField { enableLogging?: boolean; // default value is false } -export interface RestoreBufferFieldMaskOptions { +export interface RestoreFieldsOptions { bufferFields?: BufferField[]; maskFields?: string[]; omitFields?: string[]; } -export interface RestoreFieldsOptions { - fieldOptions?: RestoreBufferFieldMaskOptions; -} - export type TransportStreamArray = Logger['transports']; -export function createLogger(opts: RestoreLoggerOptions = {}, fieldOpts: RestoreFieldsOptions = {}): Logger { +export function createLogger(opts: RestoreLoggerOptions = {}): Logger { // TODO reason for setting namespaces flag? (log as any).namespaces = true; @@ -44,14 +41,14 @@ export function createLogger(opts: RestoreLoggerOptions = {}, fieldOpts: Restore } if (opts.console) { - transports.push(createConsoleTransport({ ...opts.console, sourcePointer: opts.sourcePointer }, fieldOpts)); + transports.push(createConsoleTransport({ ...opts.console, sourcePointer: opts.sourcePointer, fieldOptions: opts.fieldOptions })); } if (opts.file) { transports.push(createFileTransport({ ...opts.file, sourcePointer: opts.sourcePointer })); } if (opts.elasticsearch) { opts.elasticsearch.dataStream = true; - const esTransport = createElasticSearchTransport({ ...opts.elasticsearch, sourcePointer: opts.sourcePointer, esTransformer: opts.esTransformer }, fieldOpts); + const esTransport = createElasticSearchTransport({ ...opts.elasticsearch, sourcePointer: opts.sourcePointer, esTransformer: opts.esTransformer, fieldOptions: opts.fieldOptions }); esTransport.on('error', (error) => { console.error('Elasticsearch indexing error', error); }); diff --git a/packages/logger/src/utils.ts b/packages/logger/src/utils.ts index 49a36860..e52f4d03 100644 --- a/packages/logger/src/utils.ts +++ b/packages/logger/src/utils.ts @@ -186,20 +186,20 @@ export const setNestedPath = (object: any, fieldPath: string, operation: string, } }; -export const logFieldsHandler = (object: any, fieldOpts?: RestoreFieldsOptions) => { - if (!fieldOpts) { +export const logFieldsHandler = (object: any, fieldOptions?: RestoreFieldsOptions) => { + if (!fieldOptions) { return; } // if nonoe of bufferFields or maskFields or omitFields are set then do not proceed further - if (_.isEmpty(fieldOpts?.fieldOptions?.maskFields) && _.isEmpty(fieldOpts?.fieldOptions?.omitFields) && _.isEmpty(fieldOpts?.fieldOptions?.bufferFields)) { + if (_.isEmpty(fieldOptions?.maskFields) && _.isEmpty(fieldOptions?.omitFields) && _.isEmpty(fieldOptions?.bufferFields)) { return; } // since multiple comma separated objects can be passed as fields for logging for (let obj of object) { // iterate to check each mask field - if (!_.isEmpty(fieldOpts?.fieldOptions?.maskFields)) { - fieldOpts.fieldOptions?.maskFields?.forEach((fieldPath) => { + if (!_.isEmpty(fieldOptions?.maskFields)) { + fieldOptions?.maskFields?.forEach((fieldPath) => { let fieldExists = _.get(obj, fieldPath); if (fieldExists) { const maskLength = fieldExists.length; @@ -214,8 +214,8 @@ export const logFieldsHandler = (object: any, fieldOpts?: RestoreFieldsOptions) } // iterate to check each omit field - if (!_.isEmpty(fieldOpts?.fieldOptions?.omitFields)) { - fieldOpts.fieldOptions?.omitFields?.forEach((fieldPath) => { + if (!_.isEmpty(fieldOptions?.omitFields)) { + fieldOptions?.omitFields?.forEach((fieldPath) => { let fieldExists = _.get(obj, fieldPath); if (fieldExists) { _.unset(obj, fieldPath); @@ -228,8 +228,8 @@ export const logFieldsHandler = (object: any, fieldOpts?: RestoreFieldsOptions) } // iterate to check each buffer field - if (!_.isEmpty(fieldOpts?.fieldOptions?.bufferFields)) { - fieldOpts.fieldOptions?.bufferFields?.forEach((bufferFieldObj) => { + if (!_.isEmpty(fieldOptions?.bufferFields)) { + fieldOptions?.bufferFields?.forEach((bufferFieldObj) => { const fieldPath = bufferFieldObj.fieldPath; const enableLogging = bufferFieldObj?.enableLogging ? bufferFieldObj.enableLogging : false; let fieldExists = _.get(obj, fieldPath);