Differenze tra le versioni di "Programmazione:Java/Eclipse RCP/Log4J configurabile da Extension Point"
Riga 1: | Riga 1: | ||
=Plugin Log4J configurabile da Extension Point= | =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: | + | Per avere un logger Log4J configurabile da extension point bisogna prima di tutto crearsi il progetto di tipo plugin, convertirlo in un progetto Java Entermitted Template (JET) e configurare in maniera corretta la cartella di output: |
{| cellspacing=15 width="100%" | {| cellspacing=15 width="100%" | ||
|- valign="top" | |- valign="top" | ||
− | |width=" | + | |width="20%"| |
[[Immagine:LoggerNewProject.jpg|thumb|center]] | [[Immagine:LoggerNewProject.jpg|thumb|center]] | ||
− | |width=" | + | |width="20%"| |
[[Immagine:LoggerNewProject2.jpg|thumb|center]] | [[Immagine:LoggerNewProject2.jpg|thumb|center]] | ||
− | + | |width="20%"| | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |width=" | ||
[[Immagine:LoggerJetConversion.jpg|thumb|center]] | [[Immagine:LoggerJetConversion.jpg|thumb|center]] | ||
− | |width=" | + | |width="20%"| |
[[Immagine:LoggerJetConversion2.jpg|thumb|center]] | [[Immagine:LoggerJetConversion2.jpg|thumb|center]] | ||
+ | |width="20%"| | ||
+ | [[Immagine:LoggerJetConversion3.jpg|thumb|center]] | ||
|} | |} | ||
− | |||
− | |||
− | |||
− | |||
A questo punto è possibile iniziare la scrittura del extension point di configurazione.... | A questo punto è possibile iniziare la scrittura del extension point di configurazione.... |
Versione delle 00:16, 17 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, convertirlo in un progetto Java Entermitted Template (JET) e configurare in maniera corretta la cartella di output:
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++;
}
}
}
}
}
public class LoggerDefinitionEP {
private static final String EXTPOINT_ID = "org.logger.log4j.LoggerDefinition"; //$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 ("Logger".equals(conf.getName())) { //$NON-NLS-1$
LoggerDefinition logger = new LoggerDefinition();
logger.logger = conf.getAttribute("logger"); //$NON-NLS-1$
logger.level = LOG_LEVEL.valueOf(conf.getAttribute("level")); //$NON-NLS-1$
logger.log_in_console = Boolean.valueOf(conf.getAttribute("log_in_console")); //$NON-NLS-1$
IConfigurationElement[] appender_configurations = conf.getChildren();
IConfigurationElement appender_conf;
if (appender_configurations != null && appender_configurations.length > 0) {
for (int app = 0; app < appender_configurations.length;) {
appender_conf = appender_configurations[app];
if ("Appender".equals(appender_conf.getName())) { //$NON-NLS-1$
logger.appenders.add(appender_conf.getAttribute("name")); //$NON-NLS-1$
}
app++;
}
}
Log4JDefinitionBean.getInstance().loggers.add(logger);
}
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.
Conclusione
Ora è possibile utilizzare factory o un qualsiasi manager che inizializzi log4J in questo modo
try {
Log4JGenerator g = new Log4JGenerator();
ByteArrayInputStream bis = new ByteArrayInputStream(g.generate(Log4JDefinitionBean.getInstance()).getBytes());
Properties props = new Properties();
props.load(bis);
bis.close();
PropertyConfigurator.configure(props);
} catch (Exception e) {
System.out.println("Log4J configuration error" + e); //$NON-NLS-1$
}