-
Notifications
You must be signed in to change notification settings - Fork 32
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
Integrating a logging library #61
Conversation
The 'open questions' mentioned above referred to the logging configuration on the CLI/API level. One thing that still should be addressed on the implementation level: The
The child logger inherited the It is not entirely clear what the expectations could be here, but I'll think about some possible approaches for handling that... |
One possible solution for the problem in the previous comment is to not create the child loggers each time that they are requested, but instead store them and offer an option to set the level of all loggers that have been created. So the example from the previous comment would now look like this: import { Loggers } from "./src/logging/Loggers";
const rootLogger = Loggers.get();
rootLogger.level = "info";
const childLogger = Loggers.get("child");
// This will not affect the childLogger:
rootLogger.level = "trace";
rootLogger.trace("trace");
childLogger.trace("trace");
// This will affect the childLogger:
Loggers.setLevel("trace");
rootLogger.trace("trace");
childLogger.trace("trace"); I think this looks "nicer" than the |
The last commit adds options for configuring the logging via the CLI (to some extent). As added to the
Modifying the "target" (i.e. the implementation of the |
Addresses #31
There are many logging libraries for Node.js. I did not go though the list of (currently) 7610 packages that are found when naively searching for
logging
on npm. Even evaulating the ones that have more-than-seven-digit 'Weekly Downloads' (e.g. winston, bunyan, pino, morgan, ts-log, npmlog....) is beyond what could be justified here.Maybe
pino
? Yeah, why not 🤷♂️ ... So this PR adds https://github.com/pinojs/pino/ as a logging library.One reason why I'd still hesitate is that the logging library is something very fundamental. Changing it later can be a pain in the back. I considered to add a "thin layer" around the logging library, just to decouple the code from the library that is used. But there are too many unknowns to sensibly tackle this now. Maybe it will still happen later in this PR...
Right now, the way how this logger can be used is shown in the
LoggingDemo.ts
. The gist isYes, the
LoggerFactory
already is some sort of decoupling from the more specificimport pino...
. But it also serves the purpose of having a point to "hook in" for certain configuration options. The exact options that are required here still have to be evaluated.Beyond that, it currently does return a pino
Logger
object. And the question of abstraction and compatibiltiy then largely depends on how this will be used. Ideally, the only assumptions about that object should be...trace
,debug
,info
,warn
,error
, andfatal
string
(or an 'Error' object)The pino
Logger
actually offers more than that. This includes variable-arguments and message formatting likelogger.info("Some printf-like %d values %O", someNumber, someObject);
or the creation of "child loggers". But these are details that are specific for pino. Not using them may make it easier to switch to another library later.
I replaced the first few
console.log
statements that have been in the code with the corresponding log calls. An example output from running theupgrade
command was shown in #31 . With the pino library, the output will look like this:(Note that this was created with the
trace
log level, which is the highest level. With the default level,info
, only the last line would be printed)Alternatively, the log can also be written in JSON form, like this ...
Some open questions:
Right now, it is possible via
process.env.LOG_LEVEL
, but that may not always be the best solution. E.g. maybe there should be a--debug
/--trace
CLI option, or a more generic--logLevel trace
one....This refers to whether the output should be pretty-printed on the console, or dumped into a file as newline-separated JSON. This could be a CLI option like
--logOutput myFile
or so...The last one really bugs me. One can sprinkle one file with
messages. But when the level there has to be changed, then the only way to accomplish this is with a Search-And-Replace. I'd really prefer if it was possible to configure this as in
so that only the value of the
level
variable has to be changed, e.g. totrace
, to change the level for all these messages.