/*******************************************************************************
 * Copyright 2011 John Casey
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.commonjava.util.logging;

import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Category;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.spi.Configurator;
import org.apache.log4j.spi.LoggerRepository;

public class Log4jUtil
{

    public static void configure( final Level level )
    {
        configure( level, Collections.<String, Level> emptyMap() );
    }

    // TODO: Level map doesn't work (yet)
    public static void configure( final Level level, final Map<String, Level> customLevels )
    {
        final Configurator log4jConfigurator = new Configurator()
        {
            @Override
            public void doConfigure( final URL notUsed, final LoggerRepository repo )
            {
                final ConsoleAppender cAppender = new ConsoleAppender( new SimpleLayout() );
                cAppender.setThreshold( Level.ALL );

                repo.setThreshold( level );
                repo.getRootLogger()
                    .removeAllAppenders();
                repo.getRootLogger()
                    .setLevel( level );
                repo.getRootLogger()
                    .addAppender( cAppender );

                @SuppressWarnings( "unchecked" )
                final List<Logger> loggers = Collections.list( repo.getCurrentLoggers() );

                for ( final Logger logger : loggers )
                {
                    logger.setLevel( level );
                }

                @SuppressWarnings( "unchecked" )
                final List<Category> cats = Collections.list( repo.getCurrentCategories() );
                for ( final Category cat : cats )
                {
                    cat.setLevel( level );
                }

                for ( final Map.Entry<String, Level> customEntry : customLevels.entrySet() )
                {
                    final String key = customEntry.getKey();
                    final Logger logger = repo.getLogger( key );
                    logger.setLevel( customEntry.getValue() );
                }
            }
        };

        log4jConfigurator.doConfigure( null, LogManager.getLoggerRepository() );
    }

}

@@ -858,30 +858,8 @@
 ist;
-%0Aimport java.util.Map;
 %0A%0Aim
@@ -927,24 +927,56 @@
 leAppender;%0A
+import org.apache.log4j.Layout;%0A
 import org.a
@@ -1086,22 +1086,23 @@
 e.log4j.
-Simple
+Pattern
 Layout;%0A
@@ -1306,46 +1306,25 @@
 el, 
-Collections.%3CString, Level%3E emptyMap()
+%22%255p %5B%25l%5D - %25m%25n%22
  );%0A
@@ -1435,39 +1435,22 @@
 nal 
-Map%3C
 String
-, Level%3E customLevels
+ pattern
  )%0A 
@@ -1644,32 +1644,100 @@
 )%0A            %7B%0A
+                final Layout layout = new PatternLayout( pattern );%0A
                 
@@ -1795,26 +1795,14 @@
 er( 
-new SimpleL
+l
 ayout
-()
  );%0A
@@ -2693,327 +2693,8 @@
    %7D
-%0A%0A                for ( final Map.Entry%3CString, Level%3E customEntry : customLevels.entrySet() )%0A                %7B%0A                    final String key = customEntry.getKey();%0A                    final Logger logger = repo.getLogger( key );%0A                    logger.setLevel( customEntry.getValue() );%0A                %7D
 %0A   

switch to pattern appender, and allow configuration of the pattern used.