DataflowWorkerLoggingInitializer { public static synchronized void configure(DataflowWorkerLoggingOptions options) { if (!initialized) { throw new RuntimeException("configure() called before initialize()"); } if (options.getDefaultWorkerLogLevel() != null) { Level defaultLevel = getJulLevel(options.getDefaultWorkerLogLevel()); LogManager.getLogManager().getLogger(ROOT_LOGGER_NAME).setLevel(defaultLevel); } if (options.getWorkerLogLevelOverrides() != null) { for (Map.Entry<String, DataflowWorkerLoggingOptions.Level> loggerOverride : options.getWorkerLogLevelOverrides().entrySet()) { Logger logger = Logger.getLogger(loggerOverride.getKey()); logger.setLevel(getJulLevel(loggerOverride.getValue())); configuredLoggers.add(logger); } } if (options.getWorkerSystemOutMessageLevel() != null) { System.out.close(); System.setOut( JulHandlerPrintStreamAdapterFactory.create( loggingHandler, SYSTEM_OUT_LOG_NAME, getJulLevel(options.getWorkerSystemOutMessageLevel()))); } if (options.getWorkerSystemErrMessageLevel() != null) { System.err.close(); System.setErr( JulHandlerPrintStreamAdapterFactory.create( loggingHandler, SYSTEM_ERR_LOG_NAME, getJulLevel(options.getWorkerSystemErrMessageLevel()))); } }  static PrintStream getOriginalStdErr(); static synchronized void initialize(); static synchronized void configure(DataflowWorkerLoggingOptions options); static DataflowWorkerLoggingHandler getLoggingHandler(); static DataflowWorkerLoggingHandler getSdkLoggingHandler(); @VisibleForTesting static synchronized void reset(); @VisibleForTesting
  static final String RUNNER_FILEPATH_PROPERTY; }
@Test public void testWithDefaults() { DataflowWorkerLoggingOptions options = PipelineOptionsFactory.as(DataflowWorkerLoggingOptions.class); DataflowWorkerLoggingInitializer.configure(options); Logger rootLogger = LogManager.getLogManager().getLogger(""); assertEquals(1, rootLogger.getHandlers().length); assertEquals(Level.INFO, rootLogger.getLevel()); assertIsDataflowWorkerLoggingHandler(rootLogger.getHandlers()[0], Level.ALL); }
@Test public void testWithConfigurationOverride() { DataflowWorkerLoggingOptions options = PipelineOptionsFactory.as(DataflowWorkerLoggingOptions.class); options.setDefaultWorkerLogLevel(DataflowWorkerLoggingOptions.Level.WARN); DataflowWorkerLoggingInitializer.configure(options); Logger rootLogger = LogManager.getLogManager().getLogger(""); assertEquals(1, rootLogger.getHandlers().length); assertEquals(Level.WARNING, rootLogger.getLevel()); assertIsDataflowWorkerLoggingHandler(rootLogger.getHandlers()[0], Level.ALL); }
@Test public void testWithCustomLogLevels() { DataflowWorkerLoggingOptions options = PipelineOptionsFactory.as(DataflowWorkerLoggingOptions.class); options.setWorkerLogLevelOverrides( new WorkerLogLevelOverrides() .addOverrideForName("A", DataflowWorkerLoggingOptions.Level.DEBUG) .addOverrideForName("B", DataflowWorkerLoggingOptions.Level.ERROR)); DataflowWorkerLoggingInitializer.configure(options); Logger aLogger = LogManager.getLogManager().getLogger("A"); assertEquals(0, aLogger.getHandlers().length); assertEquals(Level.FINE, aLogger.getLevel()); assertTrue(aLogger.getUseParentHandlers()); Logger bLogger = LogManager.getLogManager().getLogger("B"); assertEquals(Level.SEVERE, bLogger.getLevel()); assertEquals(0, bLogger.getHandlers().length); assertTrue(aLogger.getUseParentHandlers()); }
@Test public void testStrictGlobalFilterAndRelaxedOverride() throws IOException { DataflowWorkerLoggingOptions options = PipelineOptionsFactory.as(DataflowWorkerLoggingOptions.class); options.setDefaultWorkerLogLevel(DataflowWorkerLoggingOptions.Level.ERROR); options.setWorkerLogLevelOverrides( new WorkerLogLevelOverrides() .addOverrideForName("A", DataflowWorkerLoggingOptions.Level.INFO)); DataflowWorkerLoggingInitializer.configure(options); LogManager.getLogManager().getLogger("A").info("foobar"); verifyLogOutput("foobar"); }
@Test public void testSystemOutRespectsFilterConfig() throws IOException { DataflowWorkerLoggingOptions options = PipelineOptionsFactory.as(DataflowWorkerLoggingOptions.class); options.setDefaultWorkerLogLevel(DataflowWorkerLoggingOptions.Level.ERROR); DataflowWorkerLoggingInitializer.configure(options); System.out.println("sys.out"); System.err.println("sys.err"); List<String> actualLines = retrieveLogLines(); assertThat(actualLines, not(hasItem(containsString("sys.out")))); assertThat(actualLines, hasItem(containsString("sys.err"))); }
@Test public void testSystemOutLevelOverrides() throws IOException { DataflowWorkerLoggingOptions options = PipelineOptionsFactory.as(DataflowWorkerLoggingOptions.class); options.setWorkerSystemOutMessageLevel(DataflowWorkerLoggingOptions.Level.WARN); DataflowWorkerLoggingInitializer.configure(options.as(DataflowWorkerLoggingOptions.class)); System.out.println("foobar"); verifyLogOutput("WARN"); }
@Test public void testSystemOutCustomLogLevel() throws IOException { DataflowWorkerLoggingOptions options = PipelineOptionsFactory.as(DataflowWorkerLoggingOptions.class); options.setWorkerLogLevelOverrides( new WorkerLogLevelOverrides() .addOverrideForName("System.out", DataflowWorkerLoggingOptions.Level.ERROR)); DataflowWorkerLoggingInitializer.configure(options); System.out.println("sys.out"); List<String> actualLines = retrieveLogLines(); assertThat(actualLines, not(hasItem(containsString("sys.out")))); }
@Test public void testSystemErrLevelOverrides() throws IOException { DataflowWorkerLoggingOptions options = PipelineOptionsFactory.as(DataflowWorkerLoggingOptions.class); options.setWorkerSystemErrMessageLevel(DataflowWorkerLoggingOptions.Level.WARN); DataflowWorkerLoggingInitializer.configure(options.as(DataflowWorkerLoggingOptions.class)); System.err.println("foobar"); verifyLogOutput("WARN"); }