error using SLF4J logger in Apache Flink

1.8k views Asked by At

I want to output the logs in Flink, I have created the logger code as mentioned below

I have imported the LoggerFactory

import org.slf4j.LoggerFactory;

Class CEPMonitoring contains code as follows

final  static org.slf4j.Logger logger = LoggerFactory.getLogger(CEPMonitoring.class);

     public static void main(String[] args) throws Exception {

     logger.info("execution began \n "); 
      }

However, this is not printing anything. Also, I don't have logback.xml file as mentioned at https://ci.apache.org/projects/flink/flink-docs-release-0.7/internal_logging.html

P.S - I am getting error when i use

final  static Logger logger = LoggerFactory.getLogger(CEPMonitoring.class);

Which finally led me to write

final static org.slf4j.Logger logger = LoggerFactory.getLogger(CEPMonitoring.class);

it gave an error and then I have to

On top of all this I am getting following warning upon running program

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.

I am confused what to do?

2

There are 2 answers

3
Amarjit Dhillon On

I was able to see the logs by creating a log4j.properties file in the src/main/resources folder and adding following code to it

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

In order to learn what these properties mean, I will recommend watching https://www.youtube.com/watch?v=-GkRuFU_sUgc

Also in order to suppress below warning

log4j:WARN Please initialize the log4j system properly.

Add the following line

 PropertyConfigurator.configure(" path to log4j.properties");
0
Cleidson D Nascimento On

Cause? demiter problem

Fix:

first i created a factory class using a threadLocal, for get all errors in all threads.

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();
    }

}

and in a implementation of class RichFlatMapFunction, you setting the attribute loger like this.

private static final Logger logger =  LogFactory.createNewLogger("SchoolLocationClassificationFunction");

and this is it.

output sample...

13:26:12,545 INFO  ThreadLocationCassificationWindowFunction  SCPA,1638451572545,03F1,e1f8a750-e075-4073-812c-cdf8573381db,0,locate,end