valotas / preact-context

React new Context API for preact
Other
151 stars 8 forks source link

Make warnings and logging configurable. #29

Open Download opened 5 years ago

Download commented 5 years ago

Fixes #24

Download commented 5 years ago

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?

valotas commented 5 years ago

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

Download commented 5 years ago

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 commented 5 years ago

@valotas Can you share your ideas for the logging?

valotas commented 5 years ago

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

Download commented 5 years ago

@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