Search code examples
scalalift

How to log request/response in lift


I have a lift project in which
there is a class extending RestHelper that looks like this

   serve{
        "api" / "mystuff" prefix {
         case a => ...
         case b => ...
          }
    }

How can i log all the requests(including POST parameters) and responses without adding it to each case statement?


Solution

  • To log the requests you can use LiftRules.statelessRewrite (in bootstrap.liftweb.Boot):

    LiftRules.statelessRewrite.append {
       case RewriteRequest(ParsePath("api" :: key :: Nil, "", true, _), _, _) =>
          log.info("REST: %s" format key)
          RewriteResponse("api" :: key :: Nil,true)
    }
    

    That will be intercepted before the following rest server:

    case "api":: key :: Nil Get _ => {
      val email = S.param("email") getOrElse {
        "missing email parameter"
      }
      Full(PlainTextResponse("succeeded: %s, %s" format (key,email)))
    }
    

    Note that url params are kept.

    To log the responses you use LiftRules.afterSend

    UPDATE:

    Using afterSend you can actually access both request and response.