Code Monkey home page Code Monkey logo

Comments (6)

hseeberger avatar hseeberger commented on September 1, 2024

withAccessLog is to be applied to the overall handler/route and RejectionHandlers are part of that. So any rejection should finally result in a HttpResponse and hence show up in the access log.

from accessus.

maciejbak85 avatar maciejbak85 commented on September 1, 2024

well then it does not work this way.. :)

from accessus.

hseeberger avatar hseeberger commented on September 1, 2024

Could you please come up with a minified reproducer?

from accessus.

maciejbak85 avatar maciejbak85 commented on September 1, 2024
 implicit def rejectionHandler =
    RejectionHandler.newBuilder().handleAll[MethodRejection] { rejections =>
      val methods = rejections map (_.supported)
      lazy val names = methods map (_.name) mkString ", "

      respondWithHeader(Allow(methods)) {
        options {
          complete(s"Supported methods : $names.")
        } ~
          complete(MethodNotAllowed, s"HTTP method not allowed, supported methods: $names!")
      }
    }
      .result()

def accessLog(log: LoggingAdapter): AccessLog[Long, Future[Done]] =
    Sink.foreach {
      case ((req, t0), res) =>
        val m = req.method.value
        val p = req.uri.path.toString
        val e = req.uri.query().toString()
        val s = res.status.intValue()
        val t = (now() - t0) / 1000000
        val ip = req.headers
          .collectFirst { case x: `X-Forwarded-For` => x }
          .map(_.addresses)
          .getOrElse("NA")
        log.info(s"$m $ip $p $e $s $t")
        log.info(req.headers.mkString(";"))
    }

 private def now() = System.nanoTime()

Http().bindAndHandle(routes.rootRoutes.withAccessLog(accessLog(Logging(actorSystem, "ACCESS_LOG"))), host, port)

from accessus.

hseeberger avatar hseeberger commented on September 1, 2024

Ah, now I understand. Thanks. You are right.

From the Akka docs:

Once you have defined your custom RejectionHandler you have two options for “activating” it:

  1. Bring it into implicit scope at the top-level
  2. Supply it as an argument to the handleRejections directive

Please use 2. for the time being, because the implicit magic doesn't respect the implicitly defined rejection handler.

from accessus.

hseeberger avatar hseeberger commented on September 1, 2024

Should be fixed in v2.0.0 which should land at Maven Central eventually.

from accessus.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.