Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reopen the log files, Error: write after end #33

Closed
Ajido opened this issue Sep 2, 2014 · 4 comments
Closed

Reopen the log files, Error: write after end #33

Ajido opened this issue Sep 2, 2014 · 4 comments

Comments

@Ajido
Copy link

Ajido commented Sep 2, 2014

I tried to reopen the log files but I got 'Error: write after end'.
Does morgan have the function for reopening logger?

var logger = require('morgan');

var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a' });
app.use((logger('dev', { stream: accessLogStream })));

process.on('SIGUSR2', function() {
  console.log('Reopen the log files');
  accessLogStream.removeAllListeners();
  accessLogStream.end();
  accessLogStream.close();
  accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a' });
});
Reopen the log files
[Error: write after end]
[Error: write after end]
@Ajido
Copy link
Author

Ajido commented Sep 2, 2014

I also tried to fix the 'write after end' as below. Thoughts?

+exports.stream
+
 exports = module.exports = function morgan(format, options) {
   if (typeof format === 'object') {
     options = format
@@ -56,12 +58,12 @@
   var fmt = compile(exports[format] || format || exports.default)

   // options
-  var stream = options.stream || process.stdout
+  exports.stream = options.stream || process.stdout
     , buffer = options.buffer;

   // buffering support
   if (buffer) {
-    var realStream = stream
+    var realStream = exports.stream
     var buf = []
     var timer = null
     var interval = 'number' == typeof buffer
@@ -79,7 +81,7 @@
     }

     // swap the stream
-    stream = {
+    exports.stream = {
       write: function(str){
         if (timer === null) {
           timer = setTimeout(flush, interval)
@@ -99,7 +101,7 @@
       if (skip(req, res)) return;
       var line = fmt(exports, req, res);
       if (null == line) return;
-      stream.write(line + '\n');
+      exports.stream.write(line + '\n');

@dougwilson
Copy link
Contributor

No, I want to stop having globals on the export.

The answer is you cannot close the write stream until you stop accepting requests on your http server (i.e. wait for server.close to callback).

@PizzaBrandon
Copy link

As a side thought, if you cannot close the write stream until you stop accepting requests, how would one go about implementing daily log file rotation? It doesn't seem practical to shut down your server every night at a specified time just so you could start a new log file.

@dougwilson
Copy link
Contributor

For log rotation, the stream itself would implement that. You can find file rotation streams on npm like https://www.npmjs.org/package/rotate-file-stream

dougwilson added a commit that referenced this issue Apr 8, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants