use of org.apache.ignite.logger.LoggerNodeIdAndApplicationAware in project ignite by apache.
the class Log4j2LoggerSelfTest method testUrlConstructor.
/**
* @throws Exception If failed.
*/
@Test
public void testUrlConstructor() throws Exception {
File xml = GridTestUtils.resolveIgnitePath(LOG_PATH_TEST);
assert xml != null;
assert xml.exists();
URL url = xml.toURI().toURL();
IgniteLogger log = new Log4J2Logger(url).getLogger(getClass());
System.out.println(log.toString());
assertTrue(log.toString().contains("Log4J2Logger"));
assertTrue(log.toString().contains(url.getPath()));
((LoggerNodeIdAndApplicationAware) log).setApplicationAndNode(null, UUID.randomUUID());
checkLog(log);
}
use of org.apache.ignite.logger.LoggerNodeIdAndApplicationAware in project ignite by apache.
the class Log4j2LoggerSelfTest method testPathConstructor.
/**
* @throws Exception If failed.
*/
@Test
public void testPathConstructor() throws Exception {
IgniteLogger log = new Log4J2Logger(LOG_PATH_TEST).getLogger(getClass());
System.out.println(log.toString());
assertTrue(log.toString().contains("Log4J2Logger"));
assertTrue(log.toString().contains(LOG_PATH_TEST));
((LoggerNodeIdAndApplicationAware) log).setApplicationAndNode(null, UUID.randomUUID());
checkLog(log);
}
use of org.apache.ignite.logger.LoggerNodeIdAndApplicationAware in project ignite by apache.
the class IgniteUtils method initLogger.
/**
* @param cfgLog Configured logger.
* @param app Application name.
* @param workDir Work directory.
* @return Initialized logger.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("ErrorNotRethrown")
public static IgniteLogger initLogger(@Nullable IgniteLogger cfgLog, @Nullable String app, UUID nodeId, String workDir) throws IgniteCheckedException {
try {
Exception log4jInitErr = null;
if (cfgLog == null) {
Class<?> log4jCls;
try {
log4jCls = Class.forName("org.apache.ignite.logger.log4j.Log4JLogger");
} catch (ClassNotFoundException | NoClassDefFoundError ignored) {
log4jCls = null;
}
if (log4jCls != null) {
try {
URL url = U.resolveIgniteUrl("config/ignite-log4j.xml");
if (url == null) {
File cfgFile = new File("config/ignite-log4j.xml");
if (!cfgFile.exists())
cfgFile = new File("../config/ignite-log4j.xml");
if (cfgFile.exists()) {
try {
url = cfgFile.toURI().toURL();
} catch (MalformedURLException ignore) {
// No-op.
}
}
}
if (url != null) {
boolean configured = (Boolean) log4jCls.getMethod("isConfigured").invoke(null);
if (configured)
url = null;
}
if (url != null) {
Constructor<?> ctor = log4jCls.getConstructor(URL.class);
cfgLog = (IgniteLogger) ctor.newInstance(url);
} else
cfgLog = (IgniteLogger) log4jCls.newInstance();
} catch (Exception e) {
log4jInitErr = e;
}
}
if (log4jCls == null || log4jInitErr != null)
cfgLog = new JavaLogger();
}
// Special handling for Java logger which requires work directory.
if (cfgLog instanceof JavaLogger)
((JavaLogger) cfgLog).setWorkDirectory(workDir);
// Set node IDs for all file appenders.
if (cfgLog instanceof LoggerNodeIdAndApplicationAware)
((LoggerNodeIdAndApplicationAware) cfgLog).setApplicationAndNode(app, nodeId);
else if (cfgLog instanceof LoggerNodeIdAware)
((LoggerNodeIdAware) cfgLog).setNodeId(nodeId);
if (log4jInitErr != null)
U.warn(cfgLog, "Failed to initialize Log4JLogger (falling back to standard java logging): " + log4jInitErr.getCause());
return cfgLog;
} catch (Exception e) {
throw new IgniteCheckedException("Failed to create logger.", e);
}
}
use of org.apache.ignite.logger.LoggerNodeIdAndApplicationAware in project ignite by apache.
the class GridTestLog4jLogger method setApplicationAndNode.
/**
* {@inheritDoc}
*/
@Override
public void setApplicationAndNode(@Nullable String application, UUID nodeId) {
A.notNull(nodeId, "nodeId");
this.nodeId = nodeId;
for (FileAppender a : fileAppenders) {
if (a instanceof LoggerNodeIdAndApplicationAware) {
((LoggerNodeIdAndApplicationAware) a).setApplicationAndNode(application, nodeId);
a.activateOptions();
}
}
}
use of org.apache.ignite.logger.LoggerNodeIdAndApplicationAware in project ignite by apache.
the class Log4j2LoggerSelfTest method testFileConstructor.
/**
* @throws Exception If failed.
*/
@Test
public void testFileConstructor() throws Exception {
File xml = GridTestUtils.resolveIgnitePath(LOG_PATH_TEST);
assert xml != null;
assert xml.exists();
IgniteLogger log = new Log4J2Logger(xml).getLogger(getClass());
System.out.println(log.toString());
assertTrue(log.toString().contains("Log4J2Logger"));
assertTrue(log.toString().contains(xml.getPath()));
((LoggerNodeIdAndApplicationAware) log).setApplicationAndNode(null, UUID.randomUUID());
checkLog(log);
}
Aggregations