• Log4J Appender


    项目中使用log4j打印的内容同时传输到flume


    1、flume端

    flume的agent配置内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    a1.sources=s1
    a1.sinks=k1
    a1.channels=c1
     
    a1.sources.s1.channels=c1
    a1.sinks.k1.channel=c1
     
    a1.sources.s1.type=avro
    a1.sources.s1.bind=vm1
    a1.sources.s1.port=41414
     
    a1.sinks.k1.type=logger
     
    a1.channels.c1.type=memory
    a1.channels.c1.capacity=1000
    a1.channels.c1.transactionCapacity=100

    启动这个agent

    1
    flume-ng agent --conf conf/ --conf-file conf/a1.conf --name a1 -Dflume.root.logger=INFO,console

    2、java客户端

    log4j.properties

    1
    2
    3
    4
    5
    log4j.rootLogger=INFO,flume
    log4j.appender.flume = org.apache.flume.clients.log4jappender.Log4jAppender
    log4j.appender.flume.Hostname = 192.168.1.111
    log4j.appender.flume.Port = 41414
    log4j.appender.flume.UnsafeMode = true

    java测试代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    package com.test.flume;
     
    import org.apache.log4j.Logger;
     
    public class Log4jAppenderTest {
     
        public static void main(String[] args) {
            Logger logger = Logger.getLogger(Log4jAppenderTest.class);
            logger.info("test successful");
            logger.error("error");
            System.out.println("ok");
        }
     
    }

    补充:

    1、负载均衡的Log4J Appender

    修改log4j的配置即可:

    1
    2
    3
    log4j.rootLogger=INFO,flume
    log4j.appender.flume = org.apache.flume.clients.log4jappender.LoadBalancingLog4jAppender
    log4j.appender.flume.Hosts = 192.168.1.111:41414 192.168.1.111:41414

    不同的host用空格分隔

    2、Log4J Appender也可以使用layout,如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    log4j.rootLogger=INFO,console,flume
     
    log4j.appender.flume = org.apache.flume.clients.log4jappender.Log4jAppender
    log4j.appender.flume.Hostname = 192.168.1.111
    log4j.appender.flume.Port = 41414
    log4j.appender.flume.UnsafeMode = true
    log4j.appender.flume.layout=org.apache.log4j.PatternLayout
    log4j.appender.flume.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n
     
    log4j.appender.console=org.apache.log4j.ConsoleAppender
    log4j.appender.console.layout=org.apache.log4j.PatternLayout
    log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n








  • 相关阅读:
    内存溢出
    接手新业务
    pjb fabu
    中文手册
    人背的时候,做啥都失败
    帮助开发人员学习
    python中的__dict__,__getattr__,__setattr__
    NetCore在Docker中发布及运行
    ELK基础配置
    IdentityServer4 手动验签及日志记录
  • 原文地址:https://www.cnblogs.com/lishouguang/p/4560889.html
Copyright © 2020-2023  润新知