Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Documentation for File Logging #107

Open
locallycompact opened this issue Jun 9, 2018 · 3 comments
Open

Documentation for File Logging #107

locallycompact opened this issue Jun 9, 2018 · 3 comments

Comments

@locallycompact
Copy link
Collaborator

Hi, I can't find anywhere in the documentation that covers writing out a log to both a file and to stdout. I expected to be able to do something like this:

configHandle <- openFile ("log.txt") WriteMode
logOptions <- logOptionsHandle stdout True <> logOptionsHandle configHandle True
withLogFunc logOptions $ flip runRIO $ do
             ...

which is obviously wrong as LogOptions isn't a monoid, but I can't see the actual way to do this.

Thanks

@snoyberg
Copy link
Collaborator

Better docs for this would definitely be a good thing. If we can come up with a solution, would you consider sending a doc PR?

I think what you're looking for is the Monoid instance of LogFunc itself:

withLogFunc options1 $ \lf1 -> withLogFunc options2 $ \lf2 ->
  runRIO (lf1 <> lf2) ...

Does that work for you?

@locallycompact
Copy link
Collaborator Author

Happy to put together a PR. :) That certainly compiles but it doesn't seem to be writing to file. Here's what I have:

{-# LANGUAGE NoImplicitPrelude #-}
 
import RIO
import System.IO

main = do
  configHandle <- openFile "foo.txt" WriteMode
  logStdout <- logOptionsHandle stdout True
  logFoo <- logOptionsHandle configHandle True

  withLogFunc logStdout $ \lfs ->
   withLogFunc logFoo    $ \lfo ->
    runRIO (lfs <> lfo) $
     logInfo . displayShow $ "foo"

This touches foo.txt but leaves it empty, and similarly if I run with just logFoo. What could be wrong here?

@snoyberg
Copy link
Collaborator

I think it's a lack of flushing. Using withBinaryFile instead seems to work:

{-# LANGUAGE NoImplicitPrelude #-}

import RIO

main = withBinaryFile "foo.txt" WriteMode $ \configHandle -> do
  logStdout <- logOptionsHandle stdout True
  logFoo <- logOptionsHandle configHandle True

  withLogFunc logStdout $ \lfs ->
   withLogFunc logFoo    $ \lfo ->
    runRIO (lfs <> lfo) $
     logInfo . displayShow $ "foo"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants