use of org.apache.log4j.FileAppender in project cpsolver by UniTime.
the class Test method setupLogging.
/**
* Setup log4j logging
*
* @param logFile
* log file
* @param debug
* true if debug messages should be logged (use -Ddebug=true to
* enable debug message)
*/
public static void setupLogging(File logFile, boolean debug) {
Logger root = Logger.getRootLogger();
ConsoleAppender console = new ConsoleAppender(new PatternLayout("[%t] %m%n"));
console.setThreshold(Level.INFO);
root.addAppender(console);
if (logFile != null) {
try {
FileAppender file = new FileAppender(new PatternLayout("%d{dd-MMM-yy HH:mm:ss.SSS} [%t] %-5p %c{2}> %m%n"), logFile.getPath(), false);
file.setThreshold(Level.DEBUG);
root.addAppender(file);
} catch (IOException e) {
sLog.fatal("Unable to configure logging, reason: " + e.getMessage(), e);
}
}
if (!debug)
root.setLevel(Level.INFO);
}
use of org.apache.log4j.FileAppender in project knime-core by knime.
the class NodeLogger method addWorkflowDirAppender.
/**
* Adds a new workflow directory logger for the given workflow directory if it doesn't exists yet.
* @param workflowDir the directory of the workflow that should be logged to
*/
private void addWorkflowDirAppender(final File workflowDir) {
if (workflowDir == null) {
// if the workflowDir is null we do not need to append an extra log appender
return;
}
// in this method we have to use the logger directly to prevent a deadlock!!!
final Logger logger = m_logger;
final String workflowDirPath = workflowDir.getPath();
if (workflowDirPath == null) {
return;
}
Appender wfAppender = WF_APPENDER.get(workflowDirPath);
if (wfAppender != null) {
logger.addAppender(wfAppender);
} else {
// we do the getAppender twice to prevent the synchronize block on subsequent calls!!!
synchronized (WF_APPENDER) {
// we need a synchronize block otherwise we might create a second appender that opens a file handle
// which never get closed and thus the copying of a full log file to the zip file fails
wfAppender = WF_APPENDER.get(workflowDirPath);
if (wfAppender == null) {
// use the KNIME specific LogfielAppender that moves larger log files into a separate zip file
// and that implements equals and hash code to ensure that two LogfileAppender
// with the same name are considered equal to prevent duplicate appender registration
final FileAppender fileAppender = new LogfileAppender(workflowDir);
fileAppender.setLayout(WF_DIR_LOG_FILE_LAYOUT);
fileAppender.setName(workflowDirPath);
final Filter mainFilter = LOG_FILE_APPENDER.getFilter();
fileAppender.addFilter(new Filter() {
@Override
public int decide(final LoggingEvent event) {
final Object msg = event.getMessage();
if (msg instanceof KNIMELogMessage) {
final KNIMELogMessage kmsg = (KNIMELogMessage) msg;
// can be null
final File msgDir = kmsg.getWorkflowDir();
if ((LOG_GLOBAL_IN_WF_DIR && msgDir == null) || LOG_IN_WF_DIR && workflowDir.equals(msgDir)) {
// return only neutral to let the log level based filters decide if we log this event
if (mainFilter != null) {
return mainFilter.decide(event);
}
return Filter.NEUTRAL;
}
}
return Filter.DENY;
}
});
// we have to call this function to activate the writer!!!
fileAppender.activateOptions();
logger.addAppender(fileAppender);
WF_APPENDER.put(workflowDirPath, fileAppender);
if (m_listener == null) {
m_listener = new MyWorkflowListener();
WorkflowManager.ROOT.addListener(m_listener);
}
}
}
}
}
use of org.apache.log4j.FileAppender in project scheduling by ow2-proactive.
the class TaskLogger method createFileAppender.
public File createFileAppender(File pathToFolder) throws IOException {
if (taskLogAppender.getAppender(FILE_APPENDER_NAME) != null) {
throw new IllegalStateException("Only one file appender can be created");
}
File logFile = new File(pathToFolder, new TaskLoggerRelativePathGenerator(taskId).getRelativePath());
logFile.getParentFile().mkdirs();
FileUtils.touch(logFile);
logFile.setWritable(true, false);
FileAppender fap = new FileAppender(Log4JTaskLogs.getTaskLogLayout(), logFile.getAbsolutePath(), false);
fap.setName(FILE_APPENDER_NAME);
taskLogAppender.addAppender(fap);
return logFile;
}
use of org.apache.log4j.FileAppender in project uavstack by uavorg.
the class Log4jHookProxy method figureoutLogConfiguration.
@SuppressWarnings("unchecked")
private void figureoutLogConfiguration(Logger Logger, LinkedList<LogProfileInfo> list, String appid) {
Enumeration<Appender> appenders = Logger.getAllAppenders();
while (appenders != null && appenders.hasMoreElements()) {
LogProfileInfo logProfileInfo = new LogProfileInfo();
if (appid != null) {
logProfileInfo.setAppId(appid);
}
logProfileInfo.setLogType(LogProfileInfo.LogType.Log4j);
Map<String, String> attributes = new HashMap<String, String>();
attributes.put(LogProfileInfo.ENGINE, "log4j");
Appender appender = appenders.nextElement();
if (!(appender instanceof FileAppender)) {
continue;
}
FileAppender fileAppender = (FileAppender) appender;
Layout layout = fileAppender.getLayout();
if (null != layout) {
if (layout instanceof PatternLayout) {
PatternLayout patternLayout = (PatternLayout) fileAppender.getLayout();
attributes.put(LogProfileInfo.PATTERN, patternLayout.getConversionPattern());
} else if (layout instanceof HTMLLayout) {
attributes.put(LogProfileInfo.PATTERN, "HTMLLayout");
} else if (layout instanceof SimpleLayout) {
attributes.put(LogProfileInfo.PATTERN, "SimpleLayout");
} else if (layout instanceof TTCCLayout) {
attributes.put(LogProfileInfo.PATTERN, "TTCCLayout");
}
}
boolean useBuffIO = fileAppender.getBufferedIO();
if (useBuffIO == true) {
attributes.put(LogProfileInfo.BUFFER_SIZE, String.valueOf(fileAppender.getBufferSize()));
attributes.put(LogProfileInfo.BUFFRT_IO, String.valueOf(useBuffIO));
}
logProfileInfo.setFilePath(fileAppender.getFile());
logProfileInfo.setAttributes(attributes);
list.add(logProfileInfo);
}
}
use of org.apache.log4j.FileAppender in project ignite by apache.
the class GridTestLog4jLogger method setNodeId.
/**
* {@inheritDoc}
*/
@Override
public void setNodeId(UUID nodeId) {
A.notNull(nodeId, "nodeId");
this.nodeId = nodeId;
for (FileAppender a : fileAppenders) {
if (a instanceof LoggerNodeIdAware) {
((LoggerNodeIdAware) a).setNodeId(nodeId);
a.activateOptions();
}
}
}
Aggregations