我在flink中使用log4j2,奇怪的是,logger没有在main方法中写入文件,但是在其他地方它可以很好地记录。
例子:
public class FlinkEntry {
private static final JedisCluster JEDIS_CLUSTER;
private static final Logger logger = LogManager.getLogger();
static {
Set<HostAndPort> hostAndPorts = new HashSet<>();
hostAndPorts.add(new HostAndPort("..", 7001));
JEDIS_CLUSTER = new JedisCluster(hostAndPorts);
// this line logs ok
logger.info(JEDIS_CLUSTER.getClusterNodes());
}
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<String> dataStream = environment.addSource(...);
SinkFunction<String> redisSink = createRedisSink();
dataStream.addSink(redisSink).name("redis");
// this line does not log anything
logger.info("start to execute");
environment.execute();
}
private static SinkFunction<String> createRedisSink() {
return new SinkFunction<String>() {
@Override
public void invoke(String value, Context context) {
// this line log well
logger.info("write to redis");
JEDIS_CLUSTER.lpush("rtp:example", value);
JEDIS_CLUSTER.expire("rtp:example", 10 * 60);
}
};
}
}
log4j2.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<RollingFile name="RootAppender" immediateFlush="true" fileName="/tmp/${artifactId}/logs/flinktest.log"
filePattern="/tmp/${artifactId}/logs/flink-%d{yyyy-MM-dd}.log">
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="RootAppender"/>
</Root>
</Loggers>
</Configuration>
暂无答案!
目前还没有任何答案,快来回答吧!