-1

I use winston to perform logging currently and have written a common method for it to be used all over project. Problem is, many of logging statements are like, logger.info("here is the data" , data)

With comma as concatenator, i couldn't log data in console. data can also be a content containing comma so I wouldn't be able to just use replace ',' by '+' .

My idea is regex can be like, if text starts with ' or " and its next character is ',' at end of quotes, replace with '+'

Ain't sure if it would be right but still, please help with your suggestions.

Gayathri
  • 1,776
  • 5
  • 23
  • 50
  • perhaps i can look for first occurence of ',' . Problem is i see it in most parts of project that it would take time to check every comma and replace by + . – Gayathri Aug 17 '18 at 08:03
  • You could use regex for match the pattern but need to take care about the unwanted replaces, because regex pattern may match with the other places as well, it is good to manually verify and replace do "Next...Next" instead of "Replace all" – Nitin Dhomse Aug 17 '18 at 08:06
  • True. I agree with it – Gayathri Aug 17 '18 at 08:07
  • is there any other way to achieve this? – Gayathri Aug 17 '18 at 08:09
  • No, your system will not be that much intelligent to understand the exact replace, need a good algorithm or need to write that much intelligent regex, anybody could help you here with regex or suggest a better way – Nitin Dhomse Aug 17 '18 at 08:11
  • even if there's a way to log with winston even in presence of comma would help but not sure if it is possible. – Gayathri Aug 17 '18 at 08:14

1 Answers1

0

Perhaps you can monkey-patch it with something like

logger.info = ((infoFunc) => {
    // create the patched info that concatenates all arguments
    // before calling the original logger.info
    let patch = () => {
        // put any logic here that you need to achieve the desired result.
        var message = args.join('');
        infoFunc(message);
    };
    return patch;
})(logger.info);

Not tested

Just make sure it's run right after the logger is set up.

This will work as a quick fix to get things running but I wouldn't recommend leaving it in your code and should be removed once all calls to logger.info have been cleaned up.

phuzi
  • 12,078
  • 3
  • 26
  • 50