Share the Knowledge
RSS icon Home icon
  • How to log to multiple log files with log4j

    Posted on March 6th, 2011 webmaster No comments         Print Print

    Back to Java coding!

    We have a Java application that calls an external application and since it gets called quite often we figured we should probably log its standard output and standard error streams to a separate log file so we could easily locate issues with it later.  This would also allow us to create a different message format for it and different handlers.

    Luckily, it turned out that this is pretty easy to do with log4j, simply edit your log4j.properties file and add a category (which is basically another logger).

    For example:

    # Add a new category/logger called "ExternalAppLogger" and specify
    # the log level ("INFO") and the appender name ("extAppLogFile").
    log4j.category.ExternalAppLogger=INFO, extAppLogFile
    
    # Set the appender settings
    log4j.appender.extAppLogFile=org.apache.log4j.RollingFileAppender
    log4j.appender.extAppLogFile.File=D:/MyCoolApp/logs/external-app.log
    log4j.appender.extAppLogFile.MaxFileSize=20000KB
    log4j.appender.extAppLogFile.MaxBackupIndex=10
    log4j.appender.extAppLogFile.layout=org.apache.log4j.PatternLayout
    log4j.appender.extAppLogFile.layout.ConversionPattern=%d %5p %C{1}: %n%m%n
    

    Now in your Java code, you can create a separate Logger object for the above:

    
    Logger extAppLogger = Logger.getLogger("ExternalAppLogger")
    
    extAppLogger.info("This message will be written to D:/MyCoolApp/logs/external-app.log.")
    

    Source: http://www.jguru.com/faq/view.jsp?EID=1311014

  • Java losing to newer programming languages?

    Posted on January 3rd, 2008 webmaster No comments         Print Print

    Here’s an interesting article from infoworld.com saying that Java is losing ground to newer languages such as Ruby on Rails, PHP, AJAX, and Microsoft’s .NET in web and mobile applications development. Developers are complaining that Java hasn’t gotten easier and has become more complicated which slows them down. The article also mentioned a survey of 1,850 businesses and found .NET to be the choice over Java among businesses of all sizes and industries. Click here to read the full article…

    Do you agree or disagree?