Kafka Json Logger Java clients with the best end, and quickly try

Kafka Json Logger best to use the Java client.

slf4j4json

Kafka Json Logger best to use the library; do not try pity!

Description

Kafka is provided a json logger client that supports the output json log format to kafka, file, console.

It supports the full functionality of slf4j.

KafkaLog4jAppender better than with configurable better.

Support close logger, flush log before the program closed to kafka.

Supports Chain programming mode, user-friendly.

maven dependency

  1. Add repository
<repositories>
    <repositories>
         <repository>
             <id>release</id>
             <url>https://raw.github.com/joyang1/slf4j4json/mvn-repo/</url>
             <snapshots>
                 <enabled>true</enabled>
                 <updatePolicy>always</updatePolicy>
             </snapshots>
         </repository>
     </repositories>
</repositories>
  1. Add dependency
<dependency>
    <groupId>cn.tommyyang</groupId>
    <artifactId>slf4j4json</artifactId>
    <version>1.4.0</version>
</dependency>

use

You need to configure the log4j.properties

Since this is also dependent on the jar package slf4j, so log4j.properties press log4j configuration can be used normally.
log4j.properties detailed configuration instructions refer to log4j configuration described

log4j.rootLogger=DEBUG,console

#输出日志到控制台
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.Threshold=all
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss} [%c\:%L]-[%p] %m%n

slf4j4json use

  1. Direct printing
    is similar with slf4j, but the chain library provides programming, user-friendly.

//通过LoggerFactory获取一个logger
final static Logger logger = LoggerFactory.getLogger(Test.class);

//打印必须调用log()方法
logger.info().field("key1", "value1").intField("key2", 1).log();
logger.warn().field("key1", "value1").intField("key2", 1).log();
logger.debug().strField("key1", "value1").longField("key2", 1L).log();
logger.trace().strField("key1", "value1").intField("key2", 1).log();
logger.error().strField("key1", "value1").intField("key2", 1).log();
  1. Example, some of the key assignment is repeated using getLogger method, the following examples

JsonLogger jsonLogger = logger.info().strField("appname", "app").getLogger();
jsonLogger.strField("msg","infotest1").intField("port", 2).log();
jsonLogger.field("msg","infotest2").longField("long", 23L).log();
  1. 1.4.0 update, only getLogger method

Configuring the LogConfig print log to kafka, or to print log file;
if the print log file to file or console, you need to configure the log4j.properties, see how to configure specific use ;
if only print log to kafka, then do not configure log4j .properties.


LogConfig conf = new LogConfig("Vinci", LogLevel.INFO, "localhost:9092", "admin-app-log");
LoggerFactory.openKafkaLogger(conf);

Logger logger  = LoggerFactory.getLogger();

logger.info().strField("test", "test-info").log();
logger.error().strField("test", "test-error").log();
logger.warn().strField("test", "test-warn").log();
logger.debug().strField("test", "test-debug").log();

Export


{"key1":"value1","key2":1,"index":"server-end-log","host":"DESKTOP-2B1VG6J","level":"info","time":1535021174}

version

  • 1.0版本

    • JsonLog For Java to achieve the basic functions

    • Use KafkaLog4jAppender print log to Kafka

  • 1.1版本

    • Repair JsonLogger jsonLogger = logger.info () strField ( "appname", "app") getLogger ();.. Appear bug duplicate content
  • 1.2版本

    • Removing the log comes to the index key

    • Modify repository

      
      <repositories>
          <repository>
              <id>release</id>
              <url>https://raw.github.com/joyang1/slf4j4json/mvn-repo/</url>
              <snapshots>
                  <enabled>true</enabled>
                  <updatePolicy>always</updatePolicy>
              </snapshots>
          </repository>
      </repositories>
      
  • 1.3.0版本

    • Add to realize kafka logger, and batch printing log to kafka

    • 10s or 5m flush again

  • 1.4.0版本

    • Achieve optimization logger

Welcome fork and star

Github Address: slf4j4json

Guess you like

Origin www.cnblogs.com/joyang/p/11839238.html