Ubuntu 16.04+Java8环境下如何仅开启AWS KCL/KPL的ERROR与WARN日志?
Got it, let's sort out this log noise issue for AWS KCL and KPL on your Ubuntu 16.04 + Java 8 setup. Since you don't have an existing log configuration file, we'll build one from scratch based on the logging frameworks these libraries typically use (most often SLF4J with Logback, or Log4j 2).
Option 1: Using Logback (Most Common Default)
AWS KCL/KPL often rely on SLF4J with Logback as the underlying logger. Here's how to create a config file to filter out INFO logs:
Create the Logback config file
In your project'ssrc/main/resourcesdirectory (or place it in your Java classpath if running a standalone JAR), make a file namedlogback.xmlwith this content:<configuration> <!-- Console output (since you don't use log files) --> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- Set root log level to WARN (includes ERROR and WARN only) --> <root level="WARN"> <appender-ref ref="CONSOLE"/> </root> <!-- Explicitly lock AWS KCL/KPL packages to WARN level --> <!-- Covers older KCL versions --> <logger name="com.amazonaws.services.kinesis" level="WARN" additivity="false"> <appender-ref ref="CONSOLE"/> </logger> <!-- Covers newer KCL versions (software.amazon namespace) --> <logger name="software.amazon.kinesis" level="WARN" additivity="false"> <appender-ref ref="CONSOLE"/> </logger> <!-- KPL package --> <logger name="com.amazonaws.services.kinesis.producer" level="WARN" additivity="false"> <appender-ref ref="CONSOLE"/> </logger> </configuration>- The
additivity="false"flag prevents duplicate log entries (since the root logger already handles console output). - We target both old and new AWS package namespaces to cover all KCL versions.
- The
Ensure the config is in your classpath
- If using a build tool like Maven/Gradle, placing the file in
src/main/resourceswill automatically include it in your JAR. - For standalone JARs, put the
logback.xmlin the same directory as your JAR and run with:java -cp ".:your-application.jar" com.your.package.MainClass
- If using a build tool like Maven/Gradle, placing the file in
Option 2: Using Log4j 2
If your project uses Log4j 2 instead, create a log4j2.xml file in src/main/resources (or your classpath) with this content:
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <!-- Root log level set to WARN --> <Root level="WARN"> <AppenderRef ref="Console"/> </Root> <!-- AWS KCL/KPL specific loggers --> <Logger name="com.amazonaws.services.kinesis" level="WARN" additivity="false"> <AppenderRef ref="Console"/> </Logger> <Logger name="software.amazon.kinesis" level="WARN" additivity="false"> <AppenderRef ref="Console"/> </Logger> <Logger name="com.amazonaws.services.kinesis.producer" level="WARN" additivity="false"> <AppenderRef ref="Console"/> </Logger> </Loggers> </Configuration>
Follow the same classpath steps as above to ensure the config is picked up.
Verify the Setup
After applying the config, start your application. You should only see ERROR and WARN level logs from AWS KCL/KPL, with all the noisy INFO entries suppressed.
内容的提问来源于stack exchange,提问作者Stav Alfi

