Results 1 to 3 of 3
  1. #1
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Logger logs old messages

    I have the following code that comes directly from the book Core Java vol 1. The last two statements are mine but when I change the last statement's String the message being logged doesn't change. For that manner when I change the level of the last statement the file doesn't update to the correct log level. What am I doing wrong?

    Java Code:
    public class LogTester
    {
    	public static void main(String[] args)
    	{
    		if (System.getProperty("java.util.logging.config.class") == null
    				&& System.getProperty("java.util.config.file") == null)
    		{
    			try
    			{
    				Logger.getLogger("com.horstmann.corejava").setLevel(Level.ALL);
    				final int LOG_ROTATION_COUNT = 10;
    				Handler handler = new FileHandler("%h/LoggingImageViewer%g.log", 0, LOG_ROTATION_COUNT);
    				handler.setLevel(Level.ALL);
    				Logger.getLogger("com.horstmann.corejava").addHandler(handler);
    			}
    			catch (IOException e)
    			{
    				Logger.getLogger("com.horstmann.corejava").log(Level.SEVERE, "Can't create log file handler", e);
    			}
    		}
    
    		Logger logger = Logger.getLogger("com.horstmann.corejava");
    		logger.finer("New message");
    	}
    }
    EDIT: Forgot to say the file also only logs the old message and level from a previous recompilation
    Last edited by typedef; 03-04-2014 at 07:27 PM.

  2. #2
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Logger logs old messages

    Tried a new example I adapted from an online example. It works until I try to add a rotation to the FileHandler. Here is the code for that example:
    Java Code:
    public static void main(String[] args)
    {
    	Logger logger = Logger.getLogger("com.horstmann.corejava");
    	FileHandler handler = null;
    
    	try
    	{
    		logger.setLevel(Level.ALL);
    		handler = new FileHandler("%h/LoggingImageViewer%g.log");
    		handler.setLevel(Level.ALL);
    		SimpleFormatter formatter = new SimpleFormatter();
    		handler.setFormatter(formatter);
    		logger.addHandler(handler);
    		logger.severe("New asadfasdfd");
    	}
    	catch (IOException e)
    	{
    		logger.log(Level.SEVERE, "Can't create log file handler", e);
    	}
    
    	logger.info("New message");
    }

  3. #3
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Logger logs old messages

    Got it to work now in both examples. My problem was that I didn't realize that on recompilation the rotation count overwrites the first file rather than continue from the last file from the last compilation.

Similar Threads

  1. Emailing error logs
    By renolovexoxo in forum New To Java
    Replies: 1
    Last Post: 02-03-2014, 05:41 AM
  2. Log4j Grouping application logs
    By mhanda in forum New To Java
    Replies: 0
    Last Post: 03-09-2010, 12:19 AM
  3. how to get lucene logs
    By virendra in forum Lucene
    Replies: 0
    Last Post: 01-06-2010, 10:46 AM
  4. Maintaining logs of a chat session
    By vrk in forum AWT / Swing
    Replies: 1
    Last Post: 10-27-2009, 03:18 PM
  5. log4j - multiple logs with diffrent contents
    By ld09 in forum Java Applets
    Replies: 0
    Last Post: 10-08-2009, 11:36 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
  •