[Feature Request]: Static context type/shape scope (With an implementation example) #2545
Labels
Feature Request
Request for new functionality to support use cases not already covered
Needs Investigation
🔎 Search Terms
static, context, static context, context type, static context type, context scope, static scope
The vision
The key point of this feature would be to be able to create a logger that accepts an object of a certain predefined type as context before the extra metadata you want to add. The objective is to be able to define what shape has the metadata you are going to add to the log before knowing the actual instance of the metadata.
For example (I'll show later how this can be implemented), I have defined a logger that can accept an Express.Request object as context before logging, so I can use it like:
requestLogger.with(req).warn({ extra_content : 'extra_content' })
And it would automatically retrieve the relevant information from the
req
object and add it to the metadata before logging.The way to define this static context logger would need two things:
As an example with an Express.Request context:
Notice I have also added the possibility to ignore the context if you want, tho not really recommended because it looses the whole point.
I also made a version where you don't have to use the word 'with' in
yourStaticLogger.with(instance).warn(...)
to make use of this feature, you could just callyourStaticLogger(instance).warn(...)
, but it felt more readable to add the 'with' word. Also it let me add the possibility to not add the context instance at a certain point and just callyourStaticLogger.warn(...)
, tho as said before, this is not very important.What is important and I'm sure you want to know if you have reached this point of the article is the actual implementation of the function that lets you do this magic trick.
I have never contributed to an open source project, but I feel it could be time to start doing it. Since I'm not sure how to do it or which is the correct way of doing it, I thought that starting a discussion on the topic I'd like to contribute to would be a good way, I hope I got it right.
So this is the code I have come up with:
I know it is not THE GREATEST or BIGGEST contribution, but I think it could be a great addition to the project and I'd like to contribute to it if you like it too, let me know your thoughts on it, no hard feelings if you don't like it :).
Use case
As shown before it could be used to give a shape to the scope of a logger rather than a simple static data that gets always added.
Any use case where you have instances of a type of object that repeats in different places and can give context to your logs but you don't want to be unpacking it every time you log.
In the above use case, after definining how the req object unpacks, you can call the log from many parts of you application with a simple
requestLogger.with(req).warn({ extra_content : 'extra_content' })
, where req can be any instance of Express.Request.Below there is a real world example where this could be used.
How it would be done normally:
With this feature (Note: the provided implementation works in real enviroments, it's not an imagined example)
Notice how a lot of boilerplate dissapears, leaving a clean scope with easily added context
Additional information
I know it can probably have efficiency improvements and that functions could be named differently in order to be more integrated with the project, but I'm sure someone more familiarized with it can aim to the write place to integrate them.
The mayor flaw I see to this is that it creates a function that returns a logger, which means it would be creating new child logger every time, idk how cost efficient that is but I feel like child loggers are not designed for this purpouse, therefore I suggest it should be more like a native function or overload, like another way to create child loggers that accept a shape and a static context rather than just a static context.
It would also be nice if this could be chainable, to be able to create a static context type logger from another static context type logger.
Also I'll apologize for my english, I believe I have explained it correctly but, as english is not my main language I may have misspelled some things or wrongly expressed them, let me know!
The text was updated successfully, but these errors were encountered: