ice替换log

    xiaoxiao2021-12-12  3

    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

    转载请注明原文地址: https://ju.6miu.com/read-900211.html

    最新回复(0)