{"id":536,"date":"2014-10-23T19:53:46","date_gmt":"2014-10-23T17:53:46","guid":{"rendered":"http:\/\/blog.johannes-beck.name\/?p=536"},"modified":"2014-11-30T12:21:33","modified_gmt":"2014-11-30T11:21:33","slug":"shipping-tomcat-logs-to-logstash","status":"publish","type":"post","link":"https:\/\/blog.johannes-beck.name\/?p=536","title":{"rendered":"Shipping tomcat logs to logstash"},"content":{"rendered":"<p>In my <a href=\"https:\/\/blog.johannes-beck.name\/?p=526\">previous post<\/a> I explained how to setup <a href=\"http:\/\/logstash.net\/\">Logstash<\/a>. This time we will expand it to ship the log files from <a href=\"http:\/\/tomcat.apache.org\">Tomcat<\/a>.<\/p>\n<p>I assume you are using Fedora \/ RHEL \/ Centos as OS, and have installed tomcat from the RPMS which are provided by the repository. The tutorial can easily adapted to different Linux distributions or tomcat installations, only file locations will change.<\/p>\n<p>As the first step we will change Tomcat&#8217;s logging behavior and disable the built-in log rotation. In \/etc\/tomcat\/server.xml you need to change the section at the very bottom of the configuration:<\/p>\n<p><code><br \/>\n&lt;Valve className=\"org.apache.catalina.valves.AccessLogValve\"<br \/>\n&nbsp;&nbsp;directory=\"logs\" prefix=\"access\"<br \/>\n&nbsp;&nbsp;suffix=\".log\" rotatable=\"false\" resolveHosts=\"false\"<br \/>\n&nbsp;&nbsp;pattern=\"%h %l %u %t &quot;%r&quot; %s %b\" \/&gt;<br \/>\n<\/code><\/p>\n<p>and disable the log rotation in \/etc\/tomcat\/logging.properties<\/p>\n<p><code><br \/>\n1catalina.org.apache.juli.FileHandler.level = FINE<br \/>\n1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}\/logs<br \/>\n1catalina.org.apache.juli.FileHandler.prefix = catalina<br \/>\n1catalina.org.apache.juli.FileHandler.rotatable = false<br \/>\n2localhost.org.apache.juli.FileHandler.level = FINE<br \/>\n2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}\/logs<br \/>\n2localhost.org.apache.juli.FileHandler.prefix = localhost<br \/>\n2localhost.org.apache.juli.FileHandler.rotatable = false<br \/>\n3manager.org.apache.juli.FileHandler.level = FINE<br \/>\n3manager.org.apache.juli.FileHandler.directory = ${catalina.base}\/logs<br \/>\n3manager.org.apache.juli.FileHandler.prefix = manager<br \/>\n3manager.org.apache.juli.FileHandler.rotatable = false<br \/>\n4host-manager.org.apache.juli.FileHandler.level = FINE<br \/>\n4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}\/logs<br \/>\n4host-manager.org.apache.juli.FileHandler.prefix = host-manager<br \/>\n4host-manager.org.apache.juli.FileHandler.rotatable = false<br \/>\n<\/code><\/p>\n<p>Now the log files in \/var\/log\/tomcat\/ will be written into a files with constant names like access.log, manager.log etc.<br \/>\nTo prevent that the files grow infinitely, we&#8217;ll use logrotate.<\/p>\n<p><code>yum install logrotate<\/code> <\/p>\n<p>And add a configuration for tomcat at \/etc\/logrotate.d\/tomcat<\/p>\n<p><code><br \/>\n\/var\/log\/tomcat\/catalina.out {<br \/>\n&nbsp;&nbsp;copytruncate<br \/>\n&nbsp;&nbsp;daily<br \/>\n&nbsp;&nbsp;rotate 7<br \/>\n&nbsp;&nbsp;compress<br \/>\n&nbsp;&nbsp;missingok<br \/>\n&nbsp;&nbsp;create 0644 tomcat tomcat<br \/>\n}<br \/>\n\/var\/log\/tomcat\/*.log {<br \/>\n&nbsp;&nbsp;copytruncate<br \/>\n&nbsp;&nbsp;daily<br \/>\n&nbsp;&nbsp;rotate 7<br \/>\n&nbsp;&nbsp;compress<br \/>\n&nbsp;&nbsp;missingok<br \/>\n&nbsp;&nbsp;create 0644 tomcat tomcat<br \/>\n}<br \/>\n<\/code><\/p>\n<p>You may want to use different users and retention periods.<\/p>\n<p>Now we will configure the logstash-forwarder to ship the current file; in \/opt\/logstash-forwarder\/ setup a configuration file logstash-forwarder.conf<\/p>\n<p><code><br \/>\n{<br \/>\n&nbsp;\"network\": {<br \/>\n&nbsp;&nbsp;\"servers\": [ \"my-logstash-server.example.org:9998\" ],<br \/>\n&nbsp;&nbsp;\"ssl certificate\": \"\/etc\/pki\/tls\/certs\/client01.crt\",<br \/>\n&nbsp;&nbsp;\"ssl key\": \"\/etc\/pki\/tls\/private\/client01.key\",<br \/>\n&nbsp;&nbsp;\"ssl ca\": \"\/etc\/pki\/CA\/cacert.pem\"<br \/>\n&nbsp;},<br \/>\n&nbsp;\"files\": [<br \/>\n&nbsp;&nbsp;{<br \/>\n&nbsp;&nbsp;&nbsp;\"paths\": [ \"\/var\/log\/tomcat\/access.log\" ],<br \/>\n&nbsp;&nbsp;&nbsp;\"fields\": { \"type\": \"apache-access\" }<br \/>\n&nbsp;&nbsp;}<br \/>\n&nbsp;]<br \/>\n}<br \/>\n<\/code><\/p>\n<p>and you&#8217;ll need to setup the logstash-forwarder as a daemon as explained in the last post. Now it will transmit the log files (in this example only the access.log) to logstash. To be able to parse the file correctly, you&#8217;ll need to configure the server with a parser for the type apache-access (which is similar to Apache HTTPD access.log). The first post has a working setup for this. <\/p>\n<p>If you want to ship the tomcat&#8217;s server log, or your application logs you will need to configure different parsers depending on the log format.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post I explained how to setup Logstash. This time we will expand it to ship the log files from Tomcat. I assume you are using Fedora \/ RHEL \/ Centos as OS, and have installed tomcat from the RPMS which are provided by the repository. The tutorial can easily adapted to different [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,36],"tags":[51,55],"class_list":["post-536","post","type-post","status-publish","format-standard","hentry","category-java","category-linux","tag-logstash","tag-tomcat"],"_links":{"self":[{"href":"https:\/\/blog.johannes-beck.name\/index.php?rest_route=\/wp\/v2\/posts\/536","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.johannes-beck.name\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.johannes-beck.name\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.johannes-beck.name\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.johannes-beck.name\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=536"}],"version-history":[{"count":6,"href":"https:\/\/blog.johannes-beck.name\/index.php?rest_route=\/wp\/v2\/posts\/536\/revisions"}],"predecessor-version":[{"id":542,"href":"https:\/\/blog.johannes-beck.name\/index.php?rest_route=\/wp\/v2\/posts\/536\/revisions\/542"}],"wp:attachment":[{"href":"https:\/\/blog.johannes-beck.name\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=536"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.johannes-beck.name\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=536"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.johannes-beck.name\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=536"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}