wenjin272 commented on code in PR #638:
URL: https://github.com/apache/flink-agents/pull/638#discussion_r3136096264


##########
api/src/main/java/org/apache/flink/agents/api/logger/EventLoggerConfig.java:
##########
@@ -142,7 +143,7 @@ public String toString() {
      * validation and sensible defaults.
      */
     public static final class Builder {
-        private String loggerType = "file"; // Default to file logger
+        private String loggerType = "slf4j"; // Default to SLF4J logger

Review Comment:
   I think we need to provide an enum class for LoggerType.



##########
runtime/src/main/java/org/apache/flink/agents/runtime/operator/ActionExecutionOperator.java:
##########
@@ -1128,6 +1128,7 @@ private EventLogger createEventLogger(AgentPlan 
agentPlan) {
         EventLoggerConfig.Builder loggerConfigBuilder = 
EventLoggerConfig.builder();
         String baseLogDir = agentPlan.getConfig().get(BASE_LOG_DIR);
         if (baseLogDir != null && !baseLogDir.trim().isEmpty()) {
+            loggerConfigBuilder.loggerType("file");

Review Comment:
   I think we should expose a configuration for the logger type, and set the 
default value to `LoggerType.SL4J`. There are currently two types of Loggers, 
and more may be added in the future.



##########
runtime/src/main/java/org/apache/flink/agents/runtime/eventlog/Slf4jEventLogger.java:
##########
@@ -0,0 +1,186 @@
+/*
+ * 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.flink.agents.runtime.eventlog;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.flink.agents.api.Event;
+import org.apache.flink.agents.api.EventContext;
+import org.apache.flink.agents.api.EventFilter;
+import org.apache.flink.agents.api.logger.EventLogger;
+import org.apache.flink.agents.api.logger.EventLoggerConfig;
+import org.apache.flink.agents.api.logger.EventLoggerOpenParams;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.FileAppender;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.apache.logging.log4j.core.layout.PatternLayout;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * An SLF4J-based event logger that outputs events through a dedicated SLF4J 
logger.
+ *
+ * <p>This logger writes event log records as JSON to a dedicated SLF4J logger 
named {@value
+ * #EVENT_LOGGER_NAME}. Events are automatically routed to a separate file in 
Flink's log directory,
+ * making them visible in Flink's Web UI "Logs" tab.
+ *
+ * <p>On {@link #open}, the logger automatically configures log4j2 to write 
event logs to a separate
+ * file (derived from Flink's {@code log.file} system property). No manual 
log4j2 configuration is
+ * required.
+ *
+ * <p>Unlike {@link FileEventLogger}, which creates a separate log file per 
subtask, this logger
+ * writes all events from a TaskManager to the same log destination. To 
distinguish events from
+ * different subtasks, each JSON record includes {@code jobId}, {@code 
taskName}, and {@code
+ * subtaskId} fields.
+ *
+ * <h3>Thread Safety</h3>
+ *
+ * <p>This class is <strong>thread-safe at the Flink subtask level</strong>, 
following the same
+ * guarantees as {@link FileEventLogger}. Each subtask instance gets its own 
logger instance with
+ * its own subtask context fields.
+ */
+public class Slf4jEventLogger implements EventLogger {
+    public static final String PRETTY_PRINT_PROPERTY_KEY = "prettyPrint";
+
+    /** Dedicated logger name for event log output. */
+    public static final String EVENT_LOGGER_NAME = 
"org.apache.flink.agents.EventLog";
+
+    private static final String EVENT_LOG_APPENDER_NAME = 
"FlinkAgentsEventLogAppender";
+
+    private static final Logger EVENT_LOG = 
LoggerFactory.getLogger(EVENT_LOGGER_NAME);
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
+    private final EventLoggerConfig config;
+    private final EventFilter eventFilter;
+    private boolean prettyPrint;
+    private String jobId;
+    private String taskName;
+    private int subtaskId;
+
+    public Slf4jEventLogger(EventLoggerConfig config) {
+        this.config = config;
+        this.eventFilter = config.getEventFilter();
+    }
+
+    @Override
+    public void open(EventLoggerOpenParams params) throws Exception {
+        prettyPrint =
+                (Boolean) 
config.getProperties().getOrDefault(PRETTY_PRINT_PROPERTY_KEY, true);

Review Comment:
   I think we should use `PRETTY_PRINT.getKey()` here. The same applies to 
`FileEventLogger`.



-- 
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]

Reply via email to