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]: Formatting output sending extra JSON object in message: {"level":"info"} #306

Open
kzunino opened this issue May 17, 2024 · 0 comments

Comments

@kzunino
Copy link

kzunino commented May 17, 2024

The problem

I am using Winston and the Winston-Loki transport to send logs. I have used this exact solution in another service, and the log format works as intended by sending a JSON string as the message, which can be prettified in a Grafana dashboard. However, for the implementation that doesn't work, Winston seems to be appending an extra JSON object {"level":"info"} to the end of my message, which breaks my ability to prettify the JSON in my dashboards. I have triple-checked my implementation, and both use cases are identical and sending log data in the same sanitized format. Despite this, one implementation continues to append that extra JSON object.

This may be related to winstonjs/winston#1775

  const logBody = {
       level: 'info',
       message: '{"level":"info","message":{"message":"Message received","environment":"development","logLevel":"info","userId":"123"},"labels":{"level":"info"}}',
       labels: {
            level: 'info',   
  }
}

// send log to Grafana
winstonLokiLogger.log(logBody);

// Expected output
{"level":"info","message":{"message":"Message received","environment":"development","logLevel":"info","userId":"123"},"labels":{"level":"info"}}

// Actual output:
{"level":"info","message":{"message":"Message received","environment":"development","logLevel":"info","userId":"123"},"labels":{"level":"info"}}, {"level":"info"}   <-- this gets appended 

What version of Logform presents the issue?

v2.6.0

What version of Node are you using?

v20.10.0

If this is a TypeScript issue, what version of TypeScript are you using?

v5.4.5

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

No response

Minimum Working Example

My winston logger is set up like this:

import { createLogger, format } from 'winston';
import LokiTransport from 'winston-loki';

const { combine, timestamp } = format;

const options = {
  // I have to set this default log to be debug, or debug registers as an info log
  level: 'debug',
  format: combine(timestamp(), format.json()),
  transports: [
    new LokiTransport({
      batching: false,
      host: 'www.logging-endpoint.com',
      json: true,
    }),
  ],
};

const winstonLokiLogger = createLogger(options);

export default winstonLokiLogger;

I am using the logger like this:

// Example log body. The message field is a JSON string

  const logBody = {
       level: 'info',
       message: '{"level":"info","message":{"message":"Message received","environment":"development","logLevel":"info","userId":"123"},"labels":{"level":"info"}}',
       labels: {
            level: 'info',   
  }
}


// send log
winstonLokiLogger.log(logBody);

The expected output, and output that works in one my legacy services, is the message field in JSON format.

Expected output
{"level":"info","message":{"message":"Message received","environment":"development","logLevel":"info","userId":"123"},"labels":{"level":"info"}}

However, I keep getting an extra JSON object appended to the output and this breaks the prettify feature that grafana offers to expand logs. Notice the extra {"level":"info"} at the end of the message.

Actual output
{"level":"info","message":{"message":"Message received","environment":"development","logLevel":"info","userId":"123"},"labels":{"level":"info"}}, {"level":"info"}   <-- this

Additional information

No response

🔎 Search Terms

Format, JSON, level

@kzunino kzunino changed the title [Bug]: Formatting output sending extra JSON object [Bug]: Formatting output sending extra JSON object in message: {"level":"info"} May 17, 2024
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

1 participant