Results 1 to 4 of 4
  1. #1
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Default Creates multiple log files

    Hi! I am having a problem. I want to write all the stackTraces to my log file. So I have created a java file to write log...

    import java.util.Properties;
    import java.util.logging.FileHandler;
    import java.util.logging.Logger;
    import java.util.logging.SimpleFormatter;

    public class WriteToLog {

    private static final String root = System.getProperty("user.dir");
    private static final String log_file = "//MyLogFile.log";
    private static Logger logger = Logger.getLogger("MyLog");
    private static Properties prop;
    private static FileHandler fh;

    public static void writeLog(String location, String message) {
    try {
    fh = new FileHandler((new StringBuilder()).append(root).append(log_file).toS tring(), true);
    logger.addHandler(fh);
    SimpleFormatter formatter = new SimpleFormatter();
    fh.setFormatter(formatter);
    logger.info(location + message);
    } catch (Exception ex) {
    ex.printStackTrace();
    }
    }
    }

    and when I write to log file using
    catch (SQLException e)
    {
    WriteToLog.writeLog(connectMySql.class.getSimpleNa me(), "Sql Connection failed");
    }

    like this. It writes but creates lots of log files. How Can I append all these logs to single logfile?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Creates multiple log files

    Add the FileHandler a single time, not every time you try to write out to the log.

  3. #3
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Default Re: Creates multiple log files

    I tried to add the file handler on constructor in this way :

    public WriteToLog()
    {
    try
    {
    fh = new FileHandler((new StringBuilder()).append(ruta).append(log_file).toS tring(), true);
    logger.addHandler(fh);
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    it gives me the null pointer exception.

  4. #4
    bikashlama is offline Member
    Join Date
    Dec 2011
    Location
    Kathmandu, Nepal
    Posts
    15
    Rep Power
    0

    Thumbs up Re: Creates multiple log files

    I got it.

    I just needed to add to methods to filehandler.

    fh.flush();
    fh.close();
    Last edited by bikashlama; 12-27-2011 at 06:19 AM.

Similar Threads

  1. Merging multiple XML files
    By MarcoGT in forum XML
    Replies: 0
    Last Post: 07-09-2011, 01:59 PM
  2. Replies: 6
    Last Post: 07-02-2011, 06:55 PM
  3. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  4. run multiple jar files by one click
    By wallacetju in forum Advanced Java
    Replies: 0
    Last Post: 02-06-2011, 06:50 AM
  5. When the compiler creates the variable?
    By hitesh_public in forum New To Java
    Replies: 14
    Last Post: 08-17-2010, 10:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •