Differenze tra le versioni di "Programmazione:Java/Eclipse RCP/Log4J configurabile da Extension Point"
(11 versioni intermedie di 2 utenti non mostrate) | |||
Riga 1: | Riga 1: | ||
− | Per avere un logger Log4J configurabile da extension point bisogna prima di tutto crearsi il progetto di tipo plugin: | + | [[Programmazione:Java/Eclipse_RCP|<< Back to Eclipse RCP]] |
+ | |||
+ | =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: | ||
{| cellspacing=15 width="100%" | {| cellspacing=15 width="100%" | ||
|- valign="top" | |- valign="top" | ||
− | |width=" | + | |width="20%"| |
− | [[Immagine:LoggerNewProject.jpg]] | + | [[Immagine:LoggerNewProject.jpg|thumb|center]] |
− | |width=" | + | |width="20%"| |
− | [[Immagine:LoggerNewProject2.jpg]] | + | [[Immagine:LoggerNewProject2.jpg|thumb|center]] |
+ | |width="20%"| | ||
+ | [[Immagine:LoggerJetConversion.jpg|thumb|center]] | ||
+ | |width="20%"| | ||
+ | [[Immagine:LoggerJetConversion2.jpg|thumb|center]] | ||
+ | |width="20%"| | ||
+ | [[Immagine:LoggerJetConversion3.jpg|thumb|center]] | ||
|} | |} | ||
− | + | A questo punto è possibile iniziare la scrittura del extension point di configurazione.... | |
+ | |||
+ | ==Definizione degli Extension Point== | ||
+ | Questi sono due esempi di definizione degli Extension Point per la configurazione degli appender e dei logger | ||
+ | |||
+ | *[[Media:AppenderDefinition.exsd.rar|AppenderDefinition.exsd.rar]]: Schema xsd di definizione Extension Point d'esempio per la configurazione degli appender del plugin di logging | ||
+ | |||
+ | *[[Media:LoggerDefinition.exsd.rar|LoggerDefinition.exsd.rar]]: Schema xsd di definizione Extension Point d'esempio per la configurazione dei logger del plugin di logging | ||
+ | |||
+ | ==Definizione dei Bean== | ||
{| cellspacing=15 width="100%" | {| cellspacing=15 width="100%" | ||
+ | |- valign="top" | ||
+ | |width="100%" colspan="2"| | ||
+ | <code java> | ||
+ | public class LoggerDefinition implements IDefinitionBean { | ||
+ | public String logger; | ||
+ | |||
+ | public LOG_LEVEL level = LOG_LEVEL.debug; | ||
+ | |||
+ | public List<String> appenders = new ArrayList<String>(); | ||
+ | |||
+ | public boolean log_in_console = true; | ||
+ | |||
+ | public boolean runtime = false; | ||
+ | |||
+ | public String getAppenders() { | ||
+ | StringBuffer sb = new StringBuffer(); | ||
+ | for (Iterator<String> it = appenders.iterator(); it.hasNext();) { | ||
+ | sb.append(it.next()); | ||
+ | if (it.hasNext()) { | ||
+ | sb.append(","); //$NON-NLS-1$ | ||
+ | } | ||
+ | } | ||
+ | return sb.toString(); | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | |- valign="top" | ||
+ | |width="100%" 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" | |- valign="top" | ||
|width="50%"| | |width="50%"| | ||
− | + | <code java> | |
+ | public class DailyAppenderDef extends AppenderDefinition { | ||
+ | public String date_pattern = "'.'yyyy-MM-dd"; //$NON-NLS-1$ | ||
+ | } | ||
+ | </code> | ||
+ | |||
|width="50%"| | |width="50%"| | ||
− | + | <code java> | |
+ | public class RollingAppenderDef extends AppenderDefinition { | ||
+ | public String max_size = "500KB"; //$NON-NLS-1$ | ||
+ | |||
+ | public String max_backup = "1"; //$NON-NLS-1$ | ||
+ | } | ||
+ | </code> | ||
+ | |- valign="top" | ||
+ | |width="100%" colspan="2"| | ||
+ | <code java> | ||
+ | public class Log4JDefinitionBean implements IDefinitionBean { | ||
+ | private static Log4JDefinitionBean instance; | ||
+ | |||
+ | public static void initialize() { | ||
+ | instance = new Log4JDefinitionBean(); | ||
+ | AppenderDefinitionEP.readConfig(); | ||
+ | LoggerDefinitionEP.readConfig(); | ||
+ | } | ||
+ | |||
+ | static { | ||
+ | initialize(); | ||
+ | } | ||
+ | |||
+ | public static Log4JDefinitionBean getInstance() { | ||
+ | return instance; | ||
+ | } | ||
+ | |||
+ | public LOG_LEVEL rootLevel = LOG_LEVEL.debug; | ||
+ | |||
+ | public List<LoggerDefinition> loggers = new ArrayList<LoggerDefinition>(); | ||
+ | |||
+ | public List<AppenderDefinition> appenders = new ArrayList<AppenderDefinition>(); | ||
+ | } | ||
+ | </code> | ||
|} | |} | ||
− | + | ==Lettura degli Extension Point== | |
+ | <code java> | ||
+ | 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++; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | <code java> | ||
+ | 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++; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
==JET Template== | ==JET Template== | ||
Riga 75: | Riga 280: | ||
<% } | <% } | ||
}%> | }%> | ||
+ | </code> | ||
+ | |||
+ | 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 | ||
+ | |||
+ | <code java> | ||
+ | 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$ | ||
+ | } | ||
</code> | </code> |
Versione attuale delle 10:25, 14 apr 2008
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
Questi sono due esempi di definizione degli Extension Point per la configurazione degli appender e dei logger
- AppenderDefinition.exsd.rar: Schema xsd di definizione Extension Point d'esempio per la configurazione degli appender del plugin di logging
- LoggerDefinition.exsd.rar: Schema xsd di definizione Extension Point d'esempio per la configurazione dei logger del plugin di logging
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$
}