Code Monkey home page Code Monkey logo

Comments (5)

dougwilson avatar dougwilson commented on May 17, 2024 7

How about just doing the following?

var express = require('express')
var fs = require('fs')
var morgan = require('morgan')
var uuid = require('node-uuid')

var app = express()
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})

app.use(function requestId(req, res, next) {
  req.id = uuid.v4()
  next()
})

morgan.token('id', function (req) {
  return req.id
})

app.use(morgan('--> :id [:date] :remote-addr :remote-user ":method :url HTTP/:http-version"', {
  immediate: true,
  stream: accessLogStream
}))
app.use(morgan('<-- :id [:date] :status :res[content-length]', {
  immediate: false,
  stream: accessLogStream
}))

app.get('/', function (req, res) {
  res.send('hello, world!')
})

app.listen(3000)

from morgan.

dougwilson avatar dougwilson commented on May 17, 2024

I'll have to think of a good way to do this. So the use-case for immediate is to log up front so you'll get the access log before your server crashes, if it does, and then you'll better know what requests could have crashed the server if necessary.

As for the request, I believe you can do everything you want with the library as-is. I'm going to look into it and see and also see if anything needs to be changed to accommodate your use-case.

from morgan.

thomas-riccardi avatar thomas-riccardi commented on May 17, 2024

I was just testing something like that, but wanted to avoid a full uuid: I just used a counter:

var counter = 0;
app.use(function requestId(req, res, next) {
  req.id = counter;
  counter += 1;
  next();
})

But I ran into issue #41.

Anyway, the only drawback with your solution is that I have to duplicate the format from the predefined ones (I usually use dev in this case). But since I can't find an easy solution to that, then I'll gladly accept your solution, thanks!

from morgan.

johncmunson avatar johncmunson commented on May 17, 2024

@dougwilson in reference to your proposed solution for a correlation id above... what about when you are logging from layers that are separate from express routing, such as service or repository layers? Will the req object need to be explicitly passed around from layer to layer? In addition... would a separate logging solution be needed in these places, since morgan seems to only function as middleware?

from morgan.

dougwilson avatar dougwilson commented on May 17, 2024

Hi @johncmunson my response was to answer the OP's specific use-case and question, and OP closed the issue as it works for them. If you have a different/additional questions, please open a new issue so everyone in this repo will see it and can jump in to help, as well as not drag back in the original reporters from years ago (they are getting emails for all our responses here). You can always link back to an issue (like this one) if it's content is relevant to your question.

from morgan.

Related Issues (20)

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.