use of ch.qos.logback.classic.joran.JoranConfigurator in project spring-boot by spring-projects.
the class LogbackConfigurationTests method consolePatternCanBeOverridden.
@Test
public void consolePatternCanBeOverridden() throws JoranException {
JoranConfigurator configurator = new JoranConfigurator();
LoggerContext context = new LoggerContext();
configurator.setContext(context);
configurator.doConfigure(new File("src/test/resources/custom-console-log-pattern.xml"));
Appender<ILoggingEvent> appender = context.getLogger("ROOT").getAppender("CONSOLE");
assertThat(appender).isInstanceOf(ConsoleAppender.class);
Encoder<?> encoder = ((ConsoleAppender<?>) appender).getEncoder();
assertThat(encoder).isInstanceOf(PatternLayoutEncoder.class);
assertThat(((PatternLayoutEncoder) encoder).getPattern()).isEqualTo("foo");
}
use of ch.qos.logback.classic.joran.JoranConfigurator in project chassis by Kixeye.
the class LoggingConfiguration method reloadLogging.
/**
* Reloads logging.
*
* @param logbackConfig XML containing the logback configuration
*/
public void reloadLogging(String logbackConfig) {
LoggerContext logContext = (LoggerContext) LoggerFactory.getILoggerFactory();
try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(logContext);
logContext.reset();
configurator.doConfigure(new ByteArrayInputStream(logbackConfig.getBytes(Charsets.UTF_8)));
} catch (JoranException je) {
// StatusPrinter will handle this
} catch (Exception ex) {
// Just in case, so we see a stacktrace
ex.printStackTrace();
}
StatusPrinter.printInCaseOfErrorsOrWarnings(logContext);
applicationContext.publishEvent(new LoggingReloadedApplicationEvent(this));
}
use of ch.qos.logback.classic.joran.JoranConfigurator in project perun by CESNET.
the class PerunLogbackConfigurator method configure.
@Override
public void configure(LoggerContext loggerContext) {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
String confDir = System.getProperty("perun.conf.custom", "/etc/perun/");
File confFile = Paths.get(confDir, "logback.xml").toFile();
if (confFile.exists()) {
System.out.println("Loading logback config file " + confFile);
try {
// loads logback file
configurator.doConfigure(confFile.toString());
} catch (JoranException e) {
e.printStackTrace();
}
} else {
System.out.println("Loading logback-default.xml file from classpath");
try (InputStream configStream = this.getClass().getResourceAsStream("/logback-default.xml")) {
// loads logback file
configurator.doConfigure(configStream);
configStream.close();
} catch (IOException | JoranException e) {
e.printStackTrace();
System.out.println("Falling back to logback basic configurator");
BasicConfigurator basicConfigurator = new BasicConfigurator();
basicConfigurator.setContext(loggerContext);
basicConfigurator.configure(loggerContext);
}
}
}
use of ch.qos.logback.classic.joran.JoranConfigurator in project sling by apache.
the class LogbackManager method configure.
private void configure(ConfiguratorCallback cb) {
long startTime = System.currentTimeMillis();
StatusListener statusListener = new StatusListenerAsList();
if (debug) {
statusListener = new OnConsoleStatusListener();
}
getStatusManager().add(statusListener);
addInfo("Resetting context: " + getLoggerContext().getName());
resetContext(statusListener);
StatusUtil statusUtil = new StatusUtil(getLoggerContext());
JoranConfigurator configurator = createConfigurator();
final List<SaxEvent> eventList = configurator.recallSafeConfiguration();
final long threshold = System.currentTimeMillis();
boolean success = false;
try {
cb.perform(configurator);
if (statusUtil.hasXMLParsingErrors(threshold)) {
cb.fallbackConfiguration(eventList, createConfigurator(), statusListener);
}
addInfo("Context: " + getLoggerContext().getName() + " reloaded.");
success = true;
} catch (Throwable t) {
//Need to catch any error as Logback must work in all scenarios
//The error would be dumped to sysout in later call to Status printer
addError("Error occurred while configuring Logback", t);
} finally {
if (!success) {
cb.fallbackConfiguration(eventList, createConfigurator(), statusListener);
}
getStatusManager().remove(statusListener);
SlingStatusPrinter.printInCaseOfErrorsOrWarnings(getLoggerContext(), resetStartTime, startTime, success);
}
}
use of ch.qos.logback.classic.joran.JoranConfigurator in project midpoint by Evolveum.
the class StartupConfiguration method setupInitialLogging.
private void setupInitialLogging(File midpointHome) {
File logbackConfigFile = new File(midpointHome, LOGBACK_CONFIG_FILENAME);
boolean clear = false;
if (logbackConfigFile.exists()) {
clear = true;
} else {
logbackConfigFile = new File(midpointHome, LOGBACK_EXTRA_CONFIG_FILENAME);
if (!logbackConfigFile.exists()) {
return;
}
}
LOGGER.info("Loading logging configuration from {} ({})", logbackConfigFile, clear ? "clearing default configuration" : "extending defalt configuration");
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
if (clear) {
context.reset();
}
try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
configurator.doConfigure(logbackConfigFile);
} catch (Exception e) {
// This will logged by defalt logging configuration
LOGGER.error("Error loading additional logging configuration: {}", e.getMessage(), e);
// If normal logging fail make sure it is logged by web container
e.printStackTrace();
}
StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}
Aggregations