Example usage for java.util.logging Logger getHandlers

List of usage examples for java.util.logging Logger getHandlers

Introduction

In this page you can find the example usage for java.util.logging Logger getHandlers.

Prototype

public Handler[] getHandlers() 

Source Link

Document

Get the Handlers associated with this logger.

Usage

From source file:com.googlecode.arit.jul.HandlerResourceScanner.java

public void scanForResources(ResourceListener resourceEventListener) {
    Enumeration<String> loggerNames = logManager.getLoggerNames();
    while (loggerNames.hasMoreElements()) {
        Logger logger = logManager.getLogger(loggerNames.nextElement());
        // On some JREs, Logger instances may be garbage collected. In this case,
        // the enumeration returned by getLoggerNames may contain names of garbage
        // collected loggers, and getLogger will return null for these names.
        // This was observed with Sun JRE 1.6. Loggers are not garbage collectable
        // with Sun JRE 1.5, IBM JRE 1.5 and IBM JRE 1.6 (WAS 7.0).
        if (logger != null) {
            Handler[] handlers = logger.getHandlers();
            for (Handler handler : handlers) {
                resourceEventListener.onResourceFound(new HandleResource(handler, logger));
            }/*from   w  w  w  . j  ava 2 s . c  o m*/
        }
    }
}

From source file:com.googlecode.arit.jul.HandlerResourceScanner.java

public void clean(ClassLoader classLoader) {
    Enumeration<String> loggerNames = logManager.getLoggerNames();
    while (loggerNames.hasMoreElements()) {
        Logger logger = logManager.getLogger(loggerNames.nextElement());
        // On some JREs, Logger instances may be garbage collected. In this case,
        // the enumeration returned by getLoggerNames may contain names of garbage
        // collected loggers, and getLogger will return null for these names.
        // This was observed with Sun JRE 1.6. Loggers are not garbage collectable
        // with Sun JRE 1.5, IBM JRE 1.5 and IBM JRE 1.6 (WAS 7.0).
        if (logger != null) {
            Handler[] handlers = logger.getHandlers();
            for (Handler handler : handlers) {
                if (classLoader.equals(handler.getClass().getClassLoader())) {
                    logger.removeHandler(handler);
                    LOG.info("Removed JUL handler " + handler.getClass().getName());
                }//from w  ww .  ja v  a2 s.  c o m
            }
        }
    }
}

From source file:org.callimachusproject.repository.CalliRepository.java

private void setHandlerLevel(Logger logger, Level level) {
    if (logger.getParent() != null) {
        setHandlerLevel(logger.getParent(), level);
    }//from w w  w  . j  av  a  2  s .c  om
    Handler[] handlers = logger.getHandlers();
    if (handlers != null) {
        for (Handler handler : handlers) {
            if (handler.getLevel().intValue() > level.intValue()) {
                handler.setLevel(level);
            }
        }
    }
}

From source file:org.springframework.boot.context.logging.LoggingApplicationListenerTests.java

private boolean bridgeHandlerInstalled() {
    Logger rootLogger = LogManager.getLogManager().getLogger("");
    Handler[] handlers = rootLogger.getHandlers();
    for (Handler handler : handlers) {
        if (handler instanceof SLF4JBridgeHandler) {
            return true;
        }/*w  w w.  ja va  2  s.c  om*/
    }
    return false;
}

From source file:org.openqa.selenium.server.mock.MockPIFrameUnitTest.java

private LoggingOptions configureLogging() throws Exception {
    // SeleniumServer.setDebugMode(true);
    LoggingOptions configuration = new LoggingOptions();
    File target = new File("target");
    if (target.exists() && target.isDirectory()) {
        configuration.setLogOutFile(new File(target, "mockpiframe.log"));
    } else {//from  ww  w .j a  v a 2  s .  c  o m
        configuration.setLogOutFile(new File("mockpiframe.log"));
    }
    LoggingManager.configureLogging(configuration, false);
    Logger logger = Logger.getLogger("");
    for (Handler handler : logger.getHandlers()) {
        if (handler instanceof StdOutHandler) {
            handler.setFormatter(new TerseFormatter(true));
            break;
        }
    }
    return configuration;
}

From source file:com.yahoo.dba.perf.myperf.common.MyPerfContext.java

private void configureLogging() {
    Logger logger = Logger.getLogger("");
    try {//from  ww w . j a v a2s  . c  om
        logger.setLevel(Level.parse(getLogLevel()));
    } catch (Exception ex) {
        logger.setLevel(Level.INFO);
    }
    try {
        for (Handler h : logger.getHandlers()) {
            if (h instanceof java.util.logging.ConsoleHandler)
                h.setLevel(Level.SEVERE);
        }
        String logRoot = System.getProperty("logPath", ".");

        java.util.logging.FileHandler fileHandler = new java.util.logging.FileHandler(
                logRoot + File.separatorChar + getLogPath(), this.logFileSize, this.logFileCount);
        fileHandler.setLevel(logger.getLevel());
        fileHandler.setFormatter(new SimpleFormatter());
        logger.addHandler(fileHandler);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:org.ebayopensource.turmeric.plugins.maven.AbstractTurmericMojo.java

/**
 * Wraps the mojo execute with some behavioral lifecycle.
 *//* w  w  w .  j  ava  2 s. c  om*/
@Override
public final void execute() throws MojoExecutionException, MojoFailureException {
    getLog().debug("[execute]");
    getLog().info("Using turmeric-maven-plugin version " + getTurmericMavenPluginVersion());

    if (executeSkip()) {
        getLog().warn("Skipping execution");
        return;
    }

    if (verbose) {
        logDependencyDetails("Turmeric Maven Plugin", AbstractTurmericMojo.class, GROUPID_SELF,
                ARTIFACTID_SELF);
        logDependencyDetails("Codegen Tools", NonInteractiveCodeGen.class,
                "org.ebayopensource.turmeric.runtime", "soa-client");
        getLog().info("Verbose Mode: enabling java.util.logging");
        // Initialize java.util.logging (which is present in CodeGen classes)

        Logger root = Logger.getLogger("");

        // Remove old delegates
        for (Handler handler : root.getHandlers()) {
            getLog().info("Removing existing logging handler: " + handler.getClass().getName());
            root.removeHandler(handler);
        }

        // Add our delegate
        root.addHandler(new LogDelegateHandler(getLog()));
    }

    getLog().debug("[onValidateParameters]");
    onValidateParameters();

    // Test for need to generate
    getLog().debug("[needsGeneration]");
    if (needsGeneration() == false) {
        getLog().warn("No need to generate. skipping turmeric plugin execution.");
        return;
    }

    try {
        getLog().debug("[onRunSetup]");
        onRunSetup();

        // Attach directories to project even if skipping servicegen.
        // This is to be a good maven and m2eclipse citizen.
        getLog().debug("[onAttachGeneratedDirectories]");
        onAttachGeneratedDirectories();

        getLog().debug("[onRun]");
        onRun();
    } finally {
        getLog().debug("[onRunTearDown]");
        onRunTearDown();
    }
}

From source file:nl.strohalm.cyclos.utils.logging.LoggingHandler.java

/**
 * Closes all handlers for the given logger
 * @param logger//  ww w .j av  a  2s . c  o  m
 */
private void close(final Logger logger) {
    if (logger == null) {
        return;
    }
    for (final Handler handler : logger.getHandlers()) {
        try {
            handler.close();
        } catch (final Exception e) {
            LOG.warn("Error while closing log handler - Ignoring", e);
        }
    }
}

From source file:com.speed.ob.Obfuscator.java

public Obfuscator(final Config config) {
    transforms = new LinkedList<>();
    store = new ClassStore();
    this.config = config;
    //set up logging
    this.LOGGER = Logger.getLogger(this.getClass().getName());
    LOGGER.info("Ob2 is starting");
    String logLvl = config.get("Obfuscator.logging");
    String logDir = config.get("Obfuscator.log_dir");
    level = parseLevel(logLvl);//from   w w  w  .j  a v  a2  s  .  com
    LOGGER.info("Logger level set to " + level.getName());
    Logger topLevel = Logger.getLogger("");
    topLevel.setLevel(level);
    File logs = new File(logDir);
    if (!logs.exists()) {
        if (!logs.mkdir())
            Logger.getLogger(this.getClass().getName()).warning("Could not create logging directory");
    }
    try {
        if (logs.exists()) {
            fHandler = new FileHandler(logs.getAbsolutePath() + File.separator + "ob%g.log");
            topLevel.addHandler(fHandler);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    for (Handler handler : topLevel.getHandlers()) {
        handler.setLevel(level);
    }
    //populate transforms
    LOGGER.info("Configuring Ob");
    LOGGER.fine("Parsing config");
    if (config.getBoolean("Obfuscator.all_transforms")) {
        LOGGER.fine("Adding all transforms");
        transforms.add(ClassNameTransform.class);
    } else {
        if (config.getBoolean("Obfuscator.classname_obfuscation")) {
            LOGGER.fine("Adding class name transform");
            transforms.add(ClassNameTransform.class);
        }
        if (config.getBoolean("Obfuscator.controlflow_obfuscation")) {
            LOGGER.fine("Control flow obfuscation not added, transform does not exist");
        }
        if (config.getBoolean("Obfuscator.string_obfuscation")) {
            LOGGER.fine("String obfuscation not added, transform does not exist");

        }
        if (config.getBoolean("Obfuscator.fieldname_transforms")) {
            LOGGER.fine("Field name obfuscation not added, transform does not exist");

        }
        if (config.getBoolean("Obfuscator.methodname_transforms")) {
            LOGGER.fine("Method name obfuscation not added, transform does not exist");

        }
    }
    LOGGER.info("Loaded " + transforms.size() + " transforms");
    String inputFile = config.get("Obfuscator.input");
    LOGGER.fine("Checking input file(s) and output directory");
    String outFile = config.get("Obfuscator.out_dir");
    out = new File(outFile);
    if (inputFile == null || inputFile.isEmpty()) {
        LOGGER.severe("Input file not specified in config");
        throw new RuntimeException("Input file not specified");
    } else {
        in = new File(inputFile);
        if (!in.exists()) {
            LOGGER.severe("Input file not found");
            throw new RuntimeException("Input file not found");
        }
        LOGGER.fine("Attempting to initialise classes");
        if (in.isDirectory()) {
            try {
                store.init(in.listFiles(), false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (in.getName().endsWith(".class")) {
            try {
                store.init(new File[] { in }, false);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (in.getName().endsWith(".jar")) {
            try {
                JarInputStream in = new JarInputStream(new FileInputStream(this.in));
                store.init(in, out, this.in);
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        LOGGER.info("Loaded " + store.nodes().size() + " classes");
    }
    if (!out.exists()) {
        LOGGER.fine("Attempting to make output directory");
        if (!out.mkdir()) {
            LOGGER.severe("Could not make output directory");
            throw new RuntimeException("Could not create output dir: " + out.getAbsolutePath());
        }
    } else if (!out.isDirectory()) {
        LOGGER.severe("Output directory is a file");
        throw new RuntimeException(out.getName() + " is not a directory, cannot output there");
    } else {
        if (!out.canWrite()) {
            LOGGER.severe("Cannot write to output directory");
            throw new RuntimeException("Cannot write to output dir: " + out.getAbsolutePath());
        }
    }

}

From source file:es.upm.dit.gsi.sim.twitter.TwitterSimulation.java

/**
 * Constructor//  w w  w  .  j a  va2 s. c o m
 * 
 * @param seed
 */
public TwitterSimulation(long seed, List<String> topics) {
    super(seed);
    Logger globalLogger = logger.getParent();
    globalLogger.setLevel(level);
    for (Handler handler : globalLogger.getHandlers()) {
        handler.setLevel(level);
    }
    this.topicManager = new TopicManager();
    this.topicManager.setTopics(topics);
}