Skip to content
This repository
Browse code

Closes GH-647 Dump **all** logs to a npm-debug.log file on faiure

  • Loading branch information...
commit 7ee4aaf2e89964b72ccc2763778f73ed6ef3ae91 1 parent 5261d27
Isaac Z. Schlueter authored
36  lib/utils/error-handler.js
@@ -5,16 +5,20 @@ var cbCalled = false
5 5
   , log = require("./log")
6 6
   , npm = require("../../npm")
7 7
   , rm = require("./rm-rf")
8  
-  , constants
  8
+  , constants = require("constants")
9 9
   , itWorked = false
10  
-
11  
-try { constants = require("constants") }
12  
-catch (ex) { constants = process }
  10
+  , path = require("path")
13 11
 
14 12
 process.on("exit", function (code) {
15 13
   if (code) itWorked = false
16 14
   if (itWorked) log("ok")
17  
-  else log.win("not ok")
  15
+  else {
  16
+    log.error([""
  17
+              ,"Additional logging details can be found in:"
  18
+              ,"    " + path.resolve("npm-debug.log")
  19
+              ].join("\n"))
  20
+    log.win("not ok")
  21
+  }
18 22
   itWorked = false // ready for next exit
19 23
 })
20 24
 
@@ -137,7 +141,6 @@ function errorHandler (er) {
137 141
               ,"    <http://github.com/isaacs/npm/issues>"
138 142
               ,"or email it to:"
139 143
               ,"    <npm-@googlegroups.com>"
140  
-              ,"Just tweeting a tiny part of the error will not be helpful."
141 144
               ].join("\n"))
142 145
     break
143 146
   }
@@ -153,7 +156,7 @@ function errorHandler (er) {
153 156
 function exit (code) {
154 157
   var doExit = npm.config.get("_exit")
155 158
   log.verbose([code, doExit], "exit")
156  
-  if (code) reallyExit()
  159
+  if (code) writeLogFile(reallyExit)
157 160
   else rm(npm.tmp, reallyExit)
158 161
   function reallyExit() {
159 162
     itWorked = !code
@@ -161,3 +164,22 @@ function exit (code) {
161 164
     else process.emit("exit", code || 0)
162 165
   }
163 166
 }
  167
+
  168
+function writeLogFile (cb) {
  169
+  var fs = require("./graceful-fs")
  170
+    , fstr = fs.createWriteStream("npm-debug.log")
  171
+    , sys = require("util")
  172
+
  173
+  log.history.forEach(function (m) {
  174
+    var lvl = log.LEVEL[m.level]
  175
+      , pref = m.pref ? " " + m.pref : ""
  176
+      , b = lvl + pref + " "
  177
+      , msg = typeof m.msg === "string" ? m.msg
  178
+            : msg instanceof Error ? msg.stack || msg.message
  179
+            : sys.inspect(m.msg, 0, 4)
  180
+    fstr.write(new Buffer(b
  181
+                         +(msg.split(/\n+/).join("\n"+b))
  182
+                         + "\n"))
  183
+  })
  184
+  fstr.on("close", cb)
  185
+}
8  lib/utils/log.js
@@ -126,6 +126,8 @@ function log (msg, pref, level, cb) {
126 126
   npm.emit("log."+normalNames[level], { msg : msg, pref : pref, cb : cb })
127 127
 }
128 128
 
  129
+var loglog = log.history = []
  130
+  , loglogLen = 0
129 131
 npm.on("log", function (logData) {
130 132
   var level = logData.level
131 133
     , msg = logData.msg
@@ -134,6 +136,12 @@ npm.on("log", function (logData) {
134 136
     , show = log.level
135 137
     , spaces = "    "
136 138
     , logFD = npm.config.get("logfd")
  139
+  loglog.push(logData)
  140
+  loglogLen ++
  141
+  if (loglogLen > 2000) {
  142
+    loglog = loglog.slice(loglogLen - 1000)
  143
+    loglogLen = 1000
  144
+  }
137 145
   if (!isFinite(level) || level < show) return cb()
138 146
   if (typeof msg !== "string" && !(msg instanceof Error)) {
139 147
     msg = sys.inspect(msg, 0, 4, true)

0 notes on commit 7ee4aaf

Please sign in to comment.
Something went wrong with that request. Please try again.