Results 1 to 2 of 2
  1. #1
    Boomn4x4 is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default logging.Logger not working past Level.INFO

    I am using the java.util.logging.* classes to log messages to a log file. It works fine, until I try to log below Level.INFO. Level.CONFIG messages are not getting written to the file.

    Java Code:
    public class PCHLog {
    
    		static Logger fileLogger;
    		static Logger replyLogger;
    
    		private static final String storeNum = System.getenv("STORE");
    		String format = "yyyy-MM-dd-HH:mm:ss:SSS";
    
    		
    		//Constructor
    		private PCHLog(){
    			
    
    			fileLogger = Logger.getLogger("pch.file");
    
    			
    			FileHandler fh = null;
    			ConsoleHandler ch = new ConsoleHandler();
    
    			
    			File logdir = new File(MyPaths.LOG.path());
    			
    			if (!logdir.exists()){
    				logdir.mkdir();
    			}
    			
    			String filename = new String(logdir.toString() + "/pch.lg%g");
    
    			try{
    				fh = new FileHandler(filename, 10000000, 10, true);
    			}
    			catch (IOException ex){
    				ex.printStackTrace();
    			}
    			
    			fh.setLevel(Level.CONFIG);
    			fh.setFormatter(new SimpleFormatter());
    			fileLogger.addHandler(fh);
    			
    			//Only log severe message to console screen
    			ch.setLevel(Level.SEVERE);
    			fileLogger.addHandler(ch);
    			fileLogger.setUseParentHandlers(false);
    			
    
    		}
    		
    		public static void log(Rc c){
    			System.out.println(c.getLevel() + " " + c.getDesc());
    			getFileLogger().log(c.getLevel(), c.getDesc());	
    		}
    		
    		private static Logger getFileLogger(){
    		    if(fileLogger == null){
    		        new PCHLog();
    		    }
    		    return fileLogger;
    		}
    		
    		}
    When I run, the level displays to the screen as CONFIG, but it doesn't log to the file. Any ideas?

  2. #2
    Boomn4x4 is offline Member
    Join Date
    Nov 2011
    Posts
    18
    Rep Power
    0

    Default Re: logging.Logger not working past Level.INFO

    Figured it out. The Logger object has its own logging level which trumps the file handler's logging level. The Logger object defaults to INFO, so even though I'm setting the file handler to CONFIG, its using the INFO.

    The fix is to change:
    Java Code:
    private static Logger getFileLogger(){
                if(fileLogger == null){
                    new PCHLog();
                }
                return fileLogger;
            }
    to

    Java Code:
    private static Logger getFileLogger(){
                if(fileLogger == null){
                    new PCHLog();
                }
                fileLogger.setLevel(Level.ALL);
                return fileLogger;
            }

Similar Threads

  1. Top level and member level
    By Differintegral in forum New To Java
    Replies: 1
    Last Post: 07-30-2010, 03:50 AM
  2. [SOLVED] Class-level vs Object-level method()
    By mfaizan24 in forum New To Java
    Replies: 7
    Last Post: 06-23-2009, 09:18 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
  •