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

winston.transports.MongoDB pushes empty root path "/" #192

Open
azwinlam opened this issue Sep 8, 2021 · 3 comments
Open

winston.transports.MongoDB pushes empty root path "/" #192

azwinlam opened this issue Sep 8, 2021 · 3 comments

Comments

@azwinlam
Copy link

azwinlam commented Sep 8, 2021

When using normal winston to push logs to file, only routes (non root only, ( / ) ) gets saved. On mongodbwinston, I am receiving many lines of root routes with no meta, no data.

Anyone have similar problems?

{'method': 'GET', 'path': '/restaurant', 'userId': '60f91de2aacd8b54faa7b662', 'username': 'admin', 'query': {}}
{'method': 'GET', 'path': '/', 'userId': None, 'username': None, 'query': {}} <==== This being logged unneccesarily
{'method': 'GET', 'path': '/', 'userId': None, 'username': None, 'query': {}}
{'method': 'GET', 'path': '/', 'userId': None, 'username': None, 'query': {}}
{'method': 'GET', 'path': '/', 'userId': None, 'username': None, 'query': {}}
{'method': 'GET', 'path': '/restaurant', 'userId': '60f91de2aacd8b54faa7b662', 'username': 'admin', 'query': {}}

@yurijmikhalevich
Copy link
Member

Hi @azwinlam!

Can you, please, share your Winston config and, ideally, a minimal reproducible example?

@azwinlam
Copy link
Author

azwinlam commented Sep 8, 2021

Thanks! Edit for formatting

const dotenv = require("dotenv").config();
require("winston-mongodb");

const {
  combine,
  timestamp,
  label,
  printf,
  prettyPrint,
  colorize,
  json,
  align,
} = winston.format;
const myFormat = printf(({ level, message, label, timestamp }) => {
  return `${timestamp} [${label}] ${level}: ${message}`;
});

const mongoURL = process.env.MONGO_URL || "failed";

const allLogger = winston.createLogger({
  level: "info",
  format: combine(json(), label({ label: "cob-rn6" }), timestamp()),
  transports: [
    new winston.transports.MongoDB({
      level: "info",
      //mongo database connection link
      db: mongoURL,
      options: {
        useNewUrlParser: true,
        useUnifiedTopology: true,
      },
      // A collection to save json formatted logs
      collection: "server_logs",
      format: combine(json(), label({ label: "cob-rn6" }), timestamp()),
    }),
  ],
});

if (process.env.NODE_ENV !== "production") {
  allLogger.add(
    new winston.transports.Console({
      format: combine(json(), label({ label: "cob-rn6" }), timestamp()),
    })
  );
  allLogger.add(
    new winston.transports.File({
      filename: "./log/error.log",
      level: "error",
      format: combine(json(), label({ label: "cob-rn6" }), timestamp()),
    })
  ),
    allLogger.add(
      new winston.transports.File({
        filename: "./log/combined.log",
        format: combine(json(), label({ label: "cob-rn6" }), timestamp()),
      })
    );
}

module.exports = allLogger;
  useNewUrlParser: true,
  useCreateIndex: true,
  useUnifiedTopology: true,
});

const app = express();
app.disable("x-powered-by");
app.use(express.json());

app.use((req, res, done) => {
  const meta = {
    method: req.method,
    path: req.path,
    userId: req.header("User"),
    username: req.header("Username"),
    query: req.query,
  };
  allLogger.info("CombinedLog", { metadata: meta });
  // console.log(req.path);
  done();
});```

@azwinlam
Copy link
Author

azwinlam commented Sep 8, 2021

Let me try to explain the problem again.

When using normal winston logging transport to file. The log is "perfect" in the sense that only requests that passes through route eg. /restaurant, /restaurant/id/, /user, are being logged with meta (good data).

When using winstonMongodb. I am receiving all of the above AND empty " / " routes. Basically doubling my log file with empty meta data from / and the other half is good data from above.

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

No branches or pull requests

2 participants