32

I'm currently working on an application based on Node/Express.js.

I have a lot of different resources files (images, css, js...) that I don't want to be logged when executing my application...

Exemple: my node application logs:

GET /stylesheets/bootstrap.min.css 200 0ms
GET /js/lib/jquery-1.8.2.min.js 200 0ms
GET /stylesheets/bootstrap-responsive.2.1.1.min.css 200 0ms
GET /stylesheets/font-awesome.css 200 0ms
GET /stylesheets/bootstrapSwitch.css 200 0ms
GET /js/lib/jquery-ui-1.8.23.effects.min.js 200 0ms
GET /js/lib/knockout-2.1.0.js 200 0ms
GET /js/src/messages.manage.js 200 0ms
GET /js/lib/bootstrap.2.1.1.min.js 200 0ms
GET /js/lib/bootstrapSwitch.js 200 0ms
...

My question is:

Can I remove these logs based on the extension of a file ? Example: *.js , *.css etc...

4
  • 6
    Move the logging pluggin below the resource pluggin in the app initialization. Done. Woohoo for Connect! Furthermore, please show your code, not your logs. ;)
    – cwharris
    Apr 11, 2013 at 15:48
  • 1
    I should have read the express.js documentation... Thanks for the answer, it worked.
    – gogson
    Apr 11, 2013 at 16:31
  • 3
    @ChristopherHarris please put this comment as an answer so that we can give you credit :)
    – Gates VP
    Apr 11, 2013 at 17:23
  • Does Express.static really not have an option for that?
    – Anthony
    Jan 2, 2020 at 18:01

5 Answers 5

34

Move the logging pluggin below the resource pluggin in the app initialization. Done.

Woohoo for Connect!

2
  • 5
    Excellent, simply answer that works. To be more explicit: first app.use(express.static and after app.use(logger('dev')); or the one you are using
    – Eagle
    Aug 9, 2017 at 20:20
  • What would happen if I commented "app.use(logger('dev'));" totally ? Jul 15, 2019 at 3:39
29

On express.js 4,

comment: //app.use(morgan('dev'));

1
  • This is not a real solution: you remove also the logs about your URL calls. The right solution is the one from @Christopher
    – Eagle
    Aug 9, 2017 at 20:22
1

If you want to disable logs like
POST /mydebug 200 1.380 ms - 2
You must to use 'skip' option in your app.js file:

Replace the line:

app.use(logger('dev');

With:

// only log error responses
app.use(logger('dev', {
    skip: function (req, res) { return res.statusCode < 400 }
}));
0

maybe this helps

app.use((req, res, next) => {
  if (
    [
      ".css",
      ".js",
      ".jpg",
      ".jpeg",
      ".gif",
      ".ico",
      ".png",
      ".bmp",
      ".pict",
      ".csv",
      ".doc",
      ".pdf",
      ".pls",
      ".ppt",
      ".tif",
      ".tiff",
      ".eps",
      ".ejs",
      ".swf",
      ".midi",
      ".mid",
      ".ttf",
      ".eot",
      ".woff",
      ".woff2",
      ".otf",
      ".svg",
      ".svgz",
      ".webp",
      ".docx",
      ".xlsx",
      ".xls",
      ".pptx",
      ".ps",
      ".class",
      ".jar"
    ].indexOf(path.extname(req.path)) == -1
  ) {
    logger("dev")(req, res, next);
  } else {
    next();
  }
});
0

In development, I prefer to know that the API endpoints are being hit, while ignoring the request for served files,

here's how I implemented it

if (config.env === 'development') {
  app.use(logger('dev', {
    skip(req, res) {
      return [
        '.css',
        '.js',
        '.jpg',
        '.jpeg',
        '.gif',
        '.ico',
        '.png',
        '.bmp',
        '.pict',
        '.csv',
        '.doc',
        '.pdf',
        '.pls',
        '.ppt',
        '.tif',
        '.tiff',
        '.eps',
        '.ejs',
        '.swf',
        '.midi',
        '.mid',
        '.ttf',
        '.eot',
        '.woff',
        '.woff2',
        '.otf',
        '.svg',
        '.svgz',
        '.webp',
        '.docx',
        '.xlsx',
        '.xls',
        '.pptx',
        '.ps',
        '.class',
        '.jar'
      ].indexOf(path.extname(req.path)) !== -1;
    }
  }));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.