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

[Bug]: can't format timestamp using winston.format.timestamp #232

Open
yurijmikhalevich opened this issue Aug 7, 2023 · 1 comment
Open

Comments

@yurijmikhalevich
Copy link
Member

yurijmikhalevich commented Aug 7, 2023

🔎 Search Terms

timezone, timestamp

The problem

Cannot format timestamp using winston.format.timestamp.

What version of Winston presents the issue?

v3.10.0

What version of Node are you using?

v16.13.2

If this worked in a previous version of Winston, which was it?

No response

Minimum Working Example

import winston from 'winston';
import mongodb from 'mongodb';
// Requiring `winston-mongodb` will expose winston.transports.MongoDB`
import 'winston-mongodb';

const MongoClient = mongodb.MongoClient;

const log = winston.createLogger({
  level: 'info',
  transports: [
    new winston.transports.Console({ format: winston.format.simple(), level: 'info' })
  ],
  format: winston.format.combine(
    winston.format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss',
    }),
  ),
});

// logging to console so far
log.info('Connecting to database...');

const url = "mongodb://localhost:27017/mydb";

const client = new MongoClient(url);
await client.connect();

const transportOptions = {
  db: await Promise.resolve(client),
  collection: 'log',
};

log.add(new winston.transports.MongoDB(transportOptions));

// following entry should appear in log collection and will contain
// metadata JSON-property containing url field
log.info('Connected to database.',{url});

Additional information

winston-mongodb: v5.1.1

As a workaround, it's possible to store a custom timestamp under .metadata, like this:

const log = winston.createLogger({
  level: 'info',
  transports: [
    new winston.transports.Console({ format: winston.format.simple(), level: 'info' })
  ],
  format: winston.format.combine(
    winston.format(info => {
      info.metadata = { myTs: new Date().toLocaleDateString() }; // or any other format
      return info;
    })(),
  ),
});
@PenchalaiahY
Copy link

Raised pull request for this issue - #270

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

No branches or pull requests

2 participants