github-actions[bot] commented on code in PR #41460: URL: https://github.com/apache/beam/pull/41460#discussion_r4618994434
########## sdks/java/extensions/sql/jdbc/src/main/java/org/apache/beam/sdk/extensions/sql/jdbc/HeldLogs.java: ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.extensions.sql.jdbc; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.List; +import java.util.logging.ConsoleHandler; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +/** + * Holds back console logging while the live view is on screen, so log lines don't draw over it. + * + * <p>In the shell, pipelines log through java.util.logging, and its console handler writes to the + * same terminal as the view. While held, the root logger's console handlers are swapped for one + * that keeps warnings and errors and only counts the rest. Closing puts the console handlers back + * and passes them the warnings and errors that came in. + */ +final class HeldLogs implements AutoCloseable { + /** How many warnings and errors to keep. Older ones are counted with the rest. */ + private static final int MAX_HELD = 100; + + private final Logger root = Logger.getLogger(""); + private final List<Handler> consoles = new ArrayList<>(); + private final Deque<LogRecord> held = new ArrayDeque<>(); + private int hidden; + + private final Handler holder = + new Handler() { + @Override + public void publish(LogRecord record) { Review Comment: **🟡 Medium**: `new Handler()` defaults to `Level.ALL`, whereas `ConsoleHandler` defaults to `Level.INFO` (and may have custom levels or filters configured). Because `holder.publish` does not check `isLoggable(record)` against the replaced `ConsoleHandler`s, any `FINE`/`DEBUG` logs emitted by loggers with lower levels enabled (such as the `LOG.debug` calls in `WatchForSnapshotsSdf` when debug logging is enabled on a file handler or logger) will be counted toward `hidden` even though the console handlers would never have printed them. Checking that at least one replaced console handler would log the record before counting or buffering avoids inflating `hidden`. ########## sdks/java/extensions/sql/jdbc/src/main/java/org/apache/beam/sdk/extensions/sql/jdbc/HeldLogs.java: ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.extensions.sql.jdbc; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.List; +import java.util.logging.ConsoleHandler; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +/** + * Holds back console logging while the live view is on screen, so log lines don't draw over it. + * + * <p>In the shell, pipelines log through java.util.logging, and its console handler writes to the + * same terminal as the view. While held, the root logger's console handlers are swapped for one + * that keeps warnings and errors and only counts the rest. Closing puts the console handlers back + * and passes them the warnings and errors that came in. + */ +final class HeldLogs implements AutoCloseable { + /** How many warnings and errors to keep. Older ones are counted with the rest. */ + private static final int MAX_HELD = 100; + + private final Logger root = Logger.getLogger(""); + private final List<Handler> consoles = new ArrayList<>(); + private final Deque<LogRecord> held = new ArrayDeque<>(); + private int hidden; + + private final Handler holder = + new Handler() { + @Override + public void publish(LogRecord record) { + synchronized (held) { + if (record.getLevel().intValue() < Level.WARNING.intValue()) { + hidden++; + return; + } + held.addLast(record); Review Comment: **🟡 Medium**: `LogRecord` lazily infers its caller class and method name by walking the call stack the first time `getSourceClassName()` or `getSourceMethodName()` is invoked (typically inside the console handler's `Formatter` during `publish`). Because `record` is buffered here and only formatted later inside `HeldLogs.close()`, all replayed warnings and errors will report `HeldLogs.close` as their source class/method instead of the actual logging call site. Calling `record.getSourceClassName();` before buffering forces the caller frame to be captured on the logging thread. ########## sdks/java/extensions/sql/jdbc/src/main/java/org/apache/beam/sdk/extensions/sql/jdbc/LiveResultView.java: ########## @@ -98,6 +100,8 @@ public void show(LiveResult result) throws IOException { Runnable quit = () -> {}; long start = System.currentTimeMillis(); long end = 0; + // Log lines would draw over the view, so hold them back until it closes. + HeldLogs logs = HeldLogs.hold(); Review Comment: **🟢 Low**: Since `HeldLogs` implements `AutoCloseable`, it can be managed directly in the `try`-with-resources header (`try (HeldLogs logs = HeldLogs.hold(); Terminal terminal = terminals.open())`) so `terminal` closes first and `logs` closes automatically without a separate `finally` block. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
