Differenze tra le versioni di "Programmazione:Java/Eclipse RCP/Log4J configurabile da Extension Point"

Da WikiSitech.
Vai alla navigazioneVai alla ricerca
Riga 1: Riga 1:
 +
=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:
  
Riga 26: Riga 27:
 
[[Immagine:LoggerJetConversion3.jpg]]
 
[[Immagine:LoggerJetConversion3.jpg]]
  
A questo punto è possibile iniziare la scrittura del template....
+
A questo punto è possibile iniziare la scrittura del extension point di configurazione....
 +
 
 +
==Definizione degli Extension Point==
 +
Ora bisogna definire gli Extension Point di configurazione
 +
 
 +
<code xml>
 +
<?xml version='1.0' encoding='UTF-8'?>
 +
<!-- Schema file written by PDE -->
 +
<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>
 +
</code>
 +
 
 +
'''org.logger.log4j.LoggerDefinition.exsd'''
 +
<code xml>
 +
<?xml version='1.0' encoding='UTF-8'?>
 +
<!-- Schema file written by PDE -->
 +
<schema targetNamespace="org.rimondini.rcp.framework.logger">
 +
<annotation>
 +
      <appInfo>
 +
        <meta.schema plugin="org.rimondini.rcp.framework.logger" 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>
 +
</code>
 +
 
 +
==Definizione dei Bean==
 +
{| cellspacing=15 width="100%"
 +
 
 +
|- valign="top"
 +
|width="33%" colspan="2"|
 +
<code java>
 +
public abstract class AppenderDefinition {
 +
  public String appender, file;
 +
 
 +
  public String layout_class = "org.apache.log4j.PatternLayout"; //$NON-NLS-1$
 +
 
 +
  public String layout_conversion_pattern = "[%d] [%-5p] [%c{3}] - %m %n"; //$NON-NLS-1$
 +
 
 +
  public boolean append;
 +
}
 +
</code>
 +
|- valign="top"
 +
|width="33%"|
 +
<code java>
 +
public class DailyAppenderDef extends AppenderDefinition {
 +
  public String date_pattern = "'.'yyyy-MM-dd"; //$NON-NLS-1$
 +
}
 +
</code>
 +
 
 +
|width="33%"|
 +
<code java>
 +
public class RollingAppenderDef extends AppenderDefinition {
 +
  public String max_size = "500KB"; //$NON-NLS-1$
 +
 
 +
  public String max_backup = "1"; //$NON-NLS-1$
 +
}
 +
</code>
 +
 
 +
|}
 +
 
 +
 
 +
==Lettura degli Extension Point==
  
 
==JET Template==
 
==JET Template==
Riga 77: Riga 469:
 
</code>
 
</code>
  
quando il file viene salvato JET genera automaticamente la classe che produce quest'output
+
quando il file viene salvato JET genera automaticamente la classe che produce quest'output.

Versione delle 17:04, 16 nov 2007

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:

LoggerNewProject.jpg

LoggerNewProject2.jpg

Dopo bisogna convertirlo in un progetto Java Entermitted Template (JET):

LoggerJetConversion.jpg

LoggerJetConversion2.jpg

e configurarlo in maniera corretta:

LoggerJetConversion3.jpg

A questo punto è possibile iniziare la scrittura del extension point di configurazione....

Definizione degli Extension Point

Ora bisogna definire gli Extension Point di configurazione

<?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.rimondini.rcp.framework.logger"> <annotation>

     <appInfo>
        <meta.schema plugin="org.rimondini.rcp.framework.logger" 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

public abstract class AppenderDefinition {

  public String appender, file;
  public String layout_class = "org.apache.log4j.PatternLayout"; //$NON-NLS-1$
  public String layout_conversion_pattern = "[%d] [%-5p] [%c{3}] - %m %n"; //$NON-NLS-1$
  public boolean append;

}

public class DailyAppenderDef extends AppenderDefinition {

  public String date_pattern = "'.'yyyy-MM-dd"; //$NON-NLS-1$

}

public class RollingAppenderDef extends AppenderDefinition {

  public String max_size = "500KB"; //$NON-NLS-1$
  public String max_backup = "1"; //$NON-NLS-1$

}


Lettura degli Extension Point

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()%>

  1. -----> 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()%> <%} } %>

  1. ----------------------------------------------------------------
  2. APPENDER_CONSOLE appender
  3. ----------------------------------------------------------------

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(); %>

  1. ----------------------------------------------------------------
  2. <%=o.appender %> appender
  3. ----------------------------------------------------------------

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) { %>

  1. Daily LOG

log4j.appender.<%=o.appender %>=org.apache.log4j.DailyRollingFileAppender log4j.appender.<%=o.appender %>.DatePattern=<%=((DailyAppenderDefinition)o).date_pattern%> <% } else if(o instanceof RollingAppenderDefinition) {%>

  1. 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.