daily pastebin goal
7%
SHARE
TWEET

Untitled

a guest Dec 6th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. const rTracer = require('cls-rtracer')
  4.  
  5. // that how you can configure winston logger
  6.  
  7. const { createLogger, format, transports } = require('winston')
  8. const { combine, timestamp, printf } = format
  9.  
  10. // a custom format that outputs request id
  11. const rTracerFormat = printf((info) => {
  12.   const rid = rTracer.id()
  13.   return rid
  14.     ? `${info.timestamp} [request-id:${rid}]: ${info.message}`
  15.     : `${info.timestamp}: ${info.message}`
  16. })
  17.  
  18. const logger = createLogger({
  19.   format: combine(
  20.     timestamp(),
  21.     rTracerFormat
  22.   ),
  23.   transports: [new transports.Console()]
  24. })
  25.  
  26. // now let's configure and start the Express app
  27.  
  28. const express = require('express')
  29.  
  30. const app = express()
  31. app.use(rTracer.expressMiddleware())
  32.  
  33. app.get('/', function (req, res) {
  34.   logger.info('Starting request handling')
  35.   fakeDbAccess()
  36.     .then((result) => res.json(result))
  37. })
  38.  
  39. async function fakeDbAccess () {
  40.   return new Promise((resolve) => {
  41.     setTimeout(() => {
  42.       logger.info('Logs from fakeDbAccess')
  43.       resolve({ message: 'Hello from cls-rtracer Express example' })
  44.     }, 0)
  45.   })
  46. }
  47.  
  48. app.listen(3000, (err) => {
  49.   if (err) {
  50.     logger.err('The app could not start')
  51.   }
  52.   logger.info('The app is listening on 3000')
  53. })
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top