Feature Request: Defaults / Formatter
Description / Use Case for a Feature
I'd really like to see a way to handle defaults with tslog. I appreciate requestId, hostname etc. but I also need to add a few more parameters as I intend to use your logger with Google's Stackdriver, which needs some additional values e.g. for labels, a service context and so on.
I can imagine that there are more use cases for custom defaults that will always be logged. Right now I'm using a transport where I attach that information. But this leads to the output deviating from what is actually being logged by tslog when not using the transport.
Maybe introducing formatters like other logging libraries do could also solve that problem by letting us mutate the arguments before they are sent to either the default transport or a custom one.
Thank you @passionkind for your proposal, I do totally get where you are coming from.
Custom formatters is definitely something that I have on my road map for the 4.x release.
In the meantime, I believe using prefix could potentially solve your problem (https://github.com/fullstack-build/tslog#prefix). Have you tried that out?
I second this, a custom final formatter would be great to enable anyone to override the log output, which should be completely customisable, whether it is for the pretty or the JSON format. Although if I had to choose only one of them, it would be the JSON one, to easily make it compatible with any log collector agent without needing to twist its configuration (not always possible on cloud providers for instance).
Sometimes you just want to change a simple key name, and sometimes entirely override the format. I've been having a pretty hard time doing so with Pino and pino-http, which are kind of customizable, but not always entirely.
V4 beta is out and supports both, custom log objects as well as custom formatters.
Here you can see how you can add a fully typed log object with as many values as you want: https://github.com/fullstack-build/tslog/blob/v4/tests/Nodejs/8_json_LogObj.test.ts#L14
And this is how you can overwrite the formatters:
-
Pretty: https://github.com/fullstack-build/tslog/blob/v4/tests/Nodejs/10_Overwrites.test.ts#L83
-
JSON: https://github.com/fullstack-build/tslog/blob/v4/tests/Nodejs/10_Overwrites.test.ts#L114
Give it a go and let me know if that solves your problem. I'll publish the final version as soon as I have gathered enough feedback.
-
npm i tslog@next -
and run it with
node --enable-source-mapsor for TypeScriptnode --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm
V4 is released now, so I'm going to close this issue. Feel free to open a new one if V4 didn't solve it for you.
Here are the docs: tslog.js.org