2012-01-03 16 views
8

में सभी नोड HTTP प्रतिक्रियाओं को वैश्विक रूप से लॉग करने के लिए एक हुक जोड़ना मैं node.js का उपयोग कर रहा हूं और HTTP अनुरोधों और प्रतिक्रियाओं को संभालने के लिए व्यक्त करता हूं। http.ServerRequest ईवेंट का उपयोग करके, मैं एक हुक जोड़ सकता हूं और HTTP अनुरोध लॉग कर सकता हूं। http.ServerResponse के लिए एक समान घटना प्रतीत नहीं होती है और मैं सोच रहा हूं कि मेरे सर्वर द्वारा भेजे गए कोड के एक टुकड़े के साथ सभी HTTP प्रतिक्रियाओं को कैसे लॉग किया जाए?नोड.जेएस/एक्सप्रेस

+0

यह करने के लिए कोई तरीका नहीं है। 'अनुरोध" '' HttpServer' द्वारा उत्सर्जित किया गया है जो व्यक्त करता है। पतला लॉगिंग प्रॉक्सी के साथ '.send' या' .end' को ओवरराइट करना सबसे आसान तरीका होगा। – Raynos

+0

धन्यवाद, मैंने http.ServerResponse विधियों को बंदर-पैचिंग समाप्त कर दिया - यह बहुत अच्छा काम करता था। –

उत्तर

10

मैंने एक पैकेज बनाया जो ऐसी ही चीज़ से संबंधित है।

// Save the real end that we will wrap 
var rEnd = res.end; 

// To track response time 
req._rlStartTime = new Date(); 

// Proxy the real end function 
res.end = function(chunk, encoding) { 
    // Do the work expected 
    res.end = rEnd; 
    res.end(chunk, encoding); 

    // And do the work we want now (logging!) 

    // Save a few more variables that we can only get at the end 
    req.kvLog.status = res.statusCode; 
    req.kvLog.response_time = (new Date() - req._rlStartTime); 

    // Send the log off to winston 
    var level = req.kvLog._rlLevel; 
    delete req.kvLog._rlLevel; 
    logger.log(level, '', req.kvLog); 
}; 

ऊपर कोड चलाता है: express-request-logger

कार्यक्रम के दिल इस तरह है की जाँच करें, यह कुछ अतिरिक्त कोड ताकि आप डेटा की अपनी खुद की कुंजी-मान नक्शा है कि अनुरोध के अनुसार लॉग होता है हो सकता है शामिल एक्सप्रेस में मिडलवेयर के रूप में। कोड पर एक नज़र डालें, और यदि आपके पास और प्रश्न हैं, तो यहां या जिथब पर मेरे साथ संपर्क करें।

+0

नोड.जेएस के मौजूदा संस्करणों में, 'res' को एक' 'फिनिश' 'उत्पन्न करना चाहिए जिसे आप अंत विधि को प्रॉक्सी करने के बजाय उपयोग कर सकते हैं। –

-6

आप सभी प्रतिक्रियाओं के लिए एक्सप्रेस 'res.send() का उपयोग कर रहे हैं, और आप एक्सप्रेस मॉड्यूल में कोड डालने से परहेज नहीं करते हैं, तो आप

में सम्मिलित कर सकते हैं

.../node_modules/एक्सप्रेस/lib/response.js:

43 res.send = function(body, headers, status){ 
44 console.log("\n...your log text here..." + body); 
45 // allow status as second arg 
46 if ('number' == typeof headers) { 
47  status = headers, 
48  headers = null; 
49 } 

मुझे नहीं लगता कि आप एक गैर त्रुटि घटना के लिए सुन सकते हैं - "बंद" res.send पर आग नहीं लगती() ;. मुझे लगता है कि ऐसा इसलिए है क्योंकि भेजें() हमेशा आपके कोड में कहीं भी कहा जाता है।

+4

यह एक बुरा विचार है। इसके बजाय अन्य उत्तरों देखें। –

8

यह अब monkeypatch लिए, जब तक एक खत्म घटना Node.js 0.8.12 के बाद से end() function पर उत्सर्जित होती है के रूप में की जरूरत है।

वास्तव में, यह शुरू में अंत के रूप में प्रकाशित किया गया था 0.8.8 में, (यह भी जाँच this), लेकिन it broke writable streams' duplexes तो यह खत्म करने के लिए दिया गया था।

+2

मैंने नोड 0.8.14 और 0.8.23 में चेक किया है और 'एंड' उत्सर्जित है, न कि' खत्म '। आपके द्वारा लिंक किए गए कामों को 0.9/0.10 शाखाओं में शामिल किया गया है (यह अब कुछ हफ्ते पहले प्रतिबद्ध संदेश के नीचे जीएच द्वारा प्रदर्शित किया जा रहा है)। –

+0

फिनिश इवेंट पर प्रतिक्रिया संदेश प्राप्त करना संभव है? –

+0

'एंड' मेरे लिए भी काम करता प्रतीत होता है, जबकि 'फिनिश' नहीं होता है। मैं उलझन में हूं! – JoeRocc

3

यदि आप केवल लॉग (अनुरोध और/या प्रतिक्रियाएं) लॉग करना चाहते हैं, तो express-winston देखें। , यह अनुरोध/प्रतिक्रिया निकाय भी लॉग कर सकता है। coffeescript में

उदाहरण:

expressWinston.requestWhitelist.push('body') 
expressWinston.responseWhitelist.push('body') 
app.use(expressWinston.logger({ 
     transports: [ 
     new winston.transports.Console({ 
      json: true, 
      colorize: true 
     }) 
     ], 
     meta: true, // optional: control whether you want to log the meta data about the request (default to true) 
     msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}" 
     expressFormat: true, // Use the default Express/morgan request formatting, with the same colors. Enabling this will override any msg and colorStatus if true. Will only output colors on transports with colorize set to true 
     colorStatus: true, // Color the status code, using the Express/morgan color palette (default green, 3XX cyan, 4XX yellow, 5XX red). Will not be recognized if expressFormat is true 
     ignoreRoute: function (req, res) { return false; } // optional: allows to skip some log messages based on request and/or response 
    })); 
+0

यह आज के लिए आधुनिक और शायद सबसे सही जवाब है। –

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^