0

我想在 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 "); 
      }

但是,这并没有打印任何东西。另外,我没有https://ci.apache.org/projects/flink/flink-docs-release-0.7/internal_logging.htmllogback.xml中提到的文件

PS - 我在使用时遇到错误

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.

我很困惑该怎么办?

4

2 回答 2

1

我可以通过在文件夹中创建一个log4j.properties文件src/main/resources并向其中添加以下代码来查看日志

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");
于 2017-08-29T00:39:52.813 回答
0

原因?分号问题

使固定:

首先,我使用 , 创建了一个工厂类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();
    }

}

在 class 的实现中RichFlatMapFunction,您可以像这样设置属性记录器。

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
于 2021-12-02T13:45:56.110 回答