Link to home
Start Free TrialLog in
Avatar of sahrom
sahrom

asked on

struts app and adding logging to it using common or jdk14looger - syntax for FileHandler

I would like to add logging to a struts application - but not sure how to

I have common logger set up and it is working but only writes to the console

how in the properties file would I specify an file to put it out to

I ran across this:
Handler fh = new FileHandler("%t/wombat.log");
Logger.getLogger("").addHandler(fh);


but I would like it in the properites file

org.apache.commons.logging.simplelog.????

thank you!!

also what goes in the commons-logger.properties and what should go in the simplelog.properties
Avatar of bloodredsun
bloodredsun
Flag of Australia image

If you are going to add logging to a struts application then I would suggest using Log4J directly (commons logging will try to use Log4J if you have it).

Log4J http://logging.apache.org/log4j/docs/ requires you to download the jars and place a properties file in the WEB-INF lib.

here's and example of how to configure a properties file

log4j.log
-------------
<!--

   Log4J Configuration Quick Reference:
   ====================================


   Priority order is DEBUG < INFO < WARN < ERROR < FATAL


   PatternLayout conversion characters:

    %c   Category of the logging event
    %C   Fully qualified class name of the caller
    %d   Date of the logging event  (example: %d{HH:mm:ss,SSS} )
    %F   File name where the logging request was issued (caution: extremely slow
)
    %l   Location information of the caller (caution: extremely slow)
    %L   Line number from where the logging request was issued (caution: extreme
ly slow)
    %m   Application-supplied message
    %M   Method name from where the logging request was issued (caution: extreme
ly slow)
    %n   Line separator
    %p   Priority of the logging event
    %r   Number of milliseconds since the start of the application
    %t   Name of the thread that generated the logging event
    %x   Nested diagnotic context associated with the thread
    %%   A single percent sign

   Format modifiers examples:

    %20c     Left pad with spaces if category is less than 20 characters long
    %-20c    Right pad with spaces if category is less than 20 characters long
    %.30c    Truncate from the beginning if category is more than 30 chars long
    %20.30c  Left pad 20 chars + truncate from beginning if more than 30 chars
    %-20.30c Right pad 20 chars + truncate from beginning if more than 30 chars

   Examples:  "%r [%t] %-5p %c %x - %m\n"
              "%-6r [%15.15t] %-5p %30.30c %x - %m\n"

-->
#level is debug and goes to stdout and R which we will specify later
log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n


log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
og4j.appender.R.DatePattern='.'ddMMyyyy
log4j.appender.R.File=./logs/log4j.log

# log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
# log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n


calling the log in a struts action

-------------------
//normal imports
// Jakarta
import org.apache.log4j.Logger;

public class MyAction extends Action      {
      private static final Logger _LOGGER = Logger.getLogger( MyAction .class.getName() );

      public ActionForward execute(
                  final ActionMapping pMapping,
                  final ActionForm pForm,
                  final HttpServletRequest pRequest,
                  final HttpServletResponse pResponse)
      throws Exception {
             _LOGGER.info(" action has started" ); //make logger call at info level      
        //blah blah return an actionforward  
      } // execute
Avatar of sahrom
sahrom

ASKER

Thanks very much for you reply.  There is so much more documentation on Log4J, that I would have tried this one to begin with , but I was told they had tried and there was a problem somewhere.  They are using JBoss
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial