Skip to content

Commit

Permalink
[FLINK-31234][k8s] Add an option to redirect stdout/stderr to files
Browse files Browse the repository at this point in the history
This closes apache#22138.
  • Loading branch information
huwh authored Jun 29, 2023
1 parent d78d52b commit dfe6bda
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
<td>String</td>
<td>Additional command line options passed to SSH clients when starting or stopping JobManager, TaskManager, and Zookeeper services (start-cluster.sh, stop-cluster.sh, start-zookeeper-quorum.sh, stop-zookeeper-quorum.sh).</td>
</tr>
<tr>
<td><h5>env.stdout-err.redirect-to-file</h5></td>
<td style="word-wrap: break-word;">false</td>
<td>Boolean</td>
<td>Whether redirect stdout and stderr to files when running foreground. If enabled, logs won't append the console too. Note that redirected files do not support rolling rotate.</td>
</tr>
<tr>
<td><h5>env.yarn.conf.dir</h5></td>
<td style="word-wrap: break-word;">(none)</td>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,20 @@ public static String[] mergeListsToArray(List<String> base, List<String> append)
.defaultValue(10)
.withDescription("The maximum number of old log files to keep.");

/**
* This option is here only for documentation generation, it is only evaluated in the shell
* scripts.
*/
@SuppressWarnings("unused")
public static final ConfigOption<Boolean> FLINK_STD_REDIRECT_TO_FILE =
ConfigOptions.key("env.stdout-err.redirect-to-file")
.booleanType()
.defaultValue(false)
.withDescription(
"Whether redirect stdout and stderr to files when running foreground. "
+ "If enabled, logs won't append the console too. "
+ "Note that redirected files do not support rolling rotate.");

/**
* This options is here only for documentation generation, it is only evaluated in the shell
* scripts.
Expand Down
5 changes: 5 additions & 0 deletions flink-dist/src/main/flink-bin/bin/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ KEY_TASKM_COMPUTE_NUMA="taskmanager.compute.numa"
KEY_ENV_PID_DIR="env.pid.dir"
KEY_ENV_LOG_DIR="env.log.dir"
KEY_ENV_LOG_MAX="env.log.max"
KEY_ENV_STD_REDIRECT_TO_FILE="env.stdout-err.redirect-to-file"
KEY_ENV_YARN_CONF_DIR="env.yarn.conf.dir"
KEY_ENV_HADOOP_CONF_DIR="env.hadoop.conf.dir"
KEY_ENV_HBASE_CONF_DIR="env.hbase.conf.dir"
Expand Down Expand Up @@ -291,6 +292,10 @@ if [ -z "${MAX_LOG_FILE_NUMBER}" ]; then
export MAX_LOG_FILE_NUMBER
fi

if [ -z "${STD_REDIRECT_TO_FILE}" ]; then
STD_REDIRECT_TO_FILE=$(readFromConfig ${KEY_ENV_STD_REDIRECT_TO_FILE} "false" "${YAML_CONF}")
fi

if [ -z "${FLINK_LOG_DIR}" ]; then
FLINK_LOG_DIR=$(readFromConfig ${KEY_ENV_LOG_DIR} "${DEFAULT_FLINK_LOG_DIR}" "${YAML_CONF}")
fi
Expand Down
9 changes: 9 additions & 0 deletions flink-dist/src/main/flink-bin/bin/flink-console.sh
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ id=$([ -f "$pid" ] && echo $(wc -l < "$pid") || echo "0")

FLINK_LOG_PREFIX="${FLINK_LOG_DIR}/flink-${FLINK_IDENT_STRING}-${SERVICE}-${id}-${HOSTNAME}"
log="${FLINK_LOG_PREFIX}.log"
out="${FLINK_LOG_PREFIX}.out"
err="${FLINK_LOG_PREFIX}.err"

log_setting=("-Dlog.file=${log}" "-Dlog4j.configuration=file:${FLINK_CONF_DIR}/log4j-console.properties" "-Dlog4j.configurationFile=file:${FLINK_CONF_DIR}/log4j-console.properties" "-Dlogback.configurationFile=file:${FLINK_CONF_DIR}/logback-console.xml")

Expand All @@ -116,4 +118,11 @@ echo $$ >> "$pid" 2>/dev/null
# Evaluate user options for local variable expansion
FLINK_ENV_JAVA_OPTS=$(eval echo ${FLINK_ENV_JAVA_OPTS})

if [ "${STD_REDIRECT_TO_FILE}" == "true" ]; then
# disable console appender to avoid redundant logs in out file
log_setting=("-Dconsole.log.level=OFF" "${log_setting[@]}")
exec 1>"${out}"
exec 2>"${err}"
fi

exec "$JAVA_RUN" $JVM_ARGS ${FLINK_ENV_JAVA_OPTS} "${log_setting[@]}" -classpath "`manglePathList "$FLINK_TM_CLASSPATH:$SQL_GATEWAY_CLASSPATH:$INTERNAL_HADOOP_CLASSPATHS"`" ${CLASS_TO_RUN} "${ARGS[@]}"
2 changes: 2 additions & 0 deletions flink-dist/src/main/flink-bin/conf/log4j-console.properties
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ appender.console.name = ConsoleAppender
appender.console.type = CONSOLE
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{yyyy-MM-dd HH:mm:ss,SSS} %-5p %-60c %x - %m%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = ${sys:console.log.level:-ALL}

# Log all infos in the given rolling file
appender.rolling.name = RollingFileAppender
Expand Down
3 changes: 3 additions & 0 deletions flink-dist/src/main/flink-bin/conf/logback-console.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{60} %X{sourceThread} - %msg%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${console.log.level:-ALL}</level>
</filter>
</appender>

<appender name="rolling" class="ch.qos.logback.core.rolling.RollingFileAppender">
Expand Down

0 comments on commit dfe6bda

Please sign in to comment.