Java Code Examples for ch.qos.logback.classic.Logger#detachAndStopAllAppenders()

The following examples show how to use ch.qos.logback.classic.Logger#detachAndStopAllAppenders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogbackModule.java    From jweb-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected void configure() {
    Logger root = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
    root.detachAndStopAllAppenders();

    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    LogbackOptions options = options("log", LogbackOptions.class);
    if (options.dir == null) {
        root.addAppender(createConsoleAppender(lc, options));
        root.setLevel(Level.valueOf(options.level));
    } else {
        root.addAppender(createFileAppender(app().dir().resolve(options.dir), lc, options));
        root.setLevel(Level.valueOf(options.level));
    }

    for (String packageName : options.excludePackages) {
        Logger logger = (Logger) LoggerFactory.getLogger(packageName);
        logger.setLevel(Level.WARN);
    }
}
 
Example 2
Source File: LogbackContainer.java    From dubbo-2.6.5 with Apache License 2.0 5 votes vote down vote up
/**
 * Initializer logback
 *
 * @param file
 * @param level
 * @param maxHistory
 */
private void doInitializer(String file, String level, int maxHistory) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.detachAndStopAllAppenders();

    // appender
    RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
    fileAppender.setContext(loggerContext);
    fileAppender.setName("application");
    fileAppender.setFile(file);
    fileAppender.setAppend(true);

    // policy
    TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
    policy.setContext(loggerContext);
    policy.setMaxHistory(maxHistory);
    policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
    policy.setParent(fileAppender);
    policy.start();
    fileAppender.setRollingPolicy(policy);

    // encoder
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
    encoder.start();
    fileAppender.setEncoder(encoder);

    fileAppender.start();

    rootLogger.addAppender(fileAppender);
    rootLogger.setLevel(Level.toLevel(level));
    rootLogger.setAdditive(false);
}
 
Example 3
Source File: TestShootDontShoot.java    From ShootOFF with GNU General Public License v3.0 5 votes vote down vote up
@After
public void tearDown() {
	TextToSpeech.silence(false);
	TrainingExerciseBase.silence(false);
	System.setOut(originalOut);

	Logger rootLogger = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
	rootLogger.detachAndStopAllAppenders();
}
 
Example 4
Source File: TestDuelingTree.java    From ShootOFF with GNU General Public License v3.0 5 votes vote down vote up
@After
public void tearDown() {
	TextToSpeech.silence(false);
	TrainingExerciseBase.silence(false);
	System.setOut(originalOut);

	Logger rootLogger = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
	rootLogger.detachAndStopAllAppenders();
}
 
Example 5
Source File: TestISSFStandardPistol.java    From ShootOFF with GNU General Public License v3.0 5 votes vote down vote up
@After
public void tearDown() {
	TextToSpeech.silence(false);
	TrainingExerciseBase.silence(false);
	System.setOut(originalOut);

	issfExercise.destroy();

	Logger rootLogger = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
	rootLogger.detachAndStopAllAppenders();
}
 
Example 6
Source File: TestBouncingTargets.java    From ShootOFF with GNU General Public License v3.0 5 votes vote down vote up
@After
public void tearDown() {
	TextToSpeech.silence(false);
	TrainingExerciseBase.silence(false);
	System.setOut(originalOut);

	Logger rootLogger = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
	rootLogger.detachAndStopAllAppenders();
}
 
Example 7
Source File: ShotDetectionTestor.java    From ShootOFF with GNU General Public License v3.0 5 votes vote down vote up
@BeforeClass
public static void setUpBaseClass() {
	System.setProperty("shootoff.home", System.getProperty("user.dir"));
	Configuration.disableErrorReporting();
	TrainingExerciseBase.silence(true);

	nu.pattern.OpenCV.loadShared();

	// Disable all loggers because our output has gotten verbose
	// enough that Travis-CI kills us. Comment out the lines below
	// and ensure tests don't call Configuration.setDebugMode(false)
	// to get debug output for specific tests.
	Logger rootLogger = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
	rootLogger.detachAndStopAllAppenders();
}
 
Example 8
Source File: SingularityExecutorLogging.java    From Singularity with Apache License 2.0 5 votes vote down vote up
public void stopTaskLogger(String taskId, Logger logger) {
  LOG.info("Stopping task logger for {}", taskId);

  try {
    logger.info("Task finished, stopping logger");

    logger.detachAndStopAllAppenders();

    logger.getLoggerContext().stop();
  } catch (Throwable t) {
    LOG.error("While closing task logger for {}", taskId, t);
  }
}
 
Example 9
Source File: BaleenLogging.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Configure logging based on a list of builders provided to it. Injects the configured logging to
 * replace the default UIMA loggers, and also sets up metrics on the logging.
 *
 * @param builders The builders to use to configure the logging
 */
public void configure(List<BaleenLoggerBuilder> builders) {
  // Install JUL to SLF4J handling (JUL is default for UIMA)
  SLF4JBridgeHandler.removeHandlersForRootLogger();
  SLF4JBridgeHandler.install();

  // Configure Logback
  LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
  Logger rootLogger = context.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);

  // Install the level change propagator to reduce the impact of JUL logging
  context.addListener(new LevelChangePropagator());

  // Remove all the existing appenders
  rootLogger.detachAndStopAllAppenders();

  for (BaleenLoggerBuilder builder : builders) {
    PatternLayoutEncoder ple = new PatternLayoutEncoder();
    ple.setCharset(StandardCharsets.UTF_8);
    ple.setContext(context);
    ple.setPattern(builder.getPattern());
    ple.start();

    Appender<ILoggingEvent> appender = builder.build(context, ple);
    if (!appender.isStarted()) {
      appender.start();
    }

    rootLogger.addAppender(appender);
  }

  LOGGER.debug("Adding instrumented metrics for logging");
  // Add an instrumented appender so we get the information about logging
  // through metrics
  InstrumentedAppender instrumentedAppender =
      new InstrumentedAppender(MetricsFactory.getInstance().getRegistry());
  instrumentedAppender.setContext(context);
  instrumentedAppender.start();
  rootLogger.addAppender(instrumentedAppender);
}
 
Example 10
Source File: LogbackSlowQueryLogProvider.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
public SlowQueryLog get() {
    Logger logger = (Logger) LoggerFactory.getLogger("sor.slow-query");
    logger.setAdditive(false);
    LoggerContext context = logger.getLoggerContext();
    logger.detachAndStopAllAppenders();

    PatternLayout logPatternLayout = new PatternLayout();
    logPatternLayout.setPattern("%-5p [%d{ISO8601," + _config.getTimeZone().getID() + "}] %m\n");
    logPatternLayout.setContext(context);
    logPatternLayout.start();

    ConsoleAppenderFactory consoleAppenderFactory = _config.getConsoleAppenderFactory();
    if (consoleAppenderFactory != null) {
        // Console is usually used only in development.  Use a synchronous appender so console output doesn't get re-ordered.
        logger.addAppender(consoleAppenderFactory.build(context, EMODB_APPLICATION_NAME, logPatternLayout));
    }

    FileAppenderFactory fileAppenderFactory = _config.getFileAppenderFactory();
    if (fileAppenderFactory != null) {
        AsyncAppender fileAsyncAppender = new AsyncAppender();
        fileAsyncAppender.addAppender(fileAppenderFactory.build(context, EMODB_APPLICATION_NAME, logPatternLayout));
        fileAsyncAppender.start();
        logger.addAppender(fileAsyncAppender);
    }

    SyslogAppenderFactory syslogAppenderFactory = _config.getSyslogAppenderFactory();
    if (syslogAppenderFactory != null) {
        AsyncAppender sysAsyncAppender = new AsyncAppender();
        sysAsyncAppender.addAppender(syslogAppenderFactory.build(context, EMODB_APPLICATION_NAME, logPatternLayout));
        sysAsyncAppender.start();
        logger.addAppender(sysAsyncAppender);
    }

    return new LogbackSlowQueryLog(logger, _config.getTooManyDeltasThreshold(), _metricRegistry);
}
 
Example 11
Source File: LoggingConfig.java    From mirror with Apache License 2.0 5 votes vote down vote up
public synchronized static void init() {
  if (started) {
    return;
  }
  started = true;

  // setup java.util.logging (which grpc-java uses) to go to slf4j
  SLF4JBridgeHandler.removeHandlersForRootLogger();
  SLF4JBridgeHandler.install();

  LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();

  LevelChangePropagator p = new LevelChangePropagator();
  p.setContext(context);
  p.start();
  context.addListener(p);

  PatternLayoutEncoder encoder = new PatternLayoutEncoder();
  encoder.setContext(context);
  encoder.setPattern(pattern);
  encoder.start();

  ConsoleAppender<ILoggingEvent> console = new ConsoleAppender<>();
  console.setContext(context);
  console.setEncoder(encoder);
  console.start();

  Logger root = getLogger(Logger.ROOT_LOGGER_NAME);
  root.detachAndStopAllAppenders();
  root.addAppender(console);
  root.setLevel(Level.INFO);

  getLogger("io.grpc").setLevel(Level.INFO);
  // silence a noisy DNS warning when we cannot resolve the other host
  getLogger("io.grpc.internal.ManagedChannelImpl").setLevel(Level.ERROR);
  // silence "ConnectivityStateManager is already disabled" warning
  getLogger("io.grpc.internal.ChannelExecutor").setLevel(Level.ERROR);
  getLogger("mirror").setLevel(Level.INFO);
}
 
Example 12
Source File: LogbackContainer.java    From dubbox with Apache License 2.0 5 votes vote down vote up
/**
 * Initializer logback
 * 
 * @param file
 * @param level
 * @param maxHistory
 */
private void doInitializer(String file, String level, int maxHistory) {
	LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
	Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
	rootLogger.detachAndStopAllAppenders();

	// appender
	RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
	fileAppender.setContext(loggerContext);
	fileAppender.setName("application");
	fileAppender.setFile(file);
	fileAppender.setAppend(true);

	// policy
	TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
	policy.setContext(loggerContext);
	policy.setMaxHistory(maxHistory);
	policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
	policy.setParent(fileAppender);
	policy.start();
	fileAppender.setRollingPolicy(policy);

	// encoder
	PatternLayoutEncoder encoder = new PatternLayoutEncoder();
	encoder.setContext(loggerContext);
	encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
	encoder.start();
	fileAppender.setEncoder(encoder);
	
	fileAppender.start();

	rootLogger.addAppender(fileAppender);
	rootLogger.setLevel(Level.toLevel(level));
	rootLogger.setAdditive(false);
}
 
Example 13
Source File: LogbackContainer.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
/**
 * Initializer logback
 *
 * @param file       log file
 * @param level      log level
 * @param maxHistory max history
 */
private void doInitializer(String file, String level, int maxHistory) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.detachAndStopAllAppenders();

    // appender
    RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<>();
    fileAppender.setContext(loggerContext);
    fileAppender.setName("application");
    fileAppender.setFile(file);
    fileAppender.setAppend(true);

    // policy
    TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<>();
    policy.setContext(loggerContext);
    policy.setMaxHistory(maxHistory);
    policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
    policy.setParent(fileAppender);
    policy.start();
    fileAppender.setRollingPolicy(policy);

    // encoder
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
    encoder.start();
    fileAppender.setEncoder(encoder);

    fileAppender.start();

    rootLogger.addAppender(fileAppender);
    rootLogger.setLevel(Level.toLevel(level));
    rootLogger.setAdditive(false);
}
 
Example 14
Source File: LogbackContainer.java    From dubbox-hystrix with Apache License 2.0 5 votes vote down vote up
/**
 * Initializer logback
 * 
 * @param file
 * @param level
 * @param maxHistory
 */
private void doInitializer(String file, String level, int maxHistory) {
	LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
	Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
	rootLogger.detachAndStopAllAppenders();

	// appender
	RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
	fileAppender.setContext(loggerContext);
	fileAppender.setName("application");
	fileAppender.setFile(file);
	fileAppender.setAppend(true);

	// policy
	TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
	policy.setContext(loggerContext);
	policy.setMaxHistory(maxHistory);
	policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
	policy.setParent(fileAppender);
	policy.start();
	fileAppender.setRollingPolicy(policy);

	// encoder
	PatternLayoutEncoder encoder = new PatternLayoutEncoder();
	encoder.setContext(loggerContext);
	encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
	encoder.start();
	fileAppender.setEncoder(encoder);
	
	fileAppender.start();

	rootLogger.addAppender(fileAppender);
	rootLogger.setLevel(Level.toLevel(level));
	rootLogger.setAdditive(false);
}
 
Example 15
Source File: Cli.java    From bazel-tools with Apache License 2.0 5 votes vote down vote up
public static void configureLogging(final String toolName, final boolean verbose) {
  final LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
  final Logger logger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
  final CliAppender appender = CliAppender.create(toolName, verbose);
  appender.setContext(loggerContext);
  logger.detachAndStopAllAppenders();
  logger.addAppender(appender);
  appender.start();
}
 
Example 16
Source File: LogUtils.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the file appender with the name of "FILE" if needed. If it's already been setup,
 * it would be ignored.
 */
public static void setAppendersForRunCommand(String serverId) {
    String logFilePath = new File(DirectoryFetcher.getCommandLogsDir(), serverId + ".log").getAbsolutePath();

    SysOutOverSLF4J.sendSystemOutAndErrToSLF4J();

    Logger rootLogger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER);

    rootLogger.detachAndStopAllAppenders();

    ((Logger) LoggerFactory.getLogger("uk.org.lidalia")).setLevel(Level.INFO);
    ((Logger) LoggerFactory.getLogger("org.nd4j")).setLevel(Level.INFO);
    ((Logger) LoggerFactory.getLogger("org")).setLevel(Level.INFO);
    ((Logger) LoggerFactory.getLogger("io")).setLevel(Level.INFO);

    LoggerContext context = (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();

    FileAppender<ILoggingEvent> fileAppender = new FileAppender<>();
    fileAppender.setName("FILE");
    fileAppender.setFile(logFilePath);
    fileAppender.setAppend(false);
    fileAppender.setContext(context);

    PatternLayoutEncoder patternLayoutEncoder = new PatternLayoutEncoder();
    patternLayoutEncoder.setPattern("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n");
    patternLayoutEncoder.setContext(context);
    patternLayoutEncoder.start();

    fileAppender.setEncoder(patternLayoutEncoder);
    fileAppender.start();

    ConsoleAppender<ILoggingEvent> consoleAppender = new ConsoleAppender<>();
    consoleAppender.setName("CONSOLE");
    consoleAppender.setContext(context);
    consoleAppender.setEncoder(patternLayoutEncoder);
    consoleAppender.start();

    rootLogger.addAppender(fileAppender);
    rootLogger.addAppender(consoleAppender);

    log.info("Logging file at: {}", logFilePath);
}
 
Example 17
Source File: LoggingFactory.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
private static Logger getCleanRoot() {
    final Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    root.detachAndStopAllAppenders();
    return root;
}
 
Example 18
Source File: OfframpConsole.java    From data-highway with Apache License 2.0 4 votes vote down vote up
/**
 * Helper function to configure where message and CLI output is directed as well as configure logging
 * This should be called first to ensure correct output of message and cli logging into the desired stream.
 */
private void configureOutput() {
  try {
    if (flipOutput) {
      PrintStream tmp = msgout;
      msgout = cliout;
      cliout = tmp;
    }

    // change output format from json to yaml
    if (format == Format.YAML) {
      mapper = new YAMLMapper();
    }

    // retrieve the ch.qos.logback.classic.LoggerContext
    LoggerContext logCtx = (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();

    PatternLayoutEncoder logEncoder = new PatternLayoutEncoder();
    logEncoder.setContext(logCtx);
    logEncoder.setPattern("%d{HH:mm:ss.SSS} - %-5level %logger{35} - %msg%n");
    logEncoder.start();

    OutputStreamAppender logConsoleAppender = new OutputStreamAppender();
    logConsoleAppender.setContext(logCtx);
    logConsoleAppender.setOutputStream(cliout);
    logConsoleAppender.setName("console");
    logConsoleAppender.setEncoder(logEncoder);
    logConsoleAppender.start();

    Logger rootLogger = logCtx.getLogger("root");
    rootLogger.detachAndStopAllAppenders();
    rootLogger.setLevel(Level.WARN);
    rootLogger.addAppender(logConsoleAppender);

    Logger roadLogger = logCtx.getLogger("com.hotels.road");
    roadLogger.setLevel(Level.WARN);

    Logger clientLogger = logCtx.getLogger("com.hotels.road.offramp.client");
    clientLogger.setLevel(debug ? Level.DEBUG : Level.INFO);

    Logger cliLogger = logCtx.getLogger("com.hotels.road.tool.cli");
    cliLogger.setLevel(debug ? Level.DEBUG : Level.INFO);
  } catch (Exception e) {
    System.err.println("Error configuring the message and cli output:");
    e.printStackTrace();
    System.exit(Error.OUTPUT_CONFIGURATION.code);
  }
}
 
Example 19
Source File: LoggingFactory.java    From bither-desktop-java with Apache License 2.0 4 votes vote down vote up
private static Logger getCleanRoot() {
    final Logger root = (Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    root.detachAndStopAllAppenders();
    return root;
}
 
Example 20
Source File: SingularityRunnerBaseLogging.java    From Singularity with Apache License 2.0 3 votes vote down vote up
public Logger prepareRootLogger(LoggerContext context) {
  Logger rootLogger = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);

  rootLogger.detachAndStopAllAppenders();

  return rootLogger;
}