Giter VIP home page Giter VIP logo

resting-squirrel-es-logger's Introduction

resting-squirrel-es-logger

ElasticSearch logger for Resting Squirrel apps.

Requirements

  • Node.js >= 8
  • ElasticSearch 7

Installation

npm install resting-squirrel-es-logger --save

Usage

import rs from 'resting-squirrel'; // peer dependency
import logger from 'resting-squirrel-es-logger';

const app = rs({
	logger: logger(/* options */),
});

app.start();

Options

appName: string - Name of the application. Default: RS App
clearTemplate: boolean - Indicates if the ES template is deleted before the logger init. Default: false
getCustomData: (property: string, loggerData: ILoggerData) => any - Function to get the value of custom property. Default: null
indexTimeFormat: string - Time format from moment for the index suffix. Default: YYYY-MM-DD
node: string - ES node. Default: http://localhost:9200
onError: (error: any) => void - Function called if some error occurs. Default: null
onReady: () => void - Function called if the logger is ready. Default: null
template: Template - Template data. Default: { name: 'rs-es-logger', numberOfReplicas: 0, numberOfShards: 1, properties: {} }
transformBody: (property: string, value: any) => any - Function to transform property value before the save in the request body. Default: null
transformQuery: (property: string, value: any) => any - Function to transform property value before the save in the request query. Default: null

Template

name: string - Name of the template. Default: rs-es-logger
numberOfReplicas: number - Number of replicas. Default: 0
numberOfShards: number - Number of shards. Default: 1
properties: { [key: string]: any } - Custom template properties for custom data. Default: {}

Transforms

Data in body and query can be transformed.

import logger from 'resting-squirrel-es-logger';

logger({
	transformBody(property, value) {
		switch (property) {
			case 'password':
				return '********'; // We really don't want to have passwords in database
			case 'profile_picture': // The picture is in base64
				return `[file(${value?.length})]`; // We don't want to save large data to database
			default: 
				return value;
		}
	},
});

Custom data

Custom data can be indexed as well. It should be the data for further searches.

import logger from 'resting-squirrel-es-logger';

logger({
	template: {
		properties: {
			userId: {
				type: 'integer',
			},
		},
	},
	getCustomData(property, { headers }) {
		// Logger doesn't have access to the incoming request so we have to store the userId to the headers in auth middleware
		switch (property) {
			case 'userId':
				// After that we can access userId from headers and save it
				return headers?.userId || null;
			default: 
				return null;
		}
	},
});

resting-squirrel-es-logger's People

Contributors

zabkwak avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.