namshi / winston-graylog2

Graylog2 transport for winston, a nodejs logging module
MIT License
126 stars 62 forks source link

TypeError: cleanedMessage.substring is not a function #73

Open shuat opened 5 years ago

shuat commented 5 years ago

It seems that this transport does not allow for logging of objects. Winston which this is built for allows logging of object and so does console.log.

schfkt commented 5 years ago

@shuat can you show an example (I mean the code that fails) how you log an object using this transport? So that I can quickly see what's the issue.

schfkt commented 5 years ago

@shuat AFAIK, it's possible to log objects with this transport, but an object must have some certain fields (message, etc.). See: https://github.com/namshi/winston-graylog2#upgrading-from-earlier-versions-of-winston-graylog2

logger.info({ message: 'this is an info message', answer: 42 });
// or equivalently
logger.info('this is an info message', { answer: 42 });
Caperious commented 4 years ago

I have the same error and no idea how to resolve it or why it occurs..

harishgupta42 commented 4 years ago

I'm facing same error while using following code:


const loggerFormat = printf(({ level, message, label, timestamp, meta }) => {
   return `${timestamp} [${label}] ${level}: ${message} ${meta ? meta : ''} `
})
const logger = winston.createLogger({
   exitOnError: false,
   format: combine(format.colorize(), format.json(), format.prettyPrint(), format.metadata(), label({ label: process.env.NODE_ENV || `localhost` }), timestamp(), loggerFormat),
   transports: [
      new transports.Console({
         level: 'debug', // Debug messages will be
         handleExceptions: true
      })
   ]
})
bwgz commented 2 years ago

I think I know the problem. The winston-graylog2 code does this ...

log(info, callback) {
  const {message, level, metadata} = info;
  const meta = Object.assign({}, metadata, this.staticMeta);
  const cleanedMessage = message || '';
  const shortMessage = cleanedMessage.substring(0, 100);

If message is an object and not a string then cleanedMessage.substring(0, 100) is going to fail. Looks to me the code assumes the message will be a string or undefined. It could check if the message is an object and then deal with that differently.

GabrielScotaCTF commented 1 year ago

I think I know the problem. The winston-graylog2 code does this ...

log(info, callback) {
  const {message, level, metadata} = info;
  const meta = Object.assign({}, metadata, this.staticMeta);
  const cleanedMessage = message || '';
  const shortMessage = cleanedMessage.substring(0, 100);

If message is an object and not a string then cleanedMessage.substring(0, 100) is going to fail. Looks to me the code assumes the message will be a string or undefined. It could check if the message is an object and then deal with that differently.

I try to send a logger.info({'hello': 123}), just to test it and you are right, it fails with the exactly error on issue name. As a work around, I just check my messages type, and if is an object I convert it using JSON.stringify(message)