All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yammer.dropwizard.logging.LoggingBean Maven / Gradle / Ivy

There is a newer version: 0.6.2
Show newest version
package com.yammer.dropwizard.logging;

// TODO: 10/12/11  -- test LoggingBean
// TODO: 10/12/11  -- document LoggingBean

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import com.google.common.collect.Lists;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.logging.LogManager;
import java.util.logging.LoggingMXBean;

public class LoggingBean implements LoggingMXBean {
    @Override
    public String getLoggerLevel(String loggerName) {
        return Log.named(loggerName).getLevel().toString();
    }

    @Override
    public List getLoggerNames() {
        final Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);

        final List names = Lists.newArrayList();
        for (Logger logger : root.getLoggerContext().getLoggerList()) {
            names.add(logger.getName());
        }

        final Enumeration moreNames = LogManager.getLogManager()
                                                        .getLoggerNames();
        while (moreNames.hasMoreElements()) {
            final String name = moreNames.nextElement();
            names.add(name);
        }

        Collections.sort(names);
        return names;
    }

    @Override
    public void setLoggerLevel(String loggerName, String levelName) {
        final Level newLevel = Level.toLevel(levelName, Level.INFO);
        Log.named(loggerName).setLevel(newLevel);
    }

    @Override
    public String getParentLoggerName(String loggerName) {
        throw new UnsupportedOperationException("Can't determine parents.");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy