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

3.0.0-rc1 - Unable to log timestamps #1175

Closed
wittyPuneet opened this issue Dec 28, 2017 · 13 comments
Closed

3.0.0-rc1 - Unable to log timestamps #1175

wittyPuneet opened this issue Dec 28, 2017 · 13 comments

Comments

@wittyPuneet
Copy link

I followed the example from the documentation, and created a logger as shown below:

const { createLogger, format, transports } = require('winston');

const logger = createLogger(
    {
        level: 'info',
        transports: [new transports.Console({
            showLevel: false,
            colorize: true,
            timestamp: true
        })
        ]
    }
);

logger.info('Server started');

But, I can seem to get the time stamps working. I get the following output in black and white:

{"message":"Server started","level":"info"}

Based on the documentation at https://github.com/winstonjs/winston/blob/master/docs/transports.md#console-transport, setting timestamp flag to true should put a default timestamp.

I even tried by returning a custom timestamp by passing a function but even that doesn't show up. Perhaps, I missed something but it seems like none of the options work, ie. colorize, showLevel, timestamp.

Or is the documentation out of sync?

@adityamertia
Copy link

No updates on this issue yet? I am facing same issue. Works on winston@2.4.0 but not on winston@3.0.0-rc1

@ChrisAlderson
Copy link
Member

Have a look at the formats you can use with winston@3.0.0rc-1.

const { createLogger, format, transports } = require('winston')

const logger = createLogger({
  level: 'info',
  transports: new transports.Console({
    format: format.combine(
      format.timestamp({
        format: 'YYYY-MM-DD' // Optional for choosing your own timestamp format.
      }),
      format.json()
    )
  })
})

logger.info('Server started')

The example prints out the following:

{"message":"Server started","level":"info","timestamp":"2018-01-17T15:20:55.621Z"}

Or with the formatting option:

{"message":"Server started","level":"info","timestamp":"2018-01-17"}

@justLuiz
Copy link

justLuiz commented Jan 25, 2018

why format.timestamp does not work without format.json being declared after format.timestamp?

@nextsoftpl
Copy link

I just want a simple at the beginning of line just as it was before, I don't want json formatting
[2018-01-17T15:20:55.621Z] INFO sdfsdfsdfsdfdfasfsadf, how can I achieve this?

@cameck
Copy link

cameck commented Feb 12, 2018

On version 2.3.1 I can achieve:
2018-02-12T18:50:25.472Z - INFO: Starting up the process

With:

const winston = require('winston');

const logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)({ timestamp: true, colorize: true}),
  ],
  exceptionHandlers: [
    new (winston.transports.Console)({ timestamp: true, colorize: true }),
  ],
  level: 'INFO',
  levels: {
    FATAL: 0,
    ERROR: 1,
    WARN: 2,
    INFO: 3,
    DEBUG: 4
  },
  colors: {
    INFO: 'blue',
    WARN: 'green',
    ERROR: 'yellow',
    FATAL: 'red'
  }
});

module.exports = logger

@armadillojim
Copy link

armadillojim commented Feb 21, 2018

I was hoping to use the "simple" line-of-text logging. I tried following the docs:

const winston = require('winston');
const logger = winston.createLogger({
    format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.colorize(),
        winston.format.simple()
    ),
    transports: [new winston.transports.Console()]
});

But that winds up mixing text and JSON like:

info: GET /alice?bob {"timestamp":"2018-02-21T02:52:31.452Z"}

I was hoping to prefix each line with the timestamp, and for the timestamp to be printed alone. Something like:

2018-02-21T02:52:31.452Z info: GET /alice?bob

but the comments above indicate that's only possible using the older style of passing options to the Console() constructor.

Any suggestions? Or am I asking the wrong question? That is, do log analyzers prefer JSON these days? TIA.

@ChrisAlderson
Copy link
Member

@armadillojim

Use the printf format like this:

const { createLogger, format, transports } = require('winston')
const { colorize, combine, timestamp, printf } = format

// Define your custom format with printf.
const myFormat = printf(info => {
  return `${info.timestamp} ${info.level}: ${info.message}`
})

const logger = createLogger({
  format: combine(
    timestamp(),
    colorize(),
    myFormat
  ),
  transports: [new transports.Console()]
})

logger.info('foo') // -> 2018-02-21T03:55:19.236Z info: foo

@nextsoftpl
Copy link

nextsoftpl commented Feb 22, 2018

@ChrisAlderson you are the man, also if you want to display the splat I enhanced your answer with:
`
const vsprintf = require('sprintf-js').vsprintf;

const myFormat = printf(info =>${info.timestamp} ${info.level}: ${vsprintf(info.message, ...(info.splat || []))})
`

@reddishtg
Copy link

@ChrisAlderson, thanks so much! The documentation did not seem clear on this to me. Much appreciated!

@indexzero
Copy link
Member

Thanks for helping folks out @ChrisAlderson. Contributions on documentation to improve explaining how formats work would be most welcome 👍

@OzzyTheGiant
Copy link

Note: be careful when using timestamp with metadata, apparently using both format functions leads to putting the time stamp as info.metadata.timestamp instead of info.timestamp

@fodisi
Copy link

fodisi commented May 11, 2019

In case someone needs to log additional metadata, including error's stack, the snippet below does the job (PS: example uses a simple output format). The snipped below targets @armadillojim expected output (described earlier ) and is a tinny improvement based on @ChrisAlderson's answer.

As described by @OzzyTheGiant, when using timestamp with metadata, timestamp is accessed as a property of metadata, not info.

import winston from 'winston';

// Creates the default formatter.
// We deconstruct "metadata" into "timestamp" and "meta", and use them in the default formatter.
const logFormat = winston.format.printf((info) => {
  const { timestamp, ...meta } = info.metadata;
  return `${timestamp} ${info.level}: ${info.message} ${JSON.stringify(meta)}`;
});

// Creates the logger. "padLevels", "colorize" and "padLevels" are optional.
logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    winston.format.padLevels(),
    winston.format.metadata(),
    winston.format.errors({ stack: true }),
    logFormat,
    winston.format.colorize({ all: true })
  ),
  transports: [new winston.transports.Console()],
});



logger.info('Simple log');
logger.info('Logger Info', { logLevel: 'info', colorize: { all: true } });
logger.error('', new Error('Logger error'));

Output:

// 2019-05-11 00:20:53 info:     Simple log {}
// 2019-05-11 00:20:53 info:     Logger Info {"logLevel":"info","colorize":{"all":true}}
// 2019-05-11 00:20:53 error:    Logger error {"stack":"Logger error\n    at ...

@kraag22
Copy link

kraag22 commented Apr 3, 2020

why format.timestamp does not work without format.json being declared after format.timestamp?

This is still the thing 2 year later.

this works
format: winston.format.combine( winston.format.timestamp(), winston.format.json() ),

this doesn't
format: winston.format.combine( winston.format.json(), winston.format.timestamp() ),

for writing logs to file

HSZemi added a commit to SiegeEngineers/aoe2cm2 that referenced this issue Apr 19, 2020
Apparently winston needs json() to be included after timestamp() for
this to work:
winstonjs/winston#1175 (comment)
Mizumaki pushed a commit to Mizumaki/winston that referenced this issue Jun 11, 2020
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