expressjs / morgan

HTTP request logger middleware for node.js
MIT License
7.95k stars 536 forks source link

morgan block my auth login redirection when I use createWriteStream for stream option #264

Closed tkxkd0159 closed 2 years ago

tkxkd0159 commented 2 years ago

I encountered a problem when logging to a file instead of stdout in Morgan. In this setting, my google login refreshes the current page instead of going to the profile selection page.
I wrote the code as below.

const accessLogStream: WriteStream =
    process.env.NODE_ENV == "production"
        ? createWriteStream(
              path.join(process.cwd(), "logs/prod", "api-access.log"),
              { flags: "a" }
          )
        : createWriteStream(
              path.join(process.cwd(), "logs/dev", "api-access.log"),
              { flags: "a" }
          );

app.use(morgan("common", { stream : process.stdout })); // When I set like this, my auth login work without problem
app.use(morgan("common", { stream: accessLogStream })); // When I set like this, my front page can't redirect to google login

Thank you.

dougwilson commented 2 years ago

Hi @tkxkd0159 that is very strange. When morgan tries to write to the stream, it does not wait for the write to complete before continuing, and even then morgan does not write until after the response is completed. I tried with a basic app without issue.

Can you please provide an app that demonstrates the issue and include instructions for how to trigger the condition with the given app? We would be happy to debug the issue. Alternatively you're welcome to make a pull request that fixes the issue.

tkxkd0159 commented 2 years ago

Thanks for your quick feedback @dougwilson ! My original repository where the problem occurred required several complicated setup processes, so I made a dummy repository that had similar problems (refresh issue). I made a simple example app that separates the backend and the front, and if you press the button called check morgan bug, the bug is reproduced (Page is refreshed again for current location after receiving a response with fetch). You can also check this with chrome network tab

The address of the source is here. => https://github.com/tkxkd0159/debug-morgan

dougwilson commented 2 years ago

Hi @tkxkd0159 thanks for that! Are you sure the bug is due to morgan? I ask because in the reproduction code you provided, you have morgan commented out. If you are able to reproduce with that given app, it would seem the issue is unrelated to morgan.

https://github.com/tkxkd0159/debug-morgan/blob/759ab6c3b4b0b35a19568c63f71be05b0b85a653/src/app.ts#L51

tkxkd0159 commented 2 years ago

Hi @dougwilson, sorry I forget to uncomment app.use(morgan("common", { stream: accessLogStream })); so this code works fine (no bug). but when you uncomment morgan part, bus is reproduced.

dougwilson commented 2 years ago

Thanks. I guess I should clarify that I tried that, and your app acted the same if I had it commented out or not, so I wasn't sure if you were able to reproduce with that app. If that is the case, it seems that for some reason I am unable to reproduce on my system and unfortunately won't be able to help produce a fix without reproducing. You'll need to debug what the cause is and make a pull request with a fix 👍 , as it may be dependent on perhaps something on the system itself.

tkxkd0159 commented 2 years ago

Thanks @dougwilson. For reference, my environment was Ubuntu 20.04.4 LTS. I'll try to debug this problem.

tkxkd0159 commented 2 years ago

I tried to check issue this my mac (Monterey 12.4) and WIndows 10 machine. And there was no bug with morgan. Maybe there is something wrong with my linux machine or just compatible issue with Ubuntu focal. I will try to check this. Thanks

dougwilson commented 2 years ago

Ah, very interesting. And sorry for not including it, I was using Windows 11.

tkxkd0159 commented 2 years ago

I tried to test with linux docker container and it works fine. Maybe there is something wrong with my local linux machine. There doesn't seem to be a problem with that package, and it seems to be a problem with my computer. Sorry for raising the wrong issue. I'll close this issue now that the issue has been resolved.