maraisr / diary

📑 Zero-dependency, fast logging library for Node, Browser and Workers
MIT License
255 stars 7 forks source link

Add check to filter log level #24

Open winston0410 opened 1 year ago

winston0410 commented 1 year ago

Is this feature in relation to a problem?

I want to use an env variable to filter log by level. For example, when I use LOG_LEVEL=error, I dont want to see log in level debug.

Describe the solution you'd like

Provide a setup function like enable, so it does not read env var initially, but user can pass env var to it

Alternative

No response

Additional context

No response

maraisr commented 1 year ago

Hi, thanks for the issue! 👋

Wondering, would something like this work for you?

// file: logger.ts

import { defaultReporter, _diary } from 'diary';
import { compare } from 'diary/utils';

export const diary = (name: string) => _diary(name, event => {
  if (compare(event.level, process.env.LOG_LEVEL) return defaultReporter(event);
});

alternatively, leave it with me! Ill have to have a think about a possible api.

winston0410 commented 1 year ago

Hey thanks for coming back, and I think that would work. But I notice that defaultReporter is not exported from diary, only default_reporter?

And yes it would be better if there is a dedicate API that only requires user to change a property to affect which log level to use