Skip to content

Commit

Permalink
fix: integrate fieldOptions into existing logger config
Browse files Browse the repository at this point in the history
  • Loading branch information
Arun-KumarH committed Oct 11, 2022
1 parent da052aa commit 93f74c9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 27 deletions.
11 changes: 6 additions & 5 deletions packages/logger/src/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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[] = [];
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions packages/logger/src/elasticsearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 };
}

Expand All @@ -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,
Expand Down
13 changes: 5 additions & 8 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@ export interface RestoreLoggerOptions extends WinstonLoggerOptions {
esTransformer?: Function,
loggerName?: string;
sourcePointer?: boolean;
fieldOptions?: RestoreFieldsOptions;
}

export interface BufferField {
fieldPath: string;
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;

Expand All @@ -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);
});
Expand Down
18 changes: 9 additions & 9 deletions packages/logger/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 93f74c9

Please sign in to comment.