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

How can we filter by user-agent? #1214

Closed
quocvu opened this issue Mar 17, 2022 · 2 comments
Closed

How can we filter by user-agent? #1214

quocvu opened this issue Mar 17, 2022 · 2 comments
Labels
question Further information is requested

Comments

@quocvu
Copy link

quocvu commented Mar 17, 2022

I might not always care about logging activities produced by the web crawlers. Does anyone have an example of how to accomplish this?
Thanks

@lamweili
Copy link
Contributor

lamweili commented Jul 6, 2022

Hi, you can now do it with this enhancement by @eyoboue, using nolog in connectLogger.
Detailed documentation at https://log4js-node.github.io/log4js-node/connect-logger.html.

var express = require("express");
var useragent = require('express-useragent');

var log4js = require("log4js");
log4js.configure({
  appenders: {
    console: { type: "console" },
    file: { type: "file", filename: "cheese.log" },
  },
  categories: {
    cheese: { appenders: ["file"], level: "info" },
    default: { appenders: ["console"], level: "info" },
  },
});
var logger = log4js.getLogger("cheese");

var app = express();
app.use(useragent.express());
app.use(
  log4js.connectLogger(logger, {
    level: "auto",
    format: ":method :url",
    nolog: (req, res) => req.useragent.platform.isBot, // do your own filtering for crawlers via `req.useragent`
  })
);

app.get("/", function (req, res) {
  res.send(req.useragent);
});
app.listen(5000);

@quocvu
Copy link
Author

quocvu commented Feb 5, 2023

thank you @lamweili. That worked for me and I was able to filter out the entry which logs the HTTP endpoint being called. However, I want to also filter out all log entries associated with that execution. For example, let's say the bot hit the page https://myhost.com/foo/page1 and when rendering that page, I could get logs from other modules such as my ORM lib. It seems this approach only filters events from express. Any tip would be very appreciated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants