How to access Environ.Map in logFn?

I’m (excitedly) upgrading a project to 0.16.0. For the most part it’s been pretty straightforward, there’s one issue I’m not sure how to best approach.

The project is a CLI and I would like it to log to the XDG-cache directory. To that end I have a helper which finds the correct directory based on how various XDG-environment variables are set. I’ve changed the helper to take an Environ.Map, but realize I can’t get my hands on the process’ Environ.Map because the logger is configured in std_options rather than main.

To be honest it never felt great finding/opening the logfile in logFn, given that means repeating that work for every line logged, so I can totally imagine that my approach here is the real problem. Either way, I’d be super grateful for tips towards achieving the goal of logging to a file based on XDG environment variables.

I think you should simply set global variables with the io interface, open file handle and/or file writer, and print to that using Io.Writer.print

So find and open the correct dir/file in application initialization and set it to the global variables.

2 Likes

open the log file in main and put it in a global. Globals are distasteful, but the std logging API is a global API, it is unavoidable.

Which is what @gustafla said, but more clear.

I would not put a writer to it in the global var, as if that would make it too easy to not flush in the logFn, which you usually want to do. logs that are too out of sync are less useful in some contexts.

3 Likes

Thank you both, that worked!