use of org.slf4j.Logger in project sling by apache.
the class LogSupport method getLogger.
// ---------- Effective logging --------------------------------------------
/**
* Get a logger for messages orginating from the given bundle. If no bundle
* is specified, we use the system bundle logger.
*
* @param bundle The bundle for which a logger is to be returned.
* @return The Logger for the bundle.
*/
private Logger getLogger(Bundle bundle) {
Long bundleId = new Long((bundle == null) ? 0 : bundle.getBundleId());
Logger log;
synchronized (loggers) {
log = loggers.get(bundleId);
}
if (log == null) {
String name;
if (bundle == null) {
// if we have no bundle, use the system bundle's name
name = Constants.SYSTEM_BUNDLE_SYMBOLICNAME;
} else {
// otherwise use the bundle symbolic name
name = bundle.getSymbolicName();
// if the bundle has no symbolic name, use the location
if (name == null) {
name = bundle.getLocation();
}
// if the bundle also has no location, use the bundle Id
if (name == null) {
name = String.valueOf(bundle.getBundleId());
}
}
log = LoggerFactory.getLogger(name);
synchronized (loggers) {
loggers.put(bundleId, log);
}
}
return log;
}
use of org.slf4j.Logger in project sling by apache.
the class LogSupportTest method prepare.
@Before
@SuppressWarnings("unchecked")
public void prepare() throws Exception {
bundle = Mockito.mock(Bundle.class);
Mockito.when(bundle.getSymbolicName()).thenReturn("foo.bundle");
Mockito.when(bundle.getBundleId()).thenReturn(42L);
StartLevel startLevel = Mockito.mock(StartLevel.class);
logSupport = new LogSupport(startLevel);
Field loggerField = LogSupport.class.getDeclaredField("loggers");
loggerField.setAccessible(true);
Map<Long, Logger> loggers = (Map<Long, Logger>) loggerField.get(logSupport);
testLogger = getMockInfoLogger();
loggers.put(bundle.getBundleId(), testLogger);
}
use of org.slf4j.Logger in project sling by apache.
the class ITConfigAdminSupport method testChangeLogLevelWithConfig.
@Test
public void testChangeLogLevelWithConfig() throws Exception {
// Set log level to debug for foo1.bar
Configuration config = ca.createFactoryConfiguration(FACTORY_PID_CONFIGS, null);
Dictionary<String, Object> p = new Hashtable<String, Object>();
p.put(LOG_LOGGERS, new String[] { "foo1.bar" });
p.put(LOG_LEVEL, "DEBUG");
config.update(p);
delay();
Logger slf4jLogger = LoggerFactory.getLogger("foo1.bar");
assertTrue(slf4jLogger.isDebugEnabled());
assertTrue(LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).isInfoEnabled());
assertFalse(LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).isDebugEnabled());
// foo1.bar should not have explicit appender attached with it
Iterator<Appender<ILoggingEvent>> itr = ((ch.qos.logback.classic.Logger) slf4jLogger).iteratorForAppenders();
assertFalse(itr.hasNext());
}
use of org.slf4j.Logger in project sling by apache.
the class QuartzJobExecutor method execute.
/**
* @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
*/
@Override
public void execute(final JobExecutionContext context) throws JobExecutionException {
final JobDataMap data = context.getJobDetail().getJobDataMap();
final JobDesc desc = new JobDesc(data);
final Logger logger = (Logger) data.get(QuartzScheduler.DATA_MAP_LOGGER);
// check run on information
if (!shouldRun(logger, desc)) {
return;
}
String origThreadName = Thread.currentThread().getName();
try {
Thread.currentThread().setName(origThreadName + "-" + desc.name);
logger.debug("Executing job {}", desc);
if (desc.job instanceof org.apache.sling.commons.scheduler.Job) {
@SuppressWarnings("unchecked") final Map<String, Serializable> configuration = (Map<String, Serializable>) data.get(QuartzScheduler.DATA_MAP_CONFIGURATION);
final JobContext jobCtx = new JobContextImpl(desc.name, configuration);
((org.apache.sling.commons.scheduler.Job) desc.job).execute(jobCtx);
} else if (desc.job instanceof Runnable) {
((Runnable) desc.job).run();
} else {
logger.error("Scheduled job {} is neither a job nor a runnable: {}", desc);
}
} catch (final Throwable t) {
// if this is a quartz exception, rethrow it
if (t instanceof JobExecutionException) {
throw (JobExecutionException) t;
}
// there is nothing we can do here, so we just log
logger.error("Exception during job execution of " + desc + " : " + t.getMessage(), t);
} finally {
Thread.currentThread().setName(origThreadName);
}
}
use of org.slf4j.Logger in project sling by apache.
the class OakVirtualInstanceBuilder method createViewChecker.
@Override
protected ViewChecker createViewChecker() throws Exception {
getOakViewChecker();
return new ViewChecker() {
private final Logger logger = LoggerFactory.getLogger(getClass());
private SimulatedLease lease = new SimulatedLease(getResourceResolverFactory(), leaseCollection, getSlingId());
protected void activate(ComponentContext c) throws Throwable {
OakViewChecker pinger = getOakViewChecker();
PrivateAccessor.invoke(pinger, "activate", new Class[] { ComponentContext.class }, new Object[] { c });
}
@Override
public void checkView() {
try {
lease.updateDescriptor(getConfig());
} catch (Exception e) {
logger.error("run: could not update lease: " + e);
}
}
public void run() {
heartbeatAndCheckView();
}
@Override
public void heartbeatAndCheckView() {
// as soon as I see others the descriptor is updated
try {
lease.updateLeaseAndDescriptor(getConfig());
} catch (Exception e) {
logger.error("run: could not update lease: " + e, e);
}
try {
getOakViewChecker().run();
} catch (Exception e) {
logger.error("run: could not ping: " + e, e);
}
if (!getIdMapService().isInitialized()) {
if (!getIdMapService().waitForInit(1500)) {
fail("init didnt work");
}
}
}
};
}
Aggregations