Commit 6abadcb0 authored by Fabien Viale's avatar Fabien Viale
Browse files

Make FileAppender pattern configurable

Add a new rm property which allows to define a conversion pattern for all FileAppenders (job/task server logs)
parent e73163cb
......@@ -169,6 +169,9 @@ pa.log4j.async.appender.buffer.size=10000
# Defines the AsynchFileAppender flush timeout
pa.log4j.async.appender.flush.timeout=50
# Defines the log4j pattern used for all file appenders (used by the scheduler for job/task log files
pa.log4j.file.appender.pattern=%d{ISO8601} %-5p [%c{1.}] %m%n
#-------------------------------------------------------
#--------------- AUTHENTICATION PROPERTIES ------------------
#-------------------------------------------------------
......
......@@ -347,7 +347,12 @@ public enum PAResourceManagerProperties implements PACommonProperties {
/**
* Defines the AsynchFileAppender flush timeout
*/
LOG4J_ASYNC_APPENDER_FLUSH_TIMOUT("pa.log4j.async.appender.flush.timeout", PropertyType.INTEGER, "50");
LOG4J_ASYNC_APPENDER_FLUSH_TIMOUT("pa.log4j.async.appender.flush.timeout", PropertyType.INTEGER, "50"),
/**
* Defines the log4j pattern used for all file appenders (used by the scheduler for job/task log files
*/
LO4J_FILE_APPENDER_PATTERN("pa.log4j.file.appender.pattern", PropertyType.STRING, "%d{ISO8601} %-5p [%c{1.}] %m%n");
/* ***************************************************************************** */
/* ***************************************************************************** */
......
......@@ -26,18 +26,15 @@
package org.ow2.proactive.utils.appenders;
import java.io.File;
import java.util.Enumeration;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Appender;
import org.apache.log4j.AsyncAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.EnhancedPatternLayout;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
import org.apache.log4j.WriterAppender;
import org.apache.log4j.spi.LoggingEvent;
import org.ow2.proactive.resourcemanager.core.properties.PAResourceManagerProperties;
public abstract class FileAppender extends WriterAppender {
......@@ -48,9 +45,13 @@ public abstract class FileAppender extends WriterAppender {
private String filesLocation;
private static EnhancedPatternLayout configuredLayout = null;
public FileAppender() {
setLayout(new PatternLayout("[%d{ISO8601} %-5p] %m%n"));
fetchLayoutFromRootLogger();
if (configuredLayout == null) {
configuredLayout = new EnhancedPatternLayout(PAResourceManagerProperties.LO4J_FILE_APPENDER_PATTERN.getValueAsString());
}
setLayout(configuredLayout);
}
@Override
......@@ -90,34 +91,6 @@ public abstract class FileAppender extends WriterAppender {
return appender;
}
private void fetchLayoutFromRootLogger() {
// trying to get a layout from log4j configuration
Enumeration<?> en = Logger.getRootLogger().getAllAppenders();
if (en != null && en.hasMoreElements()) {
Appender app = (Appender) en.nextElement();
if (app != null && app.getLayout() != null) {
if (app instanceof AsyncAppender) {
Enumeration<?> attachedAppenders = ((AsyncAppender) app).getAllAppenders();
if (attachedAppenders != null && attachedAppenders.hasMoreElements()) {
Appender attachedApp = (Appender) attachedAppenders.nextElement();
setLayoutUsingAppender(attachedApp);
}
} else {
setLayoutUsingAppender(app);
}
}
}
}
private void setLayoutUsingAppender(Appender attachedApp) {
final Layout layout = attachedApp.getLayout();
if (layout instanceof PatternLayout) {
Logger.getRootLogger().trace("Retrieved layout from log4j configuration");
PatternLayout paternLayout = (PatternLayout) layout;
setLayout(new PatternLayout(paternLayout.getConversionPattern()));
}
}
@Override
public void close() {
super.close();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment