Differenze tra le versioni di "Programmazione:Java/Eclipse RCP/Log4J configurabile da Extension Point"
Riga 30: | Riga 30: | ||
==Definizione degli Extension Point== | ==Definizione degli Extension Point== | ||
− | |||
<code xml> | <code xml> | ||
Riga 237: | Riga 236: | ||
<?xml version='1.0' encoding='UTF-8'?> | <?xml version='1.0' encoding='UTF-8'?> | ||
<!-- Schema file written by PDE --> | <!-- Schema file written by PDE --> | ||
− | <schema targetNamespace="org. | + | <schema targetNamespace="org.logger.log4j"> |
<annotation> | <annotation> | ||
<appInfo> | <appInfo> | ||
− | <meta.schema plugin="org. | + | <meta.schema plugin="org.logger.log4j" id="LoggerDefinition" name="LoggerDefinition"/> |
</appInfo> | </appInfo> | ||
<documentation> | <documentation> |
Versione delle 17:20, 16 nov 2007
Indice
Plugin Log4J configurabile da Extension Point
Per avere un logger Log4J configurabile da extension point bisogna prima di tutto crearsi il progetto di tipo plugin:
Dopo bisogna convertirlo in un progetto Java Entermitted Template (JET):
e configurarlo in maniera corretta:
A questo punto è possibile iniziare la scrittura del extension point di configurazione....
Definizione degli Extension Point
<?xml version='1.0' encoding='UTF-8'?>
<schema targetNamespace="org.logger.log4j">
<annotation>
<appInfo>
<meta.schema plugin="org.logger.log4j" id="AppenderDefinition" name="AppenderDefinition"/>
</appInfo>
<documentation>
[Enter description of this extension point.]
</documentation>
</annotation>
<element name="extension">
<complexType>
<sequence>
<element ref="DailyAppender" minOccurs="1" maxOccurs="unbounded"/>
<element ref="RollingAppender" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>
<element name="DailyAppender">
<complexType>
<attribute name="name" type="string" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="file" type="string" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="append" type="boolean" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="layout_class" type="string">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute kind="java"/>
</appInfo>
</annotation>
</attribute>
<attribute name="layout_conversion_pattern" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="date_pattern" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
</complexType>
</element>
<element name="RollingAppender">
<complexType>
<attribute name="name" type="string" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="file" type="string" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="append" type="boolean" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="layout_class" type="string">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute kind="java"/>
</appInfo>
</annotation>
</attribute>
<attribute name="layout_conversion_pattern" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="max_size" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="max_backup" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
</complexType>
</element>
<annotation>
<appInfo>
<meta.section type="since"/>
</appInfo>
<documentation>
[Enter the first release in which this extension point appears.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="examples"/>
</appInfo>
<documentation>
[Enter extension point usage example here.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="apiInfo"/>
</appInfo>
<documentation>
[Enter API information here.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="implementation"/>
</appInfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="copyright"/>
</appInfo>
<documentation>
</documentation>
</annotation>
</schema>
org.logger.log4j.LoggerDefinition.exsd
<?xml version='1.0' encoding='UTF-8'?>
<schema targetNamespace="org.logger.log4j">
<annotation>
<appInfo>
<meta.schema plugin="org.logger.log4j" id="LoggerDefinition" name="LoggerDefinition"/>
</appInfo>
<documentation>
[Enter description of this extension point.]
</documentation>
</annotation>
<element name="extension">
<complexType>
<sequence>
<element ref="Logger" minOccurs="1" maxOccurs="unbounded"/>
</sequence>
<attribute name="point" type="string" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="id" type="string">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="name" type="string">
<annotation>
<documentation>
</documentation>
<appInfo>
<meta.attribute translatable="true"/>
</appInfo>
</annotation>
</attribute>
</complexType>
</element>
<element name="Logger">
<complexType>
<sequence minOccurs="1" maxOccurs="unbounded">
<element ref="Appender"/>
</sequence>
<attribute name="logger" type="string" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
<attribute name="level" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
<simpleType>
<restriction base="string">
<enumeration value="error">
</enumeration>
<enumeration value="warn">
</enumeration>
<enumeration value="info">
</enumeration>
<enumeration value="debug">
</enumeration>
<enumeration value="trace">
</enumeration>
</restriction>
</simpleType>
</attribute>
<attribute name="log_in_console" type="boolean" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
</complexType>
</element>
<element name="Appender">
<complexType>
<attribute name="name" type="string" use="required">
<annotation>
<documentation>
</documentation>
</annotation>
</attribute>
</complexType>
</element>
<annotation>
<appInfo>
<meta.section type="since"/>
</appInfo>
<documentation>
[Enter the first release in which this extension point appears.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="examples"/>
</appInfo>
<documentation>
[Enter extension point usage example here.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="apiInfo"/>
</appInfo>
<documentation>
[Enter API information here.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="implementation"/>
</appInfo>
<documentation>
[Enter information about supplied implementation of this extension point.]
</documentation>
</annotation>
<annotation>
<appInfo>
<meta.section type="copyright"/>
</appInfo>
<documentation>
</documentation>
</annotation>
</schema>
Definizione dei Bean
| |
| |
|
|
|
Lettura degli Extension Point
public class AppenderDefinitionEP {
private static final String EXTPOINT_ID = "org.logger.log4j.AppenderDefinition"; //$NON-NLS-1$
@SuppressWarnings("unchecked")//$NON-NLS-1$
public static void readConfig() {
IExtensionPoint ep = Platform.getExtensionRegistry().getExtensionPoint(EXTPOINT_ID);
if (ep != null) {
IExtension[] extensions = ep.getExtensions();
IExtension ex;
IConfigurationElement[] configurations;
IConfigurationElement conf;
if (extensions != null && extensions.length > 0) {
for (int e = 0; e < extensions.length;) {
ex = extensions[e];
configurations = ex.getConfigurationElements();
if (configurations != null && configurations.length > 0) {
for (int c = 0; c < configurations.length;) {
conf = configurations[c];
if ("DailyAppender".equals(conf.getName())) { //$NON-NLS-1$
DailyAppenderDef appender = new DailyAppenderDef();
appender.appender = conf.getAttribute("name"); //$NON-NLS-1$
appender.file = conf.getAttribute("file"); //$NON-NLS-1$
appender.append = Boolean.valueOf(conf.getAttribute("append")); //$NON-NLS-1$
if (conf.getAttribute("date_pattern") != null) { //$NON-NLS-1$
appender.date_pattern = conf.getAttribute("date_pattern"); //$NON-NLS-1$
}
if (conf.getAttribute("layout_class") != null) { //$NON-NLS-1$
appender.layout_class = conf.getAttribute("layout_class"); //$NON-NLS-1$
}
if (conf.getAttribute("layout_conversion_pattern") != null) { //$NON-NLS-1$
appender.layout_conversion_pattern = conf.getAttribute("layout_conversion_pattern"); //$NON-NLS-1$
}
Log4JDefinitionBean.getInstance().appenders.add(appender);
} else if ("RollingAppender".equals(conf.getName())) { //$NON-NLS-1$
RollingAppenderDef appender = new RollingAppenderDef();
appender.appender = conf.getAttribute("name"); //$NON-NLS-1$
appender.file = conf.getAttribute("file"); //$NON-NLS-1$
appender.append = Boolean.valueOf(conf.getAttribute("append")); //$NON-NLS-1$
if (conf.getAttribute("max_backup") != null) { //$NON-NLS-1$
appender.max_backup = conf.getAttribute("max_backup"); //$NON-NLS-1$
}
if (conf.getAttribute("max_size") != null) { //$NON-NLS-1$
appender.max_size = conf.getAttribute("max_size"); //$NON-NLS-1$
}
if (conf.getAttribute("layout_class") != null) { //$NON-NLS-1$
appender.layout_class = conf.getAttribute("layout_class"); //$NON-NLS-1$
}
if (conf.getAttribute("layout_conversion_pattern") != null) { //$NON-NLS-1$
appender.layout_conversion_pattern = conf.getAttribute("layout_conversion_pattern"); //$NON-NLS-1$
}
Log4JDefinitionBean.getInstance().appenders.add(appender);
}
c++;
}
}
e++;
}
}
}
}
}
JET Template
Questo è un template JET che permette la generzione di un file di configurazione di log4j
<%@ jet package="org.logger.log4j.logger.generator" imports="org.logger.log4j.logger.bean.*" class="Log4JGenerator" %><%
Log4JDefinitionBean bean = (Log4JDefinitionBean)argument;
%>
log4j.rootLogger=<%=bean.rootLevel.toString()%>
- -----> Logger definition
<% for(java.util.Iterator<LoggerDefinition> it = bean.loggers.iterator(); it.hasNext();) {
LoggerDefinition o = it.next();
if(o.appenders.size() > 0) {
%>
log4j.logger.<%=o.logger%>=<%=o.level.toString()%>,<%= o.log_in_console ? " APPENDER_CONSOLE, " : " "%><%=o.getAppenders()%>
<%}
}
%>
- ----------------------------------------------------------------
- APPENDER_CONSOLE appender
- ----------------------------------------------------------------
log4j.appender.APPENDER_CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.APPENDER_CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.APPENDER_CONSOLE.layout.ConversionPattern=[%d] [%-5p] [%c{3}] - %m %n
<% for(java.util.Iterator<AppenderDefinition> it = bean.appenders.iterator(); it.hasNext();) {
AppenderDefinition o = it.next();
%>
- ----------------------------------------------------------------
- <%=o.appender %> appender
- ----------------------------------------------------------------
log4j.appender.<%=o.appender %>.File=<%=o.file%>
log4j.appender.<%=o.appender %>.layout=<%=o.layout_class%>
log4j.appender.<%=o.appender %>.layout.ConversionPattern=<%=o.layout_conversion_pattern%>
log4j.appender.<%=o.appender %>.append=<%=o.append%>
<%if(o instanceof DailyAppenderDefinition) { %>
- Daily LOG
log4j.appender.<%=o.appender %>=org.apache.log4j.DailyRollingFileAppender
log4j.appender.<%=o.appender %>.DatePattern=<%=((DailyAppenderDefinition)o).date_pattern%>
<% } else if(o instanceof RollingAppenderDefinition) {%>
- Rolling LOG
log4j.appender.<%=o.appender %>=org.apache.log4j.RollingFileAppender
log4j.appender.<%=o.appender %>.MaxFileSize=<%=((RollingAppenderDefinition)o).max_size%>
log4j.appender.<%=o.appender %>.MaxBackupIndex=<%=((RollingAppenderDefinition)o).max_backup%>
<% }
}%>
quando il file viene salvato JET genera automaticamente la classe che produce quest'output.