Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.yammer.dropwizard.config;
  
 
 import java.util.Map;
 
 import static com.yammer.dropwizard.config.LoggingConfiguration.*;
 
 // TODO: 11/7/11 <coda> -- document LoggingFactory
 // TODO: 11/7/11 <coda> -- test LoggingFactory
 
 public class LoggingFactory {
     public static void bootstrap() {
         // initially configure for WARN+ console logging
         final ConsoleConfiguration console = new ConsoleConfiguration();
         console.setEnabled(true);
         console.setTimeZone(TimeZone.getDefault());
         console.setThreshold(.);
 
         final Logger root = getCleanRoot();
         root.addAppender(LogbackFactory.buildConsoleAppender(console,
                                                              root.getLoggerContext(),
                                                              Optional.<String>absent()));
     }
 
     private final LoggingConfiguration config;
     private final String name;
 
     public LoggingFactory(LoggingConfiguration configString name) {
         this. = config;
         this. = name;
     }
 
     public void configure() {
         hijackJDKLogging();
 
         final Logger root = configureLevels();
 
         final ConsoleConfiguration console = .getConsoleConfiguration();
         if (console.isEnabled()) {
             root.addAppender(AsyncAppender.wrap(LogbackFactory.buildConsoleAppender(console,
                                                                                     root.getLoggerContext(),
                                                                                     console.getLogFormat())));
         }
 
         final FileConfiguration file = .getFileConfiguration();
         if (file.isEnabled()) {
             root.addAppender(AsyncAppender.wrap(LogbackFactory.buildFileAppender(file,
                                                                                  root.getLoggerContext(),
                                                                                  file.getLogFormat())));
         }
 
         final SyslogConfiguration syslog = .getSyslogConfiguration();
         if (syslog.isEnabled()) {
             root.addAppender(AsyncAppender.wrap(LogbackFactory.buildSyslogAppender(syslog,
                                                                                    root.getLoggerContext(),
                                                                                    ,
                                                                                    syslog.getLogFormat())));
         }
 
 
 
         final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
         try {
             final ObjectName objectName = new ObjectName("com.yammer:type=Logging");
             if (!server.isRegistered(objectName)) {
                 server.registerMBean(new JMXConfigurator(root.getLoggerContext(),
                                                          server,
                                                          objectName),
                                      objectName);
             }
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
 
         configureInstrumentation(root);
     }
 
     private void configureInstrumentation(Logger root) {
         final InstrumentedAppender appender = new InstrumentedAppender();
         appender.setContext(root.getLoggerContext());
         appender.start();
         root.addAppender(appender);
     }
 
     private void hijackJDKLogging() {
        SLF4JBridgeHandler.removeHandlersForRootLogger();
        SLF4JBridgeHandler.install();
    }
    private Logger configureLevels() {
        final Logger root = (Logger) LoggerFactory.getLogger(...);
        root.getLoggerContext().reset();
        final LevelChangePropagator propagator = new LevelChangePropagator();
        propagator.setContext(root.getLoggerContext());
        propagator.setResetJUL(true);
        root.getLoggerContext().addListener(propagator);
        root.setLevel(.getLevel());
        for (Map.Entry<StringLevelentry : .getLoggers().entrySet()) {
            ((Logger) LoggerFactory.getLogger(entry.getKey())).setLevel(entry.getValue());
        }
        return root;
    }
    private static Logger getCleanRoot() {
        final Logger root = (Logger) LoggerFactory.getLogger(...);
        root.detachAndStopAllAppenders();
        return root;
    }
New to GrepCode? Check out our FAQ X