Link to home
Start Free TrialLog in
Avatar of LeanMoreTryMore
LeanMoreTryMore

asked on

use log4j - can see the output from the console but not found the log file.

As advised from object, I used log4j. yes. this is cool.

It displays the INFO message to console, but i can't find the physical file i specified in the program.

See my code below:


public class Scheduler extends MailUtilities implements EvtConfig {

  static Logger schedulerLogger = Logger.getLogger("scheduler.log");
  Appender      schedulerAppender;
  SimpleLayout  schedulerLayout;
 
  public static void main(String[] args) {
     try {
       int dbConnOk = Utilities.SQLJdbConnection(userName, pwd, dbName );
       if ( dbConnOk == 0 ) {
           new Scheduler();
       }
       else {
         System.exit(1);
       }
     }  
     catch (Exception e) {
       System.out.println("Scheduler.main() : Errro occured due to " + e);
       e.printStackTrace();
       System.exit(1);            
     }
  } // main

  private Scheduler() throws Exception  {
    /* Log layout handler */
    schedulerLogger.setLevel(Level.INFO);
    schedulerLayout   = new SimpleLayout();
    schedulerAppender = new ConsoleAppender(schedulerLayout);
    schedulerLogger.addAppender(schedulerAppender);
    //
    schedulerLogger.info("UTeventScheduler: START");  

<=== I can see the above message in the console, but CAN'T FIND the scheduler.log file


 }


==========================
Please advise if I have missed out something.
Thanks
Avatar of Mick Barry
Mick Barry
Flag of Australia image

>   static Logger schedulerLogger = Logger.getLogger("scheduler.log");

Thats just the name of the logger.
To send the log to a file you need to add a file appender.
you can also handle the config via properties file instead of in your code

a good read: http://logging.apache.org/log4j/docs/manual.html
Avatar of LeanMoreTryMore
LeanMoreTryMore

ASKER

Please tell me how to i add a file to a file appender.
giving me a very simple codes is greatly appricated
 schedulerLogger.addAppender(new FileAppender(shedulerlayout, "sheduler.log"));
Very thanks for that.
Do i have to close the log if this process is aborted?
No log4j should look after that for you.
Just one last question.

Does log4j cause any buffer problem as I previouly experienced using System.out.println?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
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
Thanks for your quick response.
If not because of you providing such a quick response, i would cancel my subscription long time ago.