天天看點

ice替換log

1.實作Ice.Logger

package com.djl;

import org.slf4j.LoggerFactory;

import Ice.Logger;

public class MySl4jLogger implements Ice.Logger {

	@SuppressWarnings("unused")
	private final org.slf4j.Logger logger;

	public MySl4jLogger(org.slf4j.Logger logger) {
		super();
		this.logger = logger;
	}

	public MySl4jLogger(String loggerName) {
		logger = LoggerFactory.getLogger(loggerName);
	}

	@Override
	public Logger cloneWithPrefix(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void error(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public String getPrefix() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void print(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void trace(String arg0, String arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public void warning(String arg0) {
		// TODO Auto-generated method stub

	}

}
           

2.入口類

package com.djl;

import IceBox.Server;

public class MySl4jIceBoxServer {

	public static void main(String[] args) {
		System.out.println("【***********伺服器開啟************】");
		Ice.InitializationData data = new Ice.InitializationData();
		data.properties = Ice.Util.createProperties();
		data.properties.setProperty("Ice.Admin.DelayCreation", "1");
		data.logger = new MySl4jLogger("MyIce_Log_Deploy");
		Server server = new Server();
		System.exit(server.main("ICE.BoxServer", args, data));
		
	}
}
           

3.在application.xml(參考點選打開連結)檔案中添加入口類的全包名,詳細配置如下:

<option>-DAppHome=D:\ice\demo3\node${id}</option>

<option>-DAppId=HellowServer${id}</option>

上面這兩個配置是用來自定義log日志檔案位址使用的,參考4,在各個節點下建立node{節點數}目錄,即可在

這個目錄下生成日志檔案(規範目錄結構,友善使用icepatch2進行項目拷貝,參考點選打開連結)

<icegrid>
  <application name="Demo">
	<properties id="MultiThreaded">
		<property name = "Ice.PrintStackTraces" value="1"/>
		<property name = "Ice.Trace.Retry" value="2"/>
		<property name = "Ice.Trace.Network" value="2"/>
		<property name = "Ice.Trace.ThreadPool" value="2"/>
		<property name = "Ice.Trace.Locator" value="2"/>
	</properties>
    <server-template id="Hello">
		<parameter name="id"/>
		<!-- icebox 應用名-->
		<icebox id = "HellowServer${id}" exe="java" activation="on-demand">
			<properties>
				<properties refid="MultiThreaded"/>
				<!-- 異步調用與本地服務collocationOptimize沖突,當啷個服務在同一個java程序
						時(IceBox),用UseSharedCommunicator參數将其設定為共享Communicator,
						進而開啟服務直接本地調用的優化-->
				<property name="IceBox.UseSharedCommunicator.OnlineBook" value="1"/>
				<!--
				<property name="IceBox.UseSharedCommunicator.另外一個服務名(service)" value="1"/>
						-->
			</properties>
			<option>-DAppHome=D:\ice\demo3\node${id}</option>
			<option>-DAppId=HellowServer${id}</option>
			<!-- 不可缺少 若自定義log,不使用
			<option>IceBox.Server</option>
			 -->
			
			<!-- class檔案的位址以及lib的依賴位址 -->
			<env>CLASSPATH=C:\Program Files (x86)\ZeroC\Ice-3.6.3\lib\*;D:\ice\demo3\project\my_pro_1\lib\*;D:\ice\demo3\project\my_pro_1\classes</env>
			<!-- 更改log使用 -->
			<option>com.djl.MySl4jIceBoxServer</option>
			
			<!-- 用戶端調用時需要這個名字   entry是接口繼承類的全檔案名(可以配置多個service)-->
			<service name="OnlineBook" entry="com.djl.BookServer">
				<adapter name="OnlineBook" id="OnlineBook${id}" endpoints="tcp" replica-group="OnlineBookRep"/>
			</service>
			
		</icebox>
	</server-template>
	
	<replica-group id="OnlineBookRep">
		<load-balancing type="adaptive" n-replicas="0"/>
		<!-- identity要和service名稱(name)一樣 
			 type需要在ice生成的接口的抽象類中找到id,對應下面的type-->
		<object identity="OnlineBook" type="::book::OnlineBook"/>
	</replica-group>
	
    <node name="node1">	
		<!-- template和server-template中的id一樣-->
      <server-instance template="Hello" id="1"/>
    </node>
    <node name="node2">	
		<!-- template和server-template中的id一樣-->
      <server-instance template="Hello" id="2"/>
    </node>
  </application>

</icegrid>
           

4.log配置檔案

# Settings
target=${AppHome}/log/${AppId}.log

# Set root logger level to DEBUG and add appenders
log4j.rootLogger=DEBUG, A1, A2

# A1 ConsoleAppender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c  - %m  %n

# A2 FileAppender
log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=${target}
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p %t %c - %m%n
           
ice