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

[bunyan] Use message field as message #75

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions packages/bunyan/src/bunyan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ export class LogtailStream extends Writable {
return next(e);
}

// Log should have string `msg` key, > 0 length
if (typeof log.msg !== "string" || !log.msg.length) {
return next();
}

// Logging meta data
const meta: Context = {};

Expand All @@ -42,15 +37,30 @@ export class LogtailStream extends Writable {
}

// Carry over any additional data fields
// NOTE: Bunyan passes messages as log.msg but when it's missing
// we can read 'message' field from passed object.
// WARN: Bunyan ignores 'msg' field in the passed object!
// I have messaged Bunyan author here: https://github.com/trentm/node-bunyan/issues/515#issuecomment-1702682901
Object.keys(log)
.filter(key => ["time", "msg", "level", "v"].indexOf(key) < 0)
.filter(key => ["time", "msg", "message", "level", "v"].indexOf(key) < 0)
.forEach(key => (meta[key] = log[key]));

// Get message
// NOTE: Bunyan passes empty 'msg' when msg is missing
const use_msg_field = log.msg !== undefined && log.msg.length > 0;
const msg = (use_msg_field ? log.msg : log.message) || "<no message provided>";

// Prevent overriding 'message' with 'msg'
// Save 'message' as 'message_field' if we are using 'msg' as message
if (use_msg_field && log.message !== undefined) {
meta.message_field = log.message;
}

// Determine the log level
const level = getLogLevel(log.level);

// Log to Logtail
void this._logtail.log(log.msg, level, meta, stackContextHint as StackContextHint);
void this._logtail.log(msg, level, meta, stackContextHint as StackContextHint);
PetrHeinz marked this conversation as resolved.
Show resolved Hide resolved

next();
}
Expand Down