IbatisNet中的Common Logging)

來源:互聯網
上載者:User
文章目錄
  • 主介面介紹
IbatisNet中的Common Logging(轉)  自從apache的jakarta Commons項目中添加了logging組件,logging就成為java開發最通用的日誌組件。我認為Logging組件的出現最主要的有兩個:

1. 統一介面。

java社區中的Log Service組件比較多,log4j,jdk log,LogKit,其他很多項目都提供了自己的日誌組件例如Avalon。這些組件沒有統一的介面和配置,加大了我們選擇使用哪種log組件的難度。Logging組件統一了這些介面,使我們只關注與Logging提供的日誌介面,而具體使用那種日誌組件可以靈活選擇。

2. 可以靈活的決定是否需要日誌,使用什麼樣的日誌實現。

雖然logj4j等日誌組件提供的功能十分強大,但同時這些組件的系統消耗也是比較大的,這些日誌操作有可能會極大地影響系統的效能。所以系統在上線後,很多原先用於調試的日誌功能將不再需要,或者所有的應用程式記錄檔都不需要,或需要我們自己訂製的Log Service。Logging組件首先提供了空實現的日誌,也就是說只要使用這種日誌實現,我們可以在不改變程式的情況下去掉所有日誌輸出。當然我們還可以擴充Logging,實現自己的logLog Service,並在需要的時候部署我們的Log Service。

DotNet中現在主要的日誌功能是Framework提供的系統日誌和移植於java的log4j的Log4Net。其次DotNet中還沒有一個類似於Commons Logging的組件(最少我還不知道)。所以IbatisNet在common項目中實現了一個類似Commons Logging的公用服務。

主介面介紹

IbatisNet的日誌功能是通過適配器

IbatisNet的日誌功能的主介面是ILog,它定義了Log Service的主要功能,這些功能和log4net提供的應該是一樣的,分為7個層級,在public enum LogLevel中定義:

public enum LogLevel
{
All = 0,
Debug = 1,
Info = 2,
Warn = 3,
Error = 4,
Fatal = 5,
Off = 6,
}

對於從1到5的每一個層級分別定義了2個方法和一個唯讀bool屬性,例如Debug層級:

 

void Debug( object message );
void Debug( object message, Exception exception );
bool IsDebugEnabled { get; }

這些我們應該都比較熟悉,不多介紹。

另一個重要的介面是public interface ILoggerFactoryAdapter,它是適配器工廠,具體實現用於產生適配不同日誌實現的IbatisNet Common 中的ILog實現(話說得比較彆扭,看看具體例子就清楚了)。ILoggerFactoryAdapter定義如下:

namespace IBatisNet.Common.Logging
{
public interface ILoggerFactoryAdapter
{
ILog GetLogger( Type type );
ILog GetLogger( string name );
}
}

IbaitsNet中現在提供的實現有ConsoleOutLoggerFA,NoOpLoggerFA,TraceLoggerFA和IBatisNet.Common.Logging.Impl中的Log4NetLoggerFA 。對於前三種適配工廠都是產生Ibatis自訂的log服務的ILog服務;對於IBatisNet.Common.Logging.Impl中的Log4NetLoggerFA 主要用於產生適配log4NetLog Service的ILog實現。我們看一下ConsoleOutLoggerFA的實現。

 

public ILog GetLogger(Type type)
{
return GetLogger( type.FullName );
}

public ILog GetLogger(string name)
{
//略作修改,省略了緩衝
ILog log = new ConsoleOutLogger( name, _Level, _showDateTime, _showLogName,
return log;
}

 

我們可以看到這個適配工廠其實就是構造並返回了一個實現了ILog介面的具體類ConsoleOutLogger。當然該類的建構函式用傳入的屬性數組初始化了該ConsoleOutLogger。現面是ConsoleOutLogger的部分實現:

public class ConsoleOutLogger: ILog //實現了ILog介面

建構函式設定了LogLevel和其他相關的參數bool showDateTime, bool showLogName, string dateTimeFormat。

 

public ConsoleOutLogger( string logName, LogLevel logLevel, bool showDateTime, bool showLogName, string dateTimeFormat)
{
_logName = logName;
_currentLogLevel = logLevel;
_showDateTime = showDateTime;
_showLogName = showLogName;
_dateTimeFormat = dateTimeFormat;
if (_dateTimeFormat != null && _dateTimeFormat.Length > 0)
{
_hasDateTimeFormat = true;
}
}

IsDebugEnabled通過構造時的LogLevel判斷Debug開關狀態。

 

public bool IsDebugEnabled
{
get { return IsLevelEnabled( LogLevel.Debug ); }
}

對於debug輸出

 

public void Debug(object message)
{
Debug( message, null );
}

public void Debug(object message, Exception e)
{
if ( IsLevelEnabled( LogLevel.Debug ) )
{
Write( LogLevel.Debug, message, e );
}
}

Write方法構造日誌資訊,並輸出到System.Console.Out

private void Write( LogLevel level, object message, Exception e )

另外public sealed class NoOpLogger: ILog 是一個不產生任何輸出的預設日誌ILog實作類別。

二.使用

上面簡單的介紹了Ibatis Common Logging的主要介面,現在看看它的使用。Logging的使用很簡單。程式中需要日誌的類或方法可以類似下面的方式實現:

1.通過LogManager擷取一個實現了ILog介面的日誌實作類別,關於使用那種具體實現在設定檔中配置。

private static readonly ILog _logger = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType );



private static readonly ILog _logger = LogManager.GetLogger( typeof(具體類名));

推薦使用第一種。

2.使用ILog的模式

if (_logger.IsDebugEnabled)
{
_logger.Debug("Dao Proxy call to " + invocation.Method.Name);
}

3. 配置Logging使用簡單,關鍵在於配置。配置是通過ConfigurationSectionHandler 處理的,public class ConfigurationSectionHandler: IConfigurationSectionHandler ,其中實現的create方法根據配置產生了LogSetting。LogSetting封裝了FactoryAdapterType和Properties。這些資訊是從配置中擷取的。

public Type FactoryAdapterType{    get { return _factoryAdapterType; }}
public NameValueCollection Properties{ get { return _properties; }}

Common 測試包中內建的配置例子如下:

<configSections>
<sectionGroup name="iBATIS">
<section name="logging" type="IBatisNet.Common.Logging.ConfigurationSectionHandler, IBatisNet.Common" />
</sectionGroup>
</configSections>

上面的配置告訴我們對於iBATIS 配置組中的logging配置節,使用IBatisNet.Common.Logging.ConfigurationSectionHandler。

<iBATIS>
<logging>
<logFactoryAdapter type="IBatisNet.Common.Logging.Impl.ConsoleOutLoggerFA, IBatisNet.Common">
<arg key="showLogName" value="true" />
<arg key="showDateTime" value="true" />
<arg key="level" value="All" />
<arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:SSS" />
< /logFactoryAdapter>
</logging>
</iBATIS>

上面的配置節是Ibatis commons Logging使用的,我們通過LogManger的GetLogger方法擷取ILog時,LogManger會首先去看設定檔中是否已經配置了上面的logging節,如果沒有配置就會構造ConsoleOutLoggerFA 適配工廠類的執行個體(BuildDefaultLoggerFactoryAdapter()),也就是說common logging會使用ConsoleOutLogger。否則如果有配置但logFactoryAdapter中定義的type不是ILoggerFactoryAdapter實現,則也會使用ConsoleOutLogger。否則會按照type指定的類型通過反射產生一個具體ILoggerFactoryAdapter實作類別的子類,並有這個類產生實現了ILog介面的log組件。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.