Flex のロギング

ずっと前から言われてることだけど、trace() の処理は重い。
かなり重い。とにかく重い。

一方 Flex のロギング API は軽い。trace() と比べると断然軽い。しかもリリースビルドして AIR にログを送ることもできるので便利。

簡単な例を作ってみた。

flexlogger

AIR アプリ: FlexLogger.air
ソース: FlexLogger.zip

上の AIR アプリをインストールして、下のボタンを押すと押すとログが取得できます。

デモとソースは以下。

This movie requires Flash Player 10

jp/playwell/logging/LoggingTarget.as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package jp.playwell.logging
{
    import flash.events.StatusEvent;
    import flash.net.LocalConnection;
 
    import mx.core.mx_internal;
    import mx.logging.ILogger;
    import mx.logging.Log;
    import mx.logging.LogEvent;
    import mx.logging.targets.LineFormattedTarget;
 
    use namespace mx_internal;
 
    public class LoggingTarget extends LineFormattedTarget
    {
        protected var lc:LocalConnection;
        protected var currLevel:uint;
 
        public function LoggingTarget()
        {
            super();
            lc = new LocalConnection();
            lc.addEventListener(StatusEvent.STATUS,
                lc_statusHandler);
            Log.addTarget(this);
        }
        private function lc_statusHandler(event:StatusEvent):void
        {
            //if (event.level == "error")
            //    trace("jp.playwell.logging.LoggingTarget: error");
        }
 
        override public function logEvent(event:LogEvent):void
        {
            currLevel = event.level;
            super.logEvent(event);
        }
        override mx_internal function internalLog(message:String):void
        {
            localConnection.send("_FlexLogger",
                "log", currLevel, message);
        }
    }
}

LocalConnection の接続名の先頭のアンダースコアが大切なのです。

LoggingTest.mxml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
    xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns:logging="jp.playwell.logging.*"
    layout="vertical" verticalAlign="middle">
    <mx:Script>
        <![CDATA[
            import mx.logging.ILogger;
            import mx.logging.Log;
 
            private function getLogger():ILogger
            {
                return Log.getLogger("LoggingTest");
            }
        ]]>
    </mx:Script>
 
    <logging:LoggingTarget
        includeCategory="true"
        includeLevel="true"
        includeTime="true"/>
 
    <mx:Button label="logger.debug()" width="150"
        click="getLogger().debug('デバッグよ')"/>
    <mx:Button label="logger.info()" width="150"
        click="getLogger().info('情報です')"/>
    <mx:Button label="logger.warn()" width="150"
        click="getLogger().warn('警告だ')"/>
    <mx:Button label="logger.error()" width="150"
        click="getLogger().error('エラーだね')"/>
    <mx:Button label="logger.fatal()" width="150"
        click="getLogger().fatal('致命的なエラーだぜ')"/>
</mx:Application>

今回は LocalConnection でログを送っているけど、AIR を使えばローカルファイルに保存したりも簡単にできる。べんり。

さあこれからは trace() よりも mx.logging.* を使おう。

Leave a Reply