• 如何使用日志记录
    • Configuring Log4j
    • Configuring logback
    • Best practices for developers

    如何使用日志记录

    The logging in Flink is implemented using the slf4j logging interface. As underlying logging framework, log4j is used. We also provide logback configuration files and pass them to the JVM’s as properties. Users willing to use logback instead of log4j can just exclude log4j (or delete it from the lib/ folder).

    • Configuring Log4j
    • Configuring logback
    • Best practices for developers

    Configuring Log4j

    Log4j is controlled using property files. In Flink’s case, the file is usually called log4j.properties. We pass the filename and location of this file using the -Dlog4j.configuration= parameter to the JVM.

    Flink ships with the following default properties files:

    • log4j-cli.properties: Used by the Flink command line client (e.g. flink run) (not code executed on the cluster)
    • log4j-yarn-session.properties: Used by the Flink command line client when starting a YARN session (yarn-session.sh)
    • log4j.properties: JobManager/Taskmanager logs (both standalone and YARN)

    Configuring logback

    For users and developers alike it is important to control the logging framework.The configuration of the logging framework is exclusively done by configuration files.The configuration file either has to be specified by setting the environment property -Dlogback.configurationFile=<file> or by putting logback.xml in the classpath.The conf directory contains a logback.xml file which can be modified and is used if Flink is started outside of an IDE and with the provided starting scripts.The provided logback.xml has the following form:

    1. <configuration>
    2. <appender name="file" class="ch.qos.logback.core.FileAppender">
    3. <file>${log.file}</file>
    4. <append>false</append>
    5. <encoder>
    6. <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{60} %X{sourceThread} - %msg%n</pattern>
    7. </encoder>
    8. </appender>
    9. <root level="INFO">
    10. <appender-ref ref="file"/>
    11. </root>
    12. </configuration>

    In order to control the logging level of org.apache.flink.runtime.jobgraph.JobGraph, for example, one would have to add the following line to the configuration file.

    <logger name="org.apache.flink.runtime.jobgraph.JobGraph" level="DEBUG"/>

    For further information on configuring logback see LOGback’s manual.

    Best practices for developers

    The loggers using slf4j are created by calling

    import org.slf4j.LoggerFactory
    import org.slf4j.Logger
    
    Logger LOG = LoggerFactory.getLogger(Foobar.class)

    In order to benefit most from slf4j, it is recommended to use its placeholder mechanism.Using placeholders allows to avoid unnecessary string constructions in case that the logging level is set so high that the message would not be logged.The syntax of placeholders is the following:

    LOG.info("This message contains {} placeholders. {}", 2, "Yippie");

    Placeholders can also be used in conjunction with exceptions which shall be logged.

    catch(Exception exception){
        LOG.error("An {} occurred.", "error", exception);
    }