Search code examples
haskellintellij-ideaterminalintellij-pluginscotty

How to log to terminal using scotty with Intellij (Haskell plugin)?


So I'm having something odd happen. Here's an illustrative code example:

main :: IO ()
main
 = do
     scotty 8000 $ do
       get "/" serve
 where
  serve :: ActionM ()
  serve = do
    liftIO $ print "I'm about to serve a request!"

My message should print to the IntelliJ console whenever I type in "localhost:8000/", but it doesn't. Yet when I comment out the scotty stuff and just have:

main :: IO ()
main
 = do print "Hello World!"

IntelliJ has no problem printing this out. What am I doing wrong? When I use the Windows command prompt to run the executable (e.g. ghc --make to create it, then running it), everything works fine - "I'm about to serve a request!" prints to the command prompt terminal everytime I Type in "localhost:8000/"


Solution

  • Figured it out. Needed to make a logger as follows:

     logger <- mkRequestLogger def {
       outputFormat = CustomOutputFormat (\_ _ _ _ -> "")
       }
    

    so that the resulting code would look like:

    main :: IO ()
    main
     = do
         logger <- mkRequestLogger def {
           outputFormat = CustomOutputFormat (\_ _ _ _ -> "")
           }
         scotty 8000 $ do
           middleware logger
           -- do stuff here
    

    The CustomOutputFormat ignores all other incoming information, focusing only on what you want to print to the console.