winston-graylog2 icon indicating copy to clipboard operation
winston-graylog2 copied to clipboard

TypeError: cleanedMessage.substring is not a function

Open shuat opened this issue 5 years ago • 6 comments

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.

shuat avatar May 27 '19 14:05 shuat

@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 avatar May 27 '19 15:05 schfkt

@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 });

schfkt avatar May 27 '19 15:05 schfkt

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

Caperious avatar Jul 23 '20 10:07 Caperious

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
      })
   ]
})

harishgupta42 avatar Sep 10 '20 10:09 harishgupta42

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.

bwgz avatar Apr 30 '22 21:04 bwgz

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)

GabrielScotaCTF avatar Apr 27 '23 19:04 GabrielScotaCTF