[Bug]: Formatting output sending extra JSON object in message: {"level":"info"}
Opened this issue · 1 comments
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
Hi, any updates in here? I got the same issue.