我有一个类,我将它包含在现有的jar中(通过使用jar更新命令jar uf)
因此您现在可能知道此类不可修改,因此我希望以可配置的方式设置我的设置。
我在HashMap中定义了所有生产服务器和开发服务器。(我可以使用Set,但是 选择HashMap来解决另一个问题因为我将ip设为127.0.0.1并希望使用主机名收集IP)
这是我的log4j.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender class="com.Log4JCustomAppender" name="CUSTAPPEN">
<param value="includeDevMachiens" name="no" />
<layout class="org.apache.log4j.PatternLayout">
<param value="%m" name="ConversionPattern" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="CUSTAPPEN" />
</root>
</log4j:configuration>
正如您所看到的,如果Param名称为includeDevMachiens
,如果其值为no
,则不希望在执行消息时执行该消息
从开发服务器运行,反之亦然
这是我的代码,将从中执行消息。
private String includeDevMachienes ;
public String getIncludeDevMachienes() {
return includeDevMachienes;
}
public void setIncludeDevMachienes(String includeDevMachienes) {
this.includeDevMachienes = includeDevMachienes;
}
String hostAddresss = Inet4Address.getLocalHost().getHostAddress();
String include = getIncludeDevMachienes(); // yes or no
how can i use yes or no value here ??
// right now the execute will be called for everything if the hostaddress value is present in the map .
if(hostIPMap.containsValue(hostAddress))
{
execute(logMessage);
}
答案 0 :(得分:0)
if(hostIPMap.containsValue(hostAddress)) {
//Check include is yes/no
if("yes".equalsIgnoreCase(getIncludeDevMachines())){execute(logMessage);}
//do rest of the stuffs related to host
}
答案 1 :(得分:0)
您可以使用<param value="includeDevMachiens" name="no" />
代替此<param name="includeDevMachiens" name="True"
。
获取此值为
boolean include = Boolean.parseBoolean(getIncludeDevMachienes()); // yes or no
然后根据此标志进行操作。