Created
February 10, 2017 18:27
-
-
Save vkostyukov/79de535825ba18cc6b4a5078f1c8e290 to your computer and use it in GitHub Desktop.
AccessLog for Finagle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.concurrent.Executors | |
import java.io.{File, FileOutputStream, PrintWriter} | |
import com.twitter.finagle.{Service, SimpleFilter} | |
import com.twitter.finagle.http.{Request, Response} | |
import com.twitter.util.Future | |
final class AccessLog(file: File) extends SimpleFilter[Request, Response] { | |
private[this] final val scheduler = Executors.newSingleThreadExecutor() | |
private[this] final def log(r: Request): Unit = { | |
scheduler.submit(new Runnable { | |
def run(): Unit = { | |
val pw = new PrintWriter(new FileOutputStream(file, true)) | |
try pw.println(s"${r.method} ${r.path} [from: ${r.remoteAddress}]") finally pw.close() | |
} | |
}) | |
} | |
def apply(req: Request, next: Service[Request, Response]): Future[Response] = { | |
log(req) | |
next(req) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment