LogStash

[LogStash] sockAppender로 log4j 로그 입력

behonestar 2015. 8. 24. 20:12

socket server 설정


1. 서버 실행 시 참조할 log4j property 준비

: ConsoleAppender를 사용하여 stdout(콘솔화면)으로 출력하는 설정

# vi ~/log4j-server.properties

log4j.rootLogger=INFO,stdout,R

# stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d{HH:mm:ss,SSS} %m%n

# Adding this to avoid thrift logging disconnect errors.
log4j.logger.org.apache.thrift.server.TNonblockingServer=ERROR


2. log4j-1.2.17 다운로드

: 최신 버전은 SimpleSocketServer 지원하지 않음

# wget http://apache.mirror.cdnetworks.com/logging/log4j/1.2.17/log4j-1.2.17.tar.gz

# tar -zxvf ./log4j-1.2.17.tar.gz


3. SimpleSocketServer 실행

: 포트 4570

# cd apache-log4j-1.2.17/

# java -classpath ./log4j-1.2.17.jar org.apache.log4j.net.SimpleSocketServer 4570 ../log4j-server.properties



socket client (JAVA) 설정


1-1. java 프로젝트 log4j 설정 파일 수정

: 보통 *.xml 또는 *.properties 파일

: 아래는 xml 포맷의 설정 파일

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<!-- Note that this file is refreshed by the server every 60seconds, as specified
    in web.xml -->

<log4j:configuration debug="true">

    <appender name="consoleAppender" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%5p] %d{ISO8601} [%t] - [%c{1}] %m%n" />
        </layout>
    </appender>
   
    <appender name="socketAppender" class="org.apache.log4j.net.SocketAppender"> 
        <param name="Port" value="4570" /> 
        <param name="RemoteHost" value="54.65.37.240" /> 
        <param name="ReconnectionDelay" value="10000" /> 
        <param name="Threshold" value="INFO" /> 
    </appender>
   
    <logger name="logger" additivity="false">
        <level value="debug" />
        <appender-ref ref="socketAppender" />
        <appender-ref ref="consoleAppender" />
    </logger>

    <root>
        <priority value="info" />
        <appender-ref ref="consoleAppender" />
    </root>


</log4j:configuration>


1-2. 소스코드에서 직접 수정

package log4j.test;

import org.apache.log4j.Logger;
import org.apache.log4j.net.SocketAppender;

public class log4jTest {

    static Logger logger = Logger.getLogger(log4jTest.class);
   
    public static void main(String[] args)
    {
        try {
            logger.addAppender(new SocketAppender("54.65.37.240", 4570));


            logger.info("log4j SocketAppender Test!");
            Thread.sleep(1000);
               
         } catch (Exception e) {
           System.out.println("Failed to LOG by client : " + e.getMessage());
      }
    }
}


2. socket server 출력 결과

 INFO 11:11:32,556 Connected to client at /222.112.8.36
 INFO 11:11:32,556 Starting new socket node.
 INFO 11:11:32,556 Waiting to accept a new client.
 INFO 11:11:24,944 log4j SocketAppender Test!
 INFO 11:11:33,570 Caught java.net.SocketException closing conneciton.