"express custom logger" Code Answer's

You're definitely familiar with the best coding language TypeScript that developers use to develop their projects and they get all their queries like "express custom logger" answered properly. Developers are finding an appropriate answer about express custom logger related to the TypeScript coding language. By visiting this online portal developers get answers concerning TypeScript codes question like express custom logger. Enter your desired code related query in the search bar and get every piece of information about TypeScript code related question on express custom logger. 

express custom logger

By Restu Wahyu SaputraRestu Wahyu Saputra on Feb 21, 2021
import { Request, Response, NextFunction } from 'express'
import consola from 'consola'
import chalk from 'chalk'
import { dateFormat } from '../utils/util.date'

export const serviceLogger = (serviceName: string) => (req: Request, res: Response, next: NextFunction) => {
	if (process.env.NODE_ENV !== 'production') {
		customLogger(req, res, serviceName)
		next()
	}
}

const customLogger = (req: Request, res: Response, serviceName: string) => {
	res.on('finish', () => {
		if (res.statusCode >= 400) {
			consola.info(
				chalk.red(
					'[Logger]:',
					JSON.stringify({
						serviceName: serviceName,
						ipAddress: req.ip || req.headers['x-forwarded-host'] || req.connection.remoteAddress,
						path: req.url,
						method: req.method,
						status: res.statusCode,
						timestamp: dateFormat(new Date()).format('LLLL')
					})
				)
			)
		} else {
			consola.info(
				chalk.green(
					'[Logger]:',
					JSON.stringify({
						serviceName: serviceName,
						ipAddress: req.ip || req.headers['x-forwarded-host'] || req.connection.remoteAddress,
						path: req.url,
						method: req.method,
						status: res.statusCode,
						timestamp: dateFormat(new Date()).format('LLLL')
					})
				)
			)
		}
	})
}

Add Comment

0

All those coders who are working on the TypeScript based application and are stuck on express custom logger can get a collection of related answers to their query. Programmers need to enter their query on express custom logger related to TypeScript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about express custom logger for the programmers working on TypeScript code while coding their module. Coders are also allowed to rectify already present answers of express custom logger while working on the TypeScript language code. Developers can add up suggestions if they deem fit any other answer relating to "express custom logger". Visit this developer's friendly online web community, CodeProZone, and get your queries like express custom logger resolved professionally and stay updated to the latest TypeScript updates. 

TypeScript answers related to "express custom logger"

View All TypeScript queries

TypeScript queries related to "express custom logger"

Browse Other Code Languages

CodeProZone