use of ch.qos.logback.classic.Logger in project cdap by caskdata.
the class LogAppenderInitializer method setLogLevels.
public void setLogLevels(Map<String, String> logPairs) {
LoggerContext loggerContext = getLoggerContext();
if (loggerContext != null) {
for (Map.Entry<String, String> entry : logPairs.entrySet()) {
String loggerName = entry.getKey();
String logLevel = entry.getValue();
Logger logger = loggerContext.getLogger(loggerName);
LOG.info("Log level of {} changed from {} to {}", loggerName, logger.getLevel(), logLevel);
logger.setLevel(Level.toLevel(logLevel));
}
}
}
use of ch.qos.logback.classic.Logger in project cdap by caskdata.
the class LogAppenderInitializer method initialize.
@VisibleForTesting
public synchronized void initialize(String loggerName) {
LoggerContext loggerContext = getLoggerContext();
if (loggerContext != null) {
Logger logger = loggerContext.getLogger(loggerName);
// Check if the logger already contains the logAppender
if (Iterators.contains(logger.iteratorForAppenders(), logAppender)) {
LOG.warn("Log appender {} is already initialized.", logAppender.getName());
return;
}
logAppender.setContext(loggerContext);
LOG.info("Initializing log appender {}", logAppender.getName());
// Display any errors during initialization of log appender to console
StatusManager statusManager = loggerContext.getStatusManager();
OnConsoleStatusListener onConsoleListener = new OnConsoleStatusListener();
statusManager.add(onConsoleListener);
logAppender.start();
logger.addAppender(logAppender);
}
}
use of ch.qos.logback.classic.Logger in project cdap by caskdata.
the class Loggers method getEffectiveLogger.
/**
* Returns the effective {@link Logger} for the given logger name. An
* effective {@link Logger} is the most specific {@link Logger} in the logger
* hierarchy based on the given logger name, which is already defined in the given {@link LoggerContext}.
*/
public static Logger getEffectiveLogger(LoggerContext context, String loggerName) {
// If there is such logger, then just return the effective level
Logger logger = context.exists(loggerName);
if (logger != null) {
return logger;
}
// Otherwise, search through the logger hierarchy. We don't call loggerContext.getLogger to avoid
// caching of the logger inside the loggerContext, which there is no guarantee on the memory usage
// and there is no way to clean it up.
logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
int idx = LoggerNameUtil.getFirstSeparatorIndexOf(loggerName);
while (idx >= 0) {
Logger parentLogger = context.exists(loggerName.substring(0, idx));
if (parentLogger != null) {
logger = parentLogger;
}
idx = LoggerNameUtil.getSeparatorIndexOf(loggerName, idx + 1);
}
// Returns the most specific logger for the given logger name
return logger;
}
use of ch.qos.logback.classic.Logger in project dropwizard by dropwizard.
the class BootstrapLogging method bootstrap.
public static void bootstrap(Level level) {
LoggingUtil.hijackJDKLogging();
BOOTSTRAPPING_LOCK.lock();
try {
if (bootstrapped) {
return;
}
final Logger root = LoggingUtil.getLoggerContext().getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
root.detachAndStopAllAppenders();
final DropwizardLayout formatter = new DropwizardLayout(root.getLoggerContext(), TimeZone.getDefault());
formatter.start();
final ThresholdFilter filter = new ThresholdFilter();
filter.setLevel(level.toString());
filter.start();
final ConsoleAppender<ILoggingEvent> appender = new ConsoleAppender<>();
appender.addFilter(filter);
appender.setContext(root.getLoggerContext());
final LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = new LayoutWrappingEncoder<>();
layoutEncoder.setLayout(formatter);
appender.setEncoder(layoutEncoder);
appender.start();
root.addAppender(appender);
bootstrapped = true;
} finally {
BOOTSTRAPPING_LOCK.unlock();
}
}
use of ch.qos.logback.classic.Logger in project dropwizard by dropwizard.
the class DefaultLoggingFactory method configureLoggers.
private Logger configureLoggers(String name) {
final Logger root = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
loggerContext.reset();
final LevelChangePropagator propagator = new LevelChangePropagator();
propagator.setContext(loggerContext);
propagator.setResetJUL(true);
loggerContext.addListener(propagator);
root.setLevel(level);
final LevelFilterFactory<ILoggingEvent> levelFilterFactory = new ThresholdLevelFilterFactory();
final AsyncAppenderFactory<ILoggingEvent> asyncAppenderFactory = new AsyncLoggingEventAppenderFactory();
final LayoutFactory<ILoggingEvent> layoutFactory = new DropwizardLayoutFactory();
for (Map.Entry<String, JsonNode> entry : loggers.entrySet()) {
final Logger logger = loggerContext.getLogger(entry.getKey());
final JsonNode jsonNode = entry.getValue();
if (jsonNode.isTextual()) {
// Just a level as a string
logger.setLevel(Level.valueOf(jsonNode.asText()));
} else if (jsonNode.isObject()) {
// A level and an appender
final LoggerConfiguration configuration;
try {
configuration = Jackson.newObjectMapper().treeToValue(jsonNode, LoggerConfiguration.class);
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Wrong format of logger '" + entry.getKey() + "'", e);
}
logger.setLevel(configuration.getLevel());
logger.setAdditive(configuration.isAdditive());
for (AppenderFactory<ILoggingEvent> appender : configuration.getAppenders()) {
logger.addAppender(appender.build(loggerContext, name, layoutFactory, levelFilterFactory, asyncAppenderFactory));
}
} else {
throw new IllegalArgumentException("Unsupported format of logger '" + entry.getKey() + "'");
}
}
return root;
}
Aggregations