-
Notifications
You must be signed in to change notification settings - Fork 516
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
Emit levels in STRING instead of integers #194
Comments
I was just looking at the code just now for the same functionality. There is no support for that - so my question is whether that's a design decision or if they wouldn't mind me writing support for it. You could either add functionality to the resolveLevel function, or you could add another function e.g. "mapLevelToName". This should accompany new exported name variables - preferably in an object and not all separate like levels are currently. In the meantime, go ahead and use this helper function:
|
@olsonpm Thanks a lot for the directions... But I can't get that suggestion yet... :( Here's what I have... I defined the console stream... I would need a writer function for the record when the format is "bunyan", which triggers this https://github.com/thlorenz/bunyan-format/blob/master/lib/format-record.js#L370
I still can't get it to work...
|
I'm a little confused at what your write function is doing. In your above code - all it's going is setting the local rec.level and doesn't actually write anything. |
…f Integer This commit fixes Issue trentm#1 by adding an option to display the level using its String representation. Suggesting it to be the 0.2.0 version. * modified: lib/format-record.js - Adding a method to map the level suggested by @olsonpm at trentm#194 (comment) - If the option is used, just change the level. * modified: README.md - Updating the documentation by adding the examples of outputing the reports using the levels in String. * new file: example/bunyan-string-level.js - Adding the new example that logs the level using the string representation. * modified: package.json - Bumping the version to 0.2.0
@olsonpm Sorry, I was trying to get something late night :) I pushed a change to the module I use for formatting. I updated the examples and everything works as expected! Thanks for the suggestions and I added a note in the patch about your suggestion! |
Let me know how this patch would apply to Bunyan formatter and I will be glad to submit a pull request here. |
Glad you got it working, and i can definitely relate to the late night mishaps. |
… of Integer This commit fixes Issue thlorenz#1 by adding an option to display the level using its String representation. * modified: lib/format-record.js - Adding a method to map the level suggested by @olsonpm at trentm/node-bunyan#194 (comment) - If the option is used, just change the level. * modified: README.md - Updating the documentation by adding the examples of outputing the reports using the levels in String. * new file: example/bunyan-string-level.js - Adding the new example that logs the level using the string representation.
@marcellodesales I got it working here: https://gist.github.com/trentm/8f16f630ddd73d3f87d4 Not in bunyan core, but it does require a change to bunyan to export the I don't think I want explicit support for this in Bunyan core because it would basically mean an option that results in emiting log records that are almost Bunyan-spec'd log records, but not quite. That said, it is very laborious to customize a stream to do something like this. I hope that with Bunyan 2.x plans (no timeline for that at all) to make bunyan stream handling a lot cleaner, it would be easy to compose the built in rotating file stream and a small object stream that did the Please re-open if I missed something. |
I understand not modifying the current bunyan codebase to cater this use-case, however I do suggest modifying the structure in 2.x to resemble a difference as noted in the following:
Poor naming aside, this would future-proof that portion of the code as well as make it more cohesive. Any helper functions could just be prototyped onto LogLevel by developers wanting to extend its functionality and prevent the need for little utility functions like 'mapLevelToName'. |
@trentm Thanks for adding an example... However, it would be great to not only export thanks! |
This commit fixes Issue #1 by adding an option to display the level using its String representation. Suggesting it to be the 0.2.0 version. * modified: lib/format-record.js - Adding a method to map the level suggested by @olsonpm at trentm/node-bunyan#194 (comment) - If the option is used, just change the level. * modified: README.md - Updating the documentation by adding the examples of outputing the reports using the levels in String. * new file: example/bunyan-string-level.js - Adding the new example that logs the level using the string representation. * modified: package.json - Bumping the version to 0.2.0
I know this is an ancient issue, but for anyone landing here: you can just use |
Still can't actually change the output of the 'level' field to a string though? Thinking about how to post-process the output, or switching to some other lib. |
I don't think so - we ended up doing something like this: function wrappedRedisTransport() {
const rt = new RedisTransport({ /* ... */ });
return {
write: entry => rt.write(Object.assign(entry, {
level: bunyan.nameFromLevel[entry.level]
}))
};
}
bunyan.createLogger({
streams: [{
type: 'raw',
stream: wrappedRedisTransport()
}]
}); |
For the benefit of anyone else trying to log string levels to file, here's what I came up with: function levelStringFileStream(filePath) {
const fileStream = fs.createWriteStream(filePath, {
flags: 'a',
encoding: 'utf8'
});
return {
write: log => {
// Map int log level to string
const clonedLog = Object.assign({}, log, {
levelStr: bunyan.nameFromLevel[log.level]
});
var logLine = JSON.stringify(clonedLog, bunyan.safeCycles()) + '\n';
fileStream.write(logLine);
}
};
} Don't forget to set the type to raw when you use it: bunyan.createLogger({
streams: [{
type: 'raw',
stream: levelStringFileStream('/path/to/file')
}]
}); |
Here's what I came up with.... function myStdOutWithLevelName() {}
myStdOutWithLevelName.prototype.write = function(data) {
var logObject = JSON.parse(data)
// Change log level number to name and write it out
logObject.level = bunyan.nameFromLevel[logObject.level]
process.stdout.write(JSON.stringify(logObject) + '\n')
} bunyan.createLogger({
name: "console",
streams: [
{
level: "trace",
stream: new myStdOutWithLevelName()
}
],
src: false,
serializers: bunyan.stdSerializers
}) |
Why doesn't this work when just using a serializer? E.g.
|
how can I kick out some fields? 'v', for example |
Combined a couple of hints here to be
|
Hi there,
Is there any support to emitting the records as STRING values instead of integers??? We use Splunk to aggregate the logs without any pre-processing step like logstash... Splunk indexes json events and our requirement is to print the String representation to be aligned with other systems...
The requirement is that the console output must be using the "bunyan" format, while the console.log format is the "long", human-readable one... This requirement is only when writing to the rotate-file stream type...
From
To
I found the method
_emit
(https://github.com/trentm/node-bunyan/blob/master/lib/bunyan.js#L778), but it is emitting for all the types :( Any help greatly appreciated!thanks
Marcello
The text was updated successfully, but these errors were encountered: