5 Replies Latest reply: Aug 6, 2007 12:59 AM by 807605 RSS

    log4j : Different log files for different layers

    807605
      Hi all

      I need to output different log files for different layers of my project. Is there any way to do that?

      Any help is appreciated
      Gurpreet Singh
        • 1. Re: log4j : Different log files for different layers
          807605
          haven't tried but this should work
          Logger log =  PackageName.logMessage(ABC.class);
          Logger log1 =  PackageName.logMessage(DEF.class);
          You can use log and log1 in different layer.......
          • 2. Re: log4j : Different log files for different layers
            807605
            actually i did'nt get you regarding the following

            Logger log = PackageName.logMessage(ABC.class);
            Logger log1 = PackageName.logMessage(DEF.class);


            lets say my package is com.cr.sc.action and my class name is PersonAction.java ... how to write the code ??
            • 3. Re: log4j : Different log files for different layers
              807605
              You can make a package "ApplicationNameLogger" and set it into your project environment. Then, make two java classes in it as:
              public class YourLayer1Logger { 
              private static PrintWriter p = new PrintWriter(System.out);
              private static Properties  dbprop = null;
                   
              private BulkImportUtilityLogger() {
                             super();
              }
                             
              static {
                                  PropertyConfigurator.configureAndWatch("/log4j.properties");
                                  
              }
                   
              public static Logger logMessage(Class src) {
                        return     Logger.getLogger(src);
              }
              }
              Similarly make another java class YourLayer2Logger ....in same "ApplicationNameLogger" package. Whereever you want to use these, import "ApplicationNameLogger" package in those clases and use as :
              ApplicationNameLogger logLayer1 =  YourLayer1Logger.logMessage(ABC.class);
              or
              ApplicationNameLogger logLayer2 =  YourLayer2Logger.logMessage(ABC.class);
              Hope i am more clear now.

              Message was edited by:
              Mayank_03

              Message was edited by:
              Mayank_03
              • 4. Re: log4j : Different log files for different layers
                807605
                Thanks friend ..

                But i need some clarifications ::

                Why have you used the following lines >>

                private static PrintWriter p = new PrintWriter(System.out);
                private static Properties dbprop = null;

                and what is this >>
                private BulkImportUtilityLogger() {
                               super();
                }

                Thanks for your patience
                • 5. Re: log4j : Different log files for different layers
                  807605
                  hi,
                  private static PrintWriter p = new PrintWriter(System.out);
                  ---you can ignore this. I used this statement while testing
                  --------------------------------------------------------------------------------------------
                  private static Properties  dbprop = null;
                  --- This is used when you make a env. file for your application. This is also optional (depends on your application)
                  -------------------------------------------------------------------------------------------
                  corrected code is:
                  public class YourLayer1Logger { 
                  
                       
                  private YourLayer1Logger() {
                                 super();
                  }
                                 
                  static {
                           PropertyConfigurator.configureAndWatch("/log4j.properties");
                                      
                  }
                       
                  public static Logger logMessage(Class src) {
                            return     Logger.getLogger(src);
                  }
                  }