LoginSignup
0
0

More than 5 years have passed since last update.

Scala/Play!でFirePHP(Wildfireプロトコル)を使って端末側でログを見る

Last updated at Posted at 2013-12-04

12/5のScala Advent calendarは、Wildfireプロトコル(FirePHP)を使うというネタです。なぜScalaなのにPHPという単語が出てくるのか。FirePHPにはクライアント側の拡張により、サーバー上のログをクライアント側で確認できる機能があります。HTTPのヘッダに一定の記述することでそれをクライアント側で解釈しデバッグコンソールで表示するというものです。クライアントとしては、FirefoxならばFirebug+FirePHP, ChromeならばWebugなどの拡張を導入することで使用可能になります。

FirePHP Wiki | Reference / Protocol にWildFireプロトコルが乗ってますが、Scalaでそれを生成するコードとしてはこんな感じでしょうか。

package utility

import play.api.libs.json._

class Wildfire {
  var structureIndex = 1
  var msgIndex = 1
  val msg = scala.collection.mutable.Map[String, String]()

  def toHeaderMap(): Map[String, String] = {
    msg("X-Wf-Protocol-1") = "http://meta.wildfirehq.org/Protocol/JsonStream/0.2"
    msg("X-Wf-1-Plugin-1") = "http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3"
    msg("X-Wf-1-Structure-1") = "http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1"
    msg("X-Wf-1-Index") = (msgIndex - 1).toString

    msg.toMap
  }

  def put(text: String, meta: Map[String, String]) {
    val content = JsArray(Array(Json.toJson(meta), JsString(text))).toString
    msg(s"X-Wf-1-${structureIndex}-1-${msgIndex}") = s"${content.length}|${content}|"
    msgIndex += 1
  }
}

Wildfireクラスのインスタンスを作り、putメソッドでログを生成し、toHeaderMapメソッドで、HTTPヘッダ用のMapを生成できます。Play!で記述したサンプルとしては以下の通りです。

package controllers

import play.api._
import play.api.mvc._

import play.api.libs.iteratee.Enumerator

import utility.Wildfire

object Application extends Controller {

  def index = Action {
    val wildfire = new Wildfire()
    wildfire.put("hoge", Map("Type" -> "ERROR", "File" -> "Application.scala", "Line" -> "0"))
    val headers = wildfire.toHeaderMap() + (CONTENT_TYPE -> "text/plain")
    SimpleResult(
      header = ResponseHeader(OK, headers),
      body = Enumerator()
    )
  }
}

Webug だと、Developer Tools のコンソールにこのようなログが出力されます。
webug.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0