Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8158401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:35:16+00:00 2026-06-06T17:35:16+00:00

I am attempting to log only certain events to a file via express.logger(…) with

  • 0

I am attempting to log only certain events to a file via express.logger(…) with a custom function and it is hanging when the response and request do not meet the criteria I have set.

var express = require('express'),
 app = express.createServer(
  express.logger(function(req, res) {
   var status = res.statusCode,
   color = 32;

   if (status >= 500) color = 31;
   else if (status >= 400) color = 33;
   else if (status >= 300) color = 36;

   if (req.method==='POST' && req.originalUrl!=='/plankton') {
    return '\033[90m' + req.method
     + ' ' + req.originalUrl + ' '
     + '\033[' + color + 'm' + res.statusCode
     + ' \033[90m'
     + (new Date - req._startTime)
     + 'ms\033[0m';
   }
   else if (res.statusCode>=400) {
    return '\033[90m' + req.method
     + ' ' + req.originalUrl + ' '
     + ' ' + req.body + ' '
     + '\033[' + color + 'm' + res.statusCode
     + ' \033[90m'
     + (new Date - req._startTime)
     + 'ms\033[0m';
   } else {
// what should be inserted here?
// I only want to log the above events...
   }
  });
 )

return ” in the else does not work. It hangs, my guess is that its not allowing the response to finish/send.

Perhaps I should use a different method to log, but I like the simplicity of the express middleware since it allows me to not have to include log messages everywhere – or can I get around that as well and have a listener for the certain events..

Any ideas on what I should have in the else? Or suggestions on a better method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T17:35:17+00:00Added an answer on June 6, 2026 at 5:35 pm

    You need to understand that the middleware API is based on asynchronous callbacks, not synchronous return values. The return value of your middleware function will be entirely ignored. You need to follow this mold:

    function (req, res, next) {
        //look at req
        //do whatever logging you decide you want to do
        //Tell express that you are done and it can move along the middleware chain
        next();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been attempting to use $_GET to try and manage my log file of
I am attempting to find the latest log entries in my MySQL database. I
I'm attempting to use the python subprocess module to log in to a secure
I am attempting to create a simple program that will keep a text log
I am attempting to write a Perl script that parses a log where on
I am attempting to build a script that will log data that changes every
I'm attempting to save only changed attributes of my model by doing the following:
I am attempting to install a registry key that should only be accessible by
I'm attempting to log a user out of facebook with the Facebook JS SDK,
I'm attempting to install Orchard 1.0 on a Windows 7 box. It has only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.