1

I'm handling a new project with node.js backend and my morgan logger works quite wired. At first I was unable to get any output from it using any of predefined morgan templates (tiny, short, e.c.t) nothing was propagated to output. I provided custom logging function to verify does the morgan is called at all on my stack:

app.use(morgan((tokens, req, res) => {
console.log('AAAAAAAAAAA');
return 'ABABABABABAA';
}, { stream: process.stdout }));

Sequence of AAAAAAAAAAA is indeed propagated to console output via console.log inside logging function (proof that morga in actually called on each request), but logging function does not provide it's ABABABABABA sequence at all.

As long as I'm able to imagine that predefined format may fail due to non-conventional names of req/res objects (which is by the way not a case), then returning plain text from logger function shall be obviously propagated to output straight through.

Edit

Setting morgan option to {immediate: true} also does not resolve issue.

Nick Manning
  • 2,828
  • 1
  • 29
  • 50
Tomas
  • 3,269
  • 3
  • 29
  • 48
  • 1
    I can't reproduce this. – robertklep Sep 06 '18 at 12:25
  • For me, on clean project setup it's working as well, so the question is rather is there any node/express gotchas thah may prevent it from working – Tomas Sep 07 '18 at 08:47
  • It's strange that a `console.log` would work but returning the string (which would be written to `process.stdout` at some point) wouldn't. So I have no obvious ideas on what could be causing this. – robertklep Sep 07 '18 at 08:49
  • I was thinking about some custom error handling that will stop response form propagation, but then `{immidiate: true}` would work correctly - its's not – Tomas Sep 07 '18 at 09:11
  • It's also strange that the predefined templates don't work. Perhaps you're using more than one instance of `morgan` in your app? Not sure if that would break things, but who knows. – robertklep Sep 07 '18 at 10:19
  • nope, `morgan` was not used in app (only manual logging). That's a reason why I wanted to plug `morgan` :) – Tomas Sep 07 '18 at 10:22

1 Answers1

13

In my case, the problem was that I didn't call 'morgan' right after creating the app. When I changed its position, it solved the issue. I couldn't find any reference in 'morgan' page about this, but I found it in another answer here: Express Morgan not writing logs to file or STDOUT

Working solution:

let app = express()
app.use(morgan('combined'))     <--- morgan right after app
app.set('views', './views')
app.set('view engine', 'ejs')
app.use('/', routes)

Not working code (previously):

let app = express()
app.set('views', './views')
app.set('view engine', 'ejs')
app.use('/', routes)
app.use(morgan('combined'))     <--- morgan
ofri cofri
  • 886
  • 10
  • 13
  • 2
    Unfortunately, In my case `morgan` is the first middlewere registered in app (just like in your "working" example). Anyway thank you for contributing, maybe this will help someone other – Tomas Mar 18 '19 at 07:51
  • Ok. I tried your exact code in a small app, and it's printing both 'AAAAAAAAAAA' and 'ABABABABABAA'. Maybe it's something else, maybe you could show more details of your app code. – ofri cofri Mar 18 '19 at 19:48
  • 1
    This solved my issue. Why does the order matter and is that an issue on express' side? – Theo Mar 10 '20 at 04:26
  • The solution worked for me, Thanks!! but didn't understand the reason behind the specific ordering – Nikhil Jadhav Jul 09 '22 at 20:52
  • I passed in 'tiny' instead of combined as I didn't need all of the extra stuff, just wanted the response code – Tyler Jul 31 '23 at 15:26