Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

At least passing context objects everywhere is better than dynamic dependency injection. I'm in the "dump the logger in a global variable" boat too though.


I was somewhat in that boat too, until the first time I had to make several modules in the code log in a special way (that required some custom code), determined at runtime.

Mostly I just wish more languages had Lisp-style dynamic binding / "special variables". Logging is one of the perfect use cases for dynamic scope - you'd have your normal logger object/configuration as the top-level value of a global, and then let-bind it whenever you need to alter its value for all code executed within that specific scope.

Alas, about the only widely-used form of dynamic binding today is environment variables.


I do this in Scala, via https://www.scala-lang.org/api/2.12.13/scala/util/DynamicVar...

It's not perfect, since it uses JVM's thread-local storage under the hood; this can break when e.g. evaluating Futures in a ThreadPool. For variables which are rarely-overridden, like loggers, I do so with a wrapper that also switches the ExecutionContext to a new ThreadPool (urgh, multithreading...)

PS: I do the same for env vars too ;) http://www.chriswarbo.net/blog/2021-04-08-env_vars.html


That's a neat post, thanks for sharing! I didn't realize Scala had dynamic variables.

As for the HN comment that prompted your blog article, I did a double-take reading it, because I could've sworn I wrote the exact same thing around the same time - turns out I did, though on a different thread :).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: