Logstash集成log4j

1. 配置log4j.properties

log4j.rootLogger=INFO,DEBUG,logstash
log4j.appender.logstash=org.apache.log4j.net.SocketAppender
log4j.appender.logstash.Port=4560
log4j.appender.logstash.RemoteHost=10.0.0.5
log4j.appender.logstash.ReconnetionDelay=60000
log4j.appender.logstash.LocationInfo=true

2. 修改logstash input组件(favblog-log4j.conf),将日志输出到Elasticsearch

input{
 log4j{
    host => "10.0.0.5"
    mode => "server"
    type => "log4j-json"
    port => 4560
 }
}
output{
    stdout{ codec => rubydebug }
    elasticsearch { hosts => ["10.0.0.5:9200"] }
}

3.启动Logstash

[[email protected] bin]# ./logstash -f favblog-log4j.conf
Settings: Default pipeline workers: 4
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAuthCache).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Pipeline main started
{
        "message" => "*************************************Hello test!",
       "@version" => "1",
     "@timestamp" => "2016-05-31T07:24:34.989Z",
      "timestamp" => 1464679190253,
           "path" => "com.favccxx.favblog.platform.interceptor.FavInterceptor",
       "priority" => "INFO",
    "logger_name" => "com.favccxx.favblog.platform.interceptor.FavInterceptor",
         "thread" => "http-bio-8080-exec-2",
          "class" => "com.favccxx.favblog.platform.interceptor.FavInterceptor",
           "file" => "AppAuthorityInterceptor.java:65",
         "method" => "intercept",
           "host" => "10.0.0.115:54930",
           "type" => "log4j-json"
}
{
        "message" => "** errorInvokeKey:[MBGL-S]QemI2gBhHC",
       "@version" => "1",
     "@timestamp" => "2016-05-31T07:24:35.000Z",
      "timestamp" => 1464679190264,
           "path" => "com.favccxx.favblog.platform.interceptor.FavInterceptor",
       "priority" => "ERROR",
    "logger_name" => "com.favccxx.favblog.platform.interceptor.FavInterceptor",
         "thread" => "http-bio-8080-exec-2",
          "class" => "com.favccxx.favblog.platform.interceptor.FavInterceptor",
           "file" => "AppAuthorityInterceptor.java:140",
         "method" => "intercept",
           "host" => "10.0.0.115:54930",
           "type" => "log4j-json"
}
{
        "message" => "Exception occurred during processing request: Cannot create a session after the response has been committed",
       "@version" => "1",
     "@timestamp" => "2016-05-31T07:24:35.023Z",
      "timestamp" => 1464679190280,
           "path" => "org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler",
       "priority" => "ERROR",
    "logger_name" => "org.apache.struts2.dispatcher.DefaultDispatcherErrorHandler",
         "thread" => "http-bio-8080-exec-2",
          "class" => "com.opensymphony.xwork2.util.logging.commons.CommonsLogger",
           "file" => "CommonsLogger.java:42",
         "method" => "error",
    "stack_trace" => "java.lang.IllegalStateException: Cannot create a session after the response has been committed\n\tat org.apache.catalina.connector.Request.doGetSession(Request.java:3013)\n\tat org.apache.catalina.connector.Request.getSession(Request.java:2385)\n\tat org.apache.catalina.connector.RequestFacade.getSession(RequestFacade.java:897)\n\tat javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:229)\n\tat org.apache.struts2.dispatcher.SessionMap.put(SessionMap.java:182)\n\tat org.apache.struts2.interceptor.MessageStoreInterceptor.after(MessageStoreInterceptor.java:297)\n\tat org.apache.struts2.interceptor.MessageStoreInterceptor.intercept(MessageStoreInterceptor.java:198)\n\tat com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:244)\n\tat org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)\n\tat org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:564)\n\tat org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:81)\n\tat org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)\n\tat org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)\n\tat org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)\n\tat org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625)\n\tat org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.lang.Thread.run(Unknown Source)",
           "host" => "10.0.0.115:54930",
           "type" => "log4j-json"
}

4. 查询Log4j输出日志

http://10.0.0.5:9200/logstash-2016.05.31/_search?q=*&pretty%27

在elasticsearch中查询到输出的log4j日志。

时间: 2024-10-20 04:17:49

Logstash集成log4j的相关文章

SSH集成log4j日志环境

第一步:在web.xml初始化log4j <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocatio

java工程集成log4J

log4j是Java开发平台中的日志管理工具,是Apache基金会的一个开源项目,它的作用可以简单的理解为在开发过程中替代system.out的功能. 1.在工程中集成log4j: (1)在官网http://logging.apache.org/log4j/2.x/download.html中下载jar包并添加到build path中 (2)修改配置文件log4j.properties,可以百度一下,这种资源很多,我的配置文件如下: log4j.rootLogger =DEBUG,console

spring集成Log4j以及log4j配置简要说明

Spring集成: web.xml中配置log4j <context-param> <param-name>log4jConfigLocation</param-name> <param-value>WEB-INF/log4j.xml</param-value></context-param> <!-- 加载Spring框架中的log4j监听器Log4jConfigListener --><listener>

ELK日志处理之使用logstash收集log4J日志

介绍一下如何从Java工程中导出log4J日志到Logstash. 一.log4j基础 不能免俗的官方介绍: Log4j 是一个使用 Java 语言编写的,可靠.快速.灵活的日志框架(API),使用 Apache Software License 授权.它被移植到 C.C++.C#.Perl.Python.Ruby 和 Eiffel 语言中. Log4j 是高度可配置的,在运行期使用外部的配置文件对其进行配置.它按照优先级别记录日志,并可将日志信息定向输出到各种介质,比如数据库.文件.控制台.U

springMVC集成log4j

log4j的使用非常简单,简单三步就ok. Log4j所需的jar包:commons-logging.jar和log4j.jar. Jar包下载地址:http://download.csdn.net/detail/qq_33556185/9568809 (一)写log4j.properties: #设置日志等级及目的地 log4j.rootLogger=ERROR,Console,File #输出到控制台 log4j.appender.Console=org.apache.log4j.Conso

(OAF)jdeveloper集成log4j并将日志输出到指定文件并写入数据库

参考: How to configure Log4j in JDev 11g Ever wanted to use log4j in your adf project ? Well though Oracle doesn't recommends the use of log4j, i say its purely your choice to use it or not. So how do we configure log4j in adf  ? Pretty simple..just fo

Spring集成Log4j日志框架

1.日志系统介绍 slf4j,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统.简答的讲就是slf4j是一系列的日志接口,而log4j是具体实现了的日志框架. slf4j与常用日志框架绑定关系,图片来源 2.Maven导入slf4j和log4j,编辑pom.xml <!-- 导入slf4j-log4j12,依赖slf4j-api和log4j,自动导入 --> <dependency> <gro

Spring MVC-集成(Integration)-集成LOG4J示例(转载实践)

以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_log4j.htm 说明:示例基于Spring MVC 4.1.6. 以下示例说明如何使用Spring Web MVC框架来触发LOG4J.首先,让我们使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态窗体的Web应用程序: 步 描述 1 创建一个名为TestWeb的项目,在一个包com.tutorialspoint下,如Sprin

spring 集成 log4j 配置

在web.xml中增加如下代码: <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <context-param> <param-name>webAppRootKey</param