Search in sources :

Example 76 with Logger

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;
}
Also used : Logger(org.slf4j.Logger)

Example 77 with Logger

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);
}
Also used : Field(java.lang.reflect.Field) Bundle(org.osgi.framework.Bundle) StartLevel(org.osgi.service.startlevel.StartLevel) Logger(org.slf4j.Logger) HashMap(java.util.HashMap) Map(java.util.Map) Before(org.junit.Before)

Example 78 with Logger

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());
}
Also used : Appender(ch.qos.logback.core.Appender) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 79 with Logger

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);
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Serializable(java.io.Serializable) Logger(org.slf4j.Logger) JobExecutionException(org.quartz.JobExecutionException) JobContext(org.apache.sling.commons.scheduler.JobContext) Job(org.quartz.Job) JobDataMap(org.quartz.JobDataMap) Map(java.util.Map)

Example 80 with Logger

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");
                }
            }
        }
    };
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) ViewChecker(org.apache.sling.discovery.base.commons.ViewChecker) OakViewChecker(org.apache.sling.discovery.oak.pinger.OakViewChecker) OakViewChecker(org.apache.sling.discovery.oak.pinger.OakViewChecker) Logger(org.slf4j.Logger)

Aggregations

Logger (org.slf4j.Logger)1088 Test (org.junit.Test)249 IOException (java.io.IOException)127 ENotificationImpl (org.eclipse.emf.ecore.impl.ENotificationImpl)110 ArrayList (java.util.ArrayList)71 InputStream (java.io.InputStream)64 List (java.util.List)59 File (java.io.File)56 Map (java.util.Map)51 LoggerFactory (org.slf4j.LoggerFactory)46 Test (org.testng.annotations.Test)43 HashMap (java.util.HashMap)39 Properties (java.util.Properties)35 HashSet (java.util.HashSet)31 FileInputStream (java.io.FileInputStream)29 Transfer (org.commonjava.maven.galley.model.Transfer)29 Set (java.util.Set)28 StoreKey (org.commonjava.indy.model.core.StoreKey)28 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)27 Date (java.util.Date)26