lambdaisland / glogi

A ClojureScript logging library based on goog.log
Mozilla Public License 2.0
119 stars 13 forks source link

Way to unset/remove log handlers? #24

Open martinklepsch opened 1 year ago

martinklepsch commented 1 year ago

Hey there :)

Finally found some time to play with this library and gotta say it's really nice how easy it is to add a custom logger.

During development in the REPL I sometimes wanted to remove all logging handlers just to have a "clean slate". I think there's a check to not add the same handler twice but with a REPL session my handler function might have changed and glogi might not be able to know that it's intended to replace an existing handler.

Is there a way to remove all handlers for a situation like this?

Thanks 🙌

martinklepsch commented 1 year ago

And maybe relatedly: I'm seeing logs being printed in Node.js despite not having called any install method.

 [  0.163s] [app.server] {:server/starting {:port 3000}, :line 165}

The formatting looks like the very default of goog.log so maybe on Node there's already some default handlers in place when loading goog.log?

plexus commented 1 year ago

Martin! Long time no see :D

Remove all handlers (bit hacky, no better API for this at the moment. PR welcome to add a proper function for this)

(->> (goog.object/get lambdaisland.glogi/root-logger "__glogi_handlers__")
     keys
     (run! lambdaisland.glogi/remove-handler))

maybe on Node there's already some default handlers in place?

I don't think so, just started a Node based cljs repl and tried it out, doesn't log anything until I install a handler. Maybe you're loading another library that's adding a goog.log handler?

martinklepsch commented 1 year ago

I don't think so, just started a Node based cljs repl and tried it out, doesn't log anything until I install a handler. Maybe you're loading another library that's adding a goog.log handler?

Ha! That was it indeed. Thanks for the pointer!

PR welcome to add a proper function for this

Do you think something like clear-handlers! to just remove all handlers like in your snippet would do the job?

I could also imagine that handlers are added with a key so when adding one with the same key, the old handler could be removed. From a 'trying this at the REPL' perspective this would be slightly better than clear-handlers! although that's also fine.

In addition I might also suggest to update the docs a bit to call out that the only-once part requires the handler function to remain exactly the same, which might not always be a given with REPLs / hot reload.

What do you think?

plexus commented 1 year ago

You're right, having a key similar to e.g. in add-watch would be more convenient, that's usually how I set up these kind of APIs, but I guess here I didn't. It would make sense to either add a three-arity version to add/remove-handler, or to add new functions that handle the case of adding by key.

Doc clarification is always appreciated, especially if the current docs tripped you up.

alysbrooks commented 1 year ago

I think this functionality is basic enough that we may want to work on it in the near future, so I'm leaving this open.