Comments (5)
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.
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.
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.
@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.
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)
- Error: EBADF: bad file descriptor, write HOT 7
- Is there a way to get the start time instead of the finish time?
- Custom tokens returning function string HOT 1
- Is this compatible with angular 11? HOT 1
- Usage in ES6 promise based import does not work HOT 1
- skip function not working with 'immediate:true' configuration HOT 3
- Colored status token HOT 1
- Dexter Morgan HOT 2
- Use morgan as custom console logger HOT 3
- morgan block my auth login redirection when I use createWriteStream for stream option HOT 9
- Adding a token named token causes all subsequent tokens to break. HOT 3
- req.route.path value is undefined HOT 3
- :res[content-length] only shows size less than 1 KB HOT 2
- Support for promise HOT 2
- Getting vulnerable issue [email protected] HOT 1
- Expose `stream.write` as option HOT 2
- webscoket request should be logged HOT 2
- SPDY > Express.JS > Next.JS don't show path details.
- `User: ':remote-user'` does not show logged-in user but `-`
- Issue HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from morgan.