jed schneider's posterous

crashland, again.

  • Home
  • Contact
    • Edit
    • Delete
    • Tags
    • Autopost

    Rails Application Specific Logging on Tomcat 7.x with log4j and jRuby

    I've been working on getting applications deployed via Warbler and Tomcat and one of the annoying features is the loss of logging to application specific log files when being deployed without any extra setup. It also turns out that besides me not getting warbler apps to deploy on Tomcat 5.x, the procedure for setting logging up is markedly different than on Tomcat 7. So, since I was able to get the apps running on 7, I went about figuring out how to get the application specific logging setup there also. So, I am assuming many things, in part:

    1. you want to deploy a Ruby on Rails web app on a Tomcat Application Server as a stand alone war file
    2. you are using Tomcat 7.x (I used 7.0.2)
    3. you are using Bundler to manage your gems
    4. the Rails app is running version 2.3.8 (though I doubt it matters)
    5. you are using log4j for logging files
    6. you are planning to create a .war file with warbler
    7. you don't have any gems with C bindings that will give jRuby a bunch of hell
    8. you are using RVM to manage gems, Ruby versions, and gemsets

    Why would I go through configuration hell to setup Tomcat, you say? I can think of no reason that satisfies this answer, but if you are like me, the less you have to deal with adding something 'new' to the system the better off you are. This is a perfect way to be able to work in a reasonable development paradigm (aka Ruby and Rails) and still be able to use the existing infrastructure (java web application servers) with management being less concerned about supporting something 'new'. Also, jRuby is quite performant, and the one click upload deployment strategy of Tomcat is sure nice (or scp your war into the webapps dir). 

    The rest of this guide is a rough outline of how to get Tomcat 7, Warbler, jRuby, and app specific logging with log4j all working together. 

    Tomcat 7

    For the most part, to setup your tomcat server to use log4j, you can follow the handy guide with the following notes:

    1. when the guide says "Place output/extras/tomcat-juli-adapters.jar in $CATALINA_HOME/lib" what the guide really means is that there are some extra files you can download from the extras link and that when you download them they are assuming you are putting them in some file structure 'output/extras'. No need to ack the tomcat project for that output directory like I did. The extras you are interested in are (linked at time of writing - so keep in mind software updates)
      1. the log4j juli jar
      2. the tomcat juli adapters jar
    2. Follow the instructions up to the point where you are able see and view the tomcat.log, it may be easiest to skip ahead to the warbler section of this article to create a java app to run, or download something like probe that will make sure that you are logging to the tomcat log file. You can

      $ tail -f $CATALINA_HOME/logs/tomcat.log

      to see as you use the app that there is logging going on.

    War Up Your App

    Warbler is a nice little gem that I have been using a lot of lately and been very thankful for Nick Sieger's help getting my apps at work off the ground. 

    There are several ways that you can setup your warble configuration and ways to use warbler. For me it was effective to create the war with the command line tool `warble war` from the root of my rails app and which will generate your war file in the root of the project. But, before you can war its best to bundle and get jRuby setup.

    So, follow this guide to setup your rails 2.3.x app to use Bundler. Warbler will use your Gemfile to pull in the gems to your war and make it a standalone application. 

    jRuby as the VM

    first, I use rvm and i have a gemset setup for each app I use.

    I use jRuby for waring up the file and all the other commands in the tutorial. 

    Setting up your Gemfile for jRuby is a bit different. You may choose to catch the gem dependencies with a similar block:

    The activerecord-jdbc-adapter gives you a generator that will setup your rails app to use the jdbc adapters when necessary.
    $ script/generate jdbc
    generates all the appropriate files for you.

     

    At this point I would probably try deploying the app on Tomcat to see if it will run before adding any more complexity with the app specific logging.

    Your workflow for this might be something like:

    $ warble war
    $ cp app.war $CATALINA_HOME/webapps/app.war
    $ open http://localhost:8080/app

    But you can also use the tomcat manager to upload your file.

    The app should load up and you should see output in your tomcat.log file.

    log4j at the app level

    So now you have the logging working on a real app running on tomcat. The rest are just details to get the file structure appropriate for Tomcat to get the information in the right places. First, run

    $ warble config

    which creates a new file in your config directory. open that up and uncomment the following line, adding the appropriate file:

    config.java_classes = FileList["log4j.properties"]

    now, copy the log4j.properties file from your tomcat root to the root of your rails project and change the name of the log file in the properties.

    log4j.appender.R.File=${catalina.home}/logs/app.log

    now, copy the jar files you downloaded earlier into your lib directory. Redeploy the app, and then look inside the log directory, you should see an app.log file created on the first request to the app, or if you restart Tomcat, it should generate on startup. Now, if you need log info, you can go straight here to see the standard rails logging you expect. Hurray!

     

    Tags » deployment ruby on rails tomcat
    • 12 October 2010
    • Views
    • 3 Comments
    • Permalink
    • Tweet
    • 3 responses
    • Like
    • Comment
    7 months ago Jed Schneider responded:
    Jed Schneider
    I'm glad I wrote this, it just came in handy again!
    2 months ago alaclorar1989 responded:
    alaclorar1989
    Goodness, there is a great deal of effective info above!
    www.muebles.pl
    about 1 month ago termonttofi1982 liked this post.
  • Jed Schneider's Space

    Jed Schneider is a Ruby and Rails software developer for the Medical University of South Carolina. He was a pro cyclist at one time, now he just rides for fun.

    Archive

    2011 (2)
    July (1)
    June (1)
    2010 (31)
    December (1)
    November (3)
    October (1)
    September (2)
    August (3)
    July (7)
    June (8)
    May (1)
    April (5)
  • About Jed Schneider

    Jed Schneider is a Ruby and Rails software developer for the Medical University of South Carolina. He was a pro cyclist at one time, now he just rides for fun.

  • Subscribe via RSS
  • Follow Me

      TwitterFacebookLinkedIn

Theme created for Posterous by Obox