tslog
tslog copied to clipboard
attachTransport to have the formatted string only
Hi,
maybe this is a missing feature, or I do not know how to use attachTransport
.
Snippet:
import { Logger, ILogObj } from "tslog";
const logger: Logger<ILogObj> = new Logger({
prettyLogTemplate: "{{yyyy}}.{{mm}}.{{dd}} {{hh}}:{{MM}}:{{ss}}:{{ms}}\t[{{logLevelName}}] ",
type: "pretty",
prettyLogTimeZone: "local"
});
logger.attachTransport((logObj) => {
console.log(JSON.stringify(logObj));
});
logger.info("I am a silly log.");
This gives on the console:
2023-07-19 20:55:27.967 [INFO] I am a silly log.
{"0":"I am a silly log.","_meta":{"runtime":"Nodejs","runtimeVersion":"v16.14.2","hostname":"juxe-PC","date":"2023-07-19T18:55:27.967Z","logLevelId":3,"logLevelName":"INFO","path":{"fullFilePath":"/home/juxeii/projects/tstest/myscript.js:12:8","fileName":"myscript.js","fileNameWithLine":"myscript.js:12","fileColumn":"8","fileLine":"12","filePath":"/myscript.js","filePathWithLine":"/myscript.js:12","method":"Object.<anonymous>"}}}
As you can see, I have created a pretty template.
But how can I just print the formatted output in the attachTransport
function?
This function works with a JSON object, which is not formatted.
Do I really need to format again in attachTransport
?
All I want, is that console.log(...)
prints 2023-07-19 20:55:27.967 [INFO] I am a silly log.
EDIT: the exact same thing has been already requested here Issue205 Has this been addressed?
I added some workaround notes because this annoyed me too: https://github.com/fullstack-build/tslog/issues/205#issuecomment-1702008472
As an alternative, you can also override transportFormatted
which has the formatted string as the first argument. The only caveat with this is you have to do it when creating the logger, you can't attach the transport lazily or conditionally later.