diary icon indicating copy to clipboard operation
diary copied to clipboard

Feature request: add a way to express "all messages above certain log level"

Open abeforgit opened this issue 3 years ago • 2 comments

In v1.0.2, the setLevel api was removed. In the release notes it is mentioned that this can be replaced by using the new onEmit api. However, as far as I can tell, this cannot express the common requirement of "print all messages from all loggers above a certain loglevel". (If there is a way to do this easily, I'd love to hear it)

Conceptually, I see 3 ways in which this could work.

  • reintroduce the "setLevel" api
  • when creating a diary, also create :error, :warning etc variants, and have the logging methods use those, so you can express "*:warning,*:error" in the filter string
  • allow inheritance of diaries, something along the lines of this:
const customEmitter = (logevent: LogEvent) => { /* logic here */ };
const baseDiary = diary("root", customEmitter);
const myDiary = baseDiary.diary("myDiary"); // its name would then be "root:myDiary"
// myDiary would then automatically have the same emitter logic

I'm speaking out of line here, but to me the last option feels like the most natural fit with the current api

abeforgit avatar Jun 18 '21 09:06 abeforgit

Hey 👋🏻 thank you so much for the issue (and fulfilling a PR) 💯

You raise a few good points,

print all messages from all loggers above a certain loglevel

Yeah the way I use it is almost exactly how you're describing in your PR, with the compare method in the onEmit. Should actually document that one 😅. And ya probably not the best DX to introduce that, especially if youre just wanting the default reporter.

The inheritance is a great idea! Not quite how I had envisioned it, my idea was consumers create their own createLogger function:

const onEmit = () => { ... };

export const createLogger = (name) => diarty(name, onEmit);

that way you can share the onEmit no problems — seeing as we're JavaScript, we typically scope loggers to a package. But ya if we want that rust vibe, abc:123:xyz (like I originally wanted), then yeah we need something to "extend/share".

So with that, thank you so much for the PR — will comment over there as well, you did docs, tests and code. Might amend it a little, just think if the : is a good separator we can enforce. Or if .diary is better/worse than .chapter — you know, "dear diary, today I x. Chapter 2, then I y", this a story telling logger.

Is any of this a blocker for you right now? Might leave it with my brain for a day. Also keen to get more of an insight to how youre using it, keen to jump on a call?

maraisr avatar Jun 19 '21 23:06 maraisr

yeah I totally missed that trick to just make a custom createLogger :D I think that basically covers my entire usecase now that I think about it, the inherited diary names is not something I really need. Would be a good idea to add that example to the docs in any case since I expect this usecase to come up quite a lot

As for the scope inheritance, I do quite like idea of it. Not sure how useful it will be in practice (going too heavy on the inheritance might actually hurt useability, and might discourage topic-based diaries) but it's a style that other loggers (e.g. loglevel ) seem to do as well so it feels quite natural.

Not sure about the diary.chapter syntax. It reads nicely, but how do you continue? diary.chapter.paragraph? and after that you kind of run out of words. It could be a conscious design decision to limit the amount of inheritance, but at that point I think I'd prefer no inheritance at all to avoid confusion.

None of this is really blocking for me, especially now that I see how I can share the onEmit without it.

I am available for a call if you'd like. My timezone is GMT+1 and I tend to be mentally available between 9:00 and 23:00 but practical considerations get in the way of course. This week especially is chaotic, wednesday or thursday will probably work out though.

abeforgit avatar Jun 21 '21 19:06 abeforgit

Hi @abeforgit thank you for your 3 year patience here with this. Going to close this issue as not planned 💕 thank you for being a loyal user all these years!

maraisr avatar May 17 '23 06:05 maraisr