我想在Flink中输出日志,我已经创建了如下所述的记录器代码
我已导入LoggerFactory
import org.slf4j.LoggerFactory;
CEPMonitoring类包含如下代码
final static org.slf4j.Logger logger = LoggerFactory.getLogger(CEPMonitoring.class);
public static void main(String[] args) throws Exception {
logger.info("execution began n ");
}
然而,这并没有打印任何内容。此外,我没有上面提到的logback.xml
文件https://ci.apache.org/projects/flink/flink-docs-release-0.7/internal_logging.html
p.S-我使用时出错
final static Logger logger = LoggerFactory.getLogger(CEPMonitoring.class);
这最终让我写了
final static org.slf4j.Logger logger = LoggerFactory.getLogger(CEPMonitoring.class);
它出现了一个错误,然后我不得不去
最重要的是,我在运行程序时收到了以下警告
log4j:WARN No appenders could be found for logger (org.stsffap.cep.monitoring.CEPMonitoring).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
我很困惑该怎么办?
我能够通过在src/main/resources
文件夹中创建一个log4j.properties
文件并向其添加以下代码来查看日志
log4j.rootLogger=INFO, console
# patterns:
# d = date
# c = class
# F = file
# p = priority (INFO, WARN, etc)
# x = NDC (nested diagnostic context) associated with the thread that generated the logging event
# m = message
# Log all infos in the console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss.SSS} %5p [%-10c] %m%n
# Log all infos in flink-app.log
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.file=flink-app.log
log4j.appender.file.append=false
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss.SSS} %5p [%-10c] %m%n
# suppress info messages from flink
log4j.logger.org.apache.flink=WARN
为了了解这些属性的含义,我建议您观看https://www.youtube.com/watch?v=-GkRuFU_sUgc
也为了抑制低于警告的
log4j:WARN请正确初始化log4j系统。
添加以下行
PropertyConfigurator.configure(" path to log4j.properties");
原因?分层问题
修复:
首先,我使用threadLocal
创建了一个工厂类,用于获取所有线程中的所有错误。
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import java.util.Properties;
public class LogFactory {
private final static ThreadLocal<Logger> logFactory = new ThreadLocal<>();
public static Logger createNewLogger(String className) {
Logger log = Logger.getLogger("Thread" + className);
Properties props = new Properties();
props.setProperty("log4j.appender.file", "org.apache.log4j.RollingFileAppender");
props.setProperty("log4j.appender.file.maxFileSize", "100MB");
props.setProperty("log4j.appender.file.Append", "false");
props.setProperty("log4j.", "100MB");
props.setProperty("log4j.appender.file.maxBackupIndex", "100");
props.setProperty("log4j.appender.file.File", "cep/" + className + ".log");
props.setProperty("log4j.appender.file.threshold", "info");
props.setProperty("log4j.appender.file.layout", "org.apache.log4j.PatternLayout");
props.setProperty("log4j.appender.file.layout.ConversionPattern",
"%d{yyyy-MM-dd HH-mm-ss} | %-5p | %C{1}:%L | %m%n");
props.setProperty("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
props.setProperty("log4j.appender.stdout.Target", "System.out");
props.setProperty("log4j.logger." + "Thread" + className, "INFO, file");
PropertyConfigurator.configure(props);
logFactory.set(log);
return getLogger();
}
public static Logger getLogger() {
return logFactory.get();
}
}
在类RichFlatMapFunction
的实现中,您可以像这样设置属性loger。
private static final Logger logger = LogFactory.createNewLogger("SchoolLocationClassificationFunction");
就是这样。
输出样本。。。
13:26:12,545 INFO ThreadLocationCassificationWindowFunction SCPA,1638451572545,03F1,e1f8a750-e075-4073-812c-cdf8573381db,0,locate,end