Flex日誌管理

來源:互聯網
上載者:User

用過java的日誌架構log4j之後,你就會被它方便而又強大的功能所吸引。我們不僅可以控制日誌輸出的目的地,還可以控制日至輸出層級,便於調試和發布。
    其實在Flex

中也提供了這樣的一個架構,Logging API就是最基本的日誌控制架構,只不過大部分的人都在用最簡單的trace()函數罷了。
    Logging API不僅提供了最基本的trace功能,還提供了log target,也就是輸出的方式。還提供了destination目的地的配置功能。通過我們對log的層級控制我們可以輸
出一些普通訊息而過濾掉debug的資訊。除此之外還可以進行自訂log target,對架構進行擴充。

重要概念和類介紹:

Logger
: 提供了介面發送log到一個特定的target,它實現了ILogger介面。
Log target
:定義了日誌將會被寫道哪裡。Flex

提供了兩類target,TraceTarget和MiniDebugTarget。TraceTarget就是將log輸出到trace()函數輸出的檔案中,也就是flashlog.txt檔案中。當然你也可以自訂一個log target。
Logging Level
:定義了當前系統可輸出的記錄層級。

如下表所示,按照高到低排列。如果現在的level是ALL,那麼系統中所有的日誌都會被輸出。如果是INFO,那麼高於INFO的DEBUG資訊就不會被輸出。這個很容易理解。

 

Logging level constant (int) Description
 ALL (0)   Designates that messages of all logging levels should be logged.
 DEBUG (2) Logs internal Flex

activities. This is most useful when debugging anapplication.Select the
DEBUG logging level to include DEBUG, INFO, WARN, ERROR, and FATAL
messages in your log files.
 INFO (4)  Logs general information.Select the INFO logging level to include INFO, WARN, ERROR, and FATAL messages in your log files.
 WARN (6)   Logs a message when the application encounters a problem. These
problems do not cause the application to stop running, but could lead
to further errors.Select the WARN logging level to include WARN, ERROR,
and FATAL messages in your log files.
 ERROR(8)    Logs a message when a critical service is not available or a
situation has occurred that restricts the use of the application.
Select the ERROR logging level to include ERROR and FATAL messages in
your log files.
 FATAL (1000)  Logs a message when an event occurs that results in the failure of
the application. Select the FATAL logging level to include only FATAL
messages in your log files.

log target 過濾filters

logTarget.filters=["mx.rpc.*","mx.messaging.*"];


這個例子中就是指定了我們要輸出日誌的類和包。只有在mx.rpc和mx.messaging包下的類才能輸出log,忽略其他的。也可以是mxml形式:

<mx:TraceTarget id="logTarget" includeDate="true" includeTime="true"
                  includeCategory="true" includeLevel="true">
            <mx:filters>
                  <mx:Array>
                        <mx:String>mx.rpc.*</mx:String>
                        <mx:String>mx.messaging.*</mx:String>
                  </mx:Array>
            </mx:filters>
           <!-- 0 is represents the LogEventLevel.ALL constant. -->
           <mx:level>0</mx:level>
</mx:TraceTarget>

參數介紹
includeDate="true",輸出的log帶日期
includeTime="true", 輸出的log帶時間
includeCategory="true",輸出的log帶分類資訊,也就是哪個類或者控制項輸出的log
includeLevel="true",輸出的log是否帶level資訊,如[INFO],[DEBUG]等。

來個實用的小例子

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml

" layout="absolute" initialize="initLog()">
 <mx:Script>
  <![CDATA[
   import mx.logging.*;
   import mx.logging.targets.*;
   private var myLogger : ILogger;
   public function printLog(level:Number):void
   {
    if(level ==2)
     myLogger.debug("This is debug click");
    if(level == 4)
     myLogger.info("This is info click");
    if(level == 6)
     myLogger.warn("This is warn click");
    if(level == 8)
     myLogger.error("This is error click");
    if(level ==1000)
     myLogger.fatal("This is fatal click");
   }
   private function initLog():void{
    /*
    // Create a target.
    var logTarget:TraceTarget = new TraceTarget();
    // Log only messages for the classes in the mx.rpc.* and
    // mx.messaging packages.
    logTarget.filters=["*"];
    // Log all log levels.
    logTarget.level = LogEventLevel.ALL;
    // Add date, time, category, and log level to the output.
    logTarget.includeDate = true;
    logTarget.includeTime = true;
    logTarget.includeCategory = true;
    logTarget.includeLevel = true;
    // Begin logging.
    Log.addTarget(logTarget);
    */
    myLogger = Log.getLogger("myCustomClass");
   }
  ]]>
 </mx:Script>
 <mx:TraceTarget level="4" includeDate="true" includeTime="true"
  includeCategory="true" includeLevel="true">
  <mx:filters>
   <mx:Array>
    <mx:String>*</mx:String>
   </mx:Array>
  </mx:filters>
 </mx:TraceTarget>
 <mx:VBox width="100%" height="100%" horizontalAlign="center" verticalAlign="middle">
  <mx:Button label="DEBUG(2)" click="printLog(2)"/>
  <mx:Button label="INFO(4)" click="printLog(4)"/>
  <mx:Button label="WARN(6)" click="printLog(6)"/>
  <mx:Button label="ERROR(8)" click="printLog(8)"/>
  <mx:Button label="FATAL(1000)" click="printLog(1000)"/>

 </mx:VBox>
 
</mx:Application>

 

本文轉自:http://www.javaeye.com/topic/392808

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.