在Scotty do块中添加打印 [英] Add print in Scotty do block

查看:62
本文介绍了在Scotty do块中添加打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Haskell的新手,如果问题很傻,请提前抱歉,但是我无法在google中找到解决方案.

I'm very new to Haskell so sorry in advance if the question is silly, but I was not able to find the solution in google.

假设我有一个使用 Scotty 网络框架的程序:

Let's say that I have this program using the Scotty web framework:

responseUserByName :: ActionM ()
responseUserByName = do name <- param "name"
                        user <- liftAndCatchIO $ getUserByUserName name
                        json user

我想添加一个日志,因为它在运行时失败了,我不知道为什么.所以我的想法是在do块中添加一些打印内容以检查值.

I would like to add a log since it is failing at runtime and I cannot tell why. So my idea was to add some print in the do block to check values.

但是由于 do 块必须返回 ActionM ,所以我无法打印并返回 IO .好吧,或者至少我不知道如何.

But since the do block has to return a ActionM I cannot print and return an IO. Well or at least I don't know how.

致谢

推荐答案

我猜想 ActionM 来自

I'm going to guess that ActionM is from Scotty. In that case, you can simply lift IO actions with liftIO, like you already do with liftAndCatchIO:

responseUserByName :: ActionM ()
responseUserByName =
    do name <- param "name"
       user <- liftAndCatchIO $ getUserByUserName name
       liftIO $ putStrLn "this is a log message"
       json user

这篇关于在Scotty do块中添加打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆