JSF (mojarra2.x) のデバッグログ出力

多くのアプリケーションのログカテゴリには、ログを出力しているパッケージ名(jp.co.test... など)が使われていますが、JSFの参照実装であるmojarra2.xでは少し異なる付与規則となっているため、以下にまとめます。

mojarraのログカテゴリ定義

mojarraのログカテゴリはjavax.enterprise.resource.webcontainer.jsfから始まります。mojarraの実装パッケージはcom.sun.facesから始まっていますが、com.sun.facesをログカテゴリに設定してログレベルを上げても何も出力されません。

カテゴリの一覧はソースコード上のcom.sun.faces.util.FacesLoggerenumとして定義されています。以下のようなカテゴリ定義があります。

  • javax.enterprise.resource.webcontainer.jsf.application
  • javax.enterprise.resource.webcontainer.jsf.resource
  • javax.enterprise.resource.webcontainer.jsf.config
  • javax.enterprise.resource.webcontainer.jsf.context
  • javax.enterprise.resource.webcontainer.jsf.facelets
  • javax.enterprise.resource.webcontainer.jsf.lifecycle
  • javax.enterprise.resource.webcontainer.jsf.managedbean
  • javax.enterprise.resource.webcontainer.jsf.renderkit
  • javax.enterprise.resource.webcontainer.jsf.taglib
  • javax.enterprise.resource.webcontainer.jsf.timing

JSFライフサイクルのロギング

javax.enterprise.resource.webcontainer.jsf.lifecycleカテゴリのログレベルをFINE以上に変更すると、JSFライフサイクル(Restore View -> Apply Request Values ..)の開始・終了時間のロギングが可能です。PhaseListenerによるロギングのようにアプリケーション自体をリビルドしなくても、APサーバの設定変更のみでログ出力が可能です。

ログ出力例

WildFly8.2.0でjavax.enterprise.resource.webcontainer.jsf.lifecycleのログレベルをDEBUGに変更すると、以下のようなログが出力されます。

2015-06-14 16:31:34,393 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-2) execute(com.sun.faces.context.FacesContextImpl@5f6d3a9e)
2015-06-14 16:31:34,393 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-2) Entering RestoreViewPhase
2015-06-14 16:31:34,417 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-2) Postback: restored view for /index.xhtml
2015-06-14 16:31:34,417 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-2) Exiting RestoreViewPhase
2015-06-14 16:31:34,418 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-2) Entering ApplyRequestValuesPhase
2015-06-14 16:31:34,419 FINE  [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-2) Exiting ApplyRequestValuesPhase
...

長くなるため完全な出力例は省略しますが、ログレベルさらに上げると、RenderResponsePhaseのロギングとして、以下のようにUIViewRootをルートとしたコンポーネントツリーの状態をロギングすることもできます。

2015-06-14 16:31:30,817 FINEST [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-1) +=+=+=+=+=+= View structure printout for /index.xhtml
2015-06-14 16:31:30,822 FINEST [javax.enterprise.resource.webcontainer.jsf.lifecycle] (default task-1) id:j_id1
type:javax.faces.component.UIViewRoot
...
APサーバ毎のログレベル設定方法

GlassFish4の場合の設定方法
mojarraのログカテゴリは、デフォルトのlogging.propertiesに既に定義されているため、デフォルトのINFOから好きなログレベルに変更するだけです。ログレベルの表記はSEVEREからFINESTまでのjava.util.logging形式です。一覧はLevelクラスのjavadocに定義されています。

glassfish4/glassfish/domains/domain1/config/logging.properties

javax.enterprise.resource.webcontainer.jsf.lifecycle=FINE

WildFly8 (JBoss) の場合の設定方法
CLIまたは、standalone.xmlを編集してログカテゴリ設定を追加します。java.util.loggingとJBoss Loggingのログレベルの対応表は、jbossのwikiに記載があります。

CLIの場合

${WILDFLY_HOME}/bin/jboss-cli.sh -c
/subsystem=logging/logger=javax.enterprise.resource.webcontainer.jsf.lifecycle:add(level=DEBUG)

standalone.xmlを編集する場合 (jacorbの下に追加)

<logger category="jacorb.config">
    <level name="ERROR"/>
</logger>
<logger category="javax.enterprise.resource.webcontainer.jsf.lifecycle">
    <level name="DEBUG"/>
</logger>
<root-logger>

まとめ

  • mojarra2.xのログカテゴリはcon.sun.facesのような実装パッケージ名でなく、javax.enterprise.resource.webcontainer.jsfから始まる。
  • この仕組みを使ってJSFライフサイクルのデバッグができる