preact-context icon indicating copy to clipboard operation
preact-context copied to clipboard

Make warnings and logging configurable.

Open Download opened this issue 6 years ago • 6 comments

  • Added interface Log with method warn
  • Added interface Options with fields:
    • providerOptional: Boolean
    • log: Log
  • Replaced noopEmitter with emitter factory createDefaultEmitter
  • Added argument of type Options to createDefaultEmitter
  • Added argument of type Options to createContext
    • Default providerOptional option to false
    • Default log option to console if it exists
  • createContext creates a defaultEmitter by calling createDefaultEmitter and passing the options to it
  • Use the defaultEmitter when there is no emitter
  • Replace console.warn with calls to log.warn

Fixes #24

Download avatar Jan 18 '19 21:01 Download

So I noticed an issue... when context would be created with an options object but without a log option set, it would not have used console... Fixed that, squashed commits then force-pushed. I think it should be ok now... Let me know if you need more changes.

EDIT: Mmm I messed up the commit message.. it is duplicated... anyway maybe you can live with it?

Download avatar Jan 18 '19 21:01 Download

Looks good. Can you also make the logger a global?

valotas avatar Jan 19 '19 09:01 valotas

Hi @valotas

Can you describe how you would make it global?

And would it be an idea to just use https://npmjs.com/package/ulog ?

I wrote this package one day to solve 'the logger problem' for all my packages in one go. It is optimized to be very small (~1kB) and very configurable. Using ulog I would implement the logging like this:

import ulog from "ulog";
const log = ulog('preact-context')
// ...
log.warn('message');

The library user can then use ulog to configure the logging. E.g. setting a key in localStorage log=info would enable info logging throughout the codebase. Setting debug=preact-context would enable debug mode for all loggers named 'preact-context' etc. It also allows injecting formatters etc.

Of course that would mean an extra dependency...

Download avatar Jan 19 '19 11:01 Download

@valotas Can you share your ideas for the logging?

Download avatar Jan 21 '19 13:01 Download

Hi @Download, excuse me for the late reply. I would export an options = { warn: console.warn } object. This is the preact way, so I believe it is good to follow the same pattern

valotas avatar Jan 26 '19 07:01 valotas

@valotas

In the meantime I built a logging library especially for libraries. It is only ~360 bytes minified and gzipped. Would it be an option?

anylogger

Download avatar Mar 10 '19 23:03 Download