Logstash and Log4j

I wanted to log from a log4j process through to Logstash, and have the logging stored in Elastic search. This can be done using the code at https://github.com/logstash/log4j-jsonevent-layout

To make things easy for my test, I put the source code for net.logstash.log4j.JSONEventLayoutV1and net.logstash.log4j.data.HostData into my source tree.

I then added json-smart-1.1.1.jar to the classpath (from https://code.google.com/p/json-smart/downloads/detail?name=json-smart-1.1.1.jar&can=2&q=)

I then created the following log4j appender in log4j.xml then used it in code:

<appender name="LogStash"
    class="org.apache.log4j.DailyRollingFileAppender">
    <param name="File" value="logs/panCoreSaas.log" />
    <param name="Append" value="true" />
    <layout class="net.logstash.log4j.JSONEventLayoutV1"/>
</appender>

Using this appender produces logs in JSON format:

{  "thread_name":"http-8080-3@#zUaiBHqDI66O25BntXrghixzzEljOHlHGPB0dNmz4RoX3gNWyB",
    "message":"Calling FindEvents",
    "@timestamp":"2014-11-11T12:48:35.094Z",
    "level":"INFO",
    "mdc": {},
    "file":"WebServiceTransactionRunner.java",
    "class":"com.pancredit.eaitoolkit.framework.runner.ws.WebServiceTransactionRunner",
    "line_number":"638",
    "logger_name":"com.pancredit.eaitoolkit.framework.runner.ws.WebServiceTransactionRunner",
    "method":"logCall",
    "@version":1,
    "source_host":"greenfinch"
}

Create a log4j_file.conf with the following contents:

input {
  file {
    path => "/apps/logstash/logstash-1.4.2/panCoreSaas.log"
    start_position => beginning
  }
}

output {
  elasticsearch {
    host => localhost
  }
  stdout { codec => rubydebug }
}

and then start logstash to process the file

$ bin/logstash -f log4j_json.conf
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

智能体格

你的鼓将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值