-
-
Notifications
You must be signed in to change notification settings - Fork 232
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
Add an option for aligning scopes #59
Conversation
The array is being joined into a string and there is nothing on either side of the string being interpolated into.
It seems conceptually simpler to me to wrap an array around a single scope name at the beginning of the function. A `join` on an array with a single item just returns the one item, so the behavior is the same. There is one behavior change, which is that `trim` was only being called when `_scopeName` was an array. I can't tell if this is an inconsistency or intentional, perhaps to allow padding spaces onto the end of the scope name to manually align multiple scopes? If that’s the case, the behavior will be obviated in a future commit.
Every time a new logger instance is created, it stores the scope(s) in a static (class) property. Then when the scopes are output, we can look through the array to find the longest scope(s) in order to figure out how much to pad the scope(s) by. The alignment will happen on the “pointer”. So it will look something like this: ``` [first scope] > output [second scope] > output ``` #49
this._timers = options.timers || new Map(); | ||
this._types = this._mergeTypes(defaultTypes, this._customTypes); | ||
this._stream = options.stream || process.stdout; | ||
this._longestLabel = defaultTypes.start.label.length; | ||
|
||
this.constructor._scopeNames = this.constructor._scopeNames || []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am curious about that, What is the purpose of using this.constructor._scopeNames
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Give the issue (#49) a read, as I go into more depth, but the TL;DR is: Scopes are defined per-instance. But in order to align the scopes, there needs to be some shared state that contains all the scopes, so that we can figure out which is the longest scope. We can create that shared state without having to re-design the interface(s) by using a static/class property. The name is sort of confusing, but this.constructor
allows us to set a static/class property without having to explicitly use the class name, e.g. Signale._scopeNames =
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now its pretty clear to me. Thanks for the explanation. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem!
@klauscfhq Hey! What do you think? |
This is moving on really nice, though I believe that it would be even more useful to have a general alignment option that would align all loggers based on all of their metadata, including filenames, time/date stamps as well as scope names. This way we could tackle the alignment problem as a whole while also covering the scope alignment subproblem individually. For example, by utilizing an The logic should be pretty much the same with only a minor modification, setting |
@klauscfhq That sounds great! I was trying to avoid changing too many things at once, but if you’re cool with it I’m happy to align All The Things™️. 👌🏼 |
@klauscfhq Do you want alignment to be on by default or off? |
@klauscfhq Also, I’m stumped. Why do underlined labels get an additional 20 characters of padding? 🤔 |
@brandonweiss is this PR stuck? |
@OmgImAlexis Oh wow, it’s been a long time—I forgot about this. I think what happened is I had some questions but never heard back from the maintainer. The project I was using Signale on was only using a really small set of Signale’s features, so it became easier to build a custom logger for my exact purposes than to try and extend Signale 😓 |
Every time a new logger instance is created, it stores the scope(s) in a static (class) property. Then when the scopes are output, we can look through the array to find the longest scope(s) in order to figure out how much to pad the scope(s) by.
The alignment will happen on the “pointer”. So it will look something like this:
#49