/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.jena.atlas.logging.java;

import java.text.MessageFormat ;
import java.util.Date ;
import java.util.logging.Formatter ;
import java.util.logging.LogManager ;
import java.util.logging.LogRecord ;

/** A pattern-driven log formatter.
 * inspired by Log4j's PatternLayout
 * Set a different output pattern with {@code .format}.
 * <p>
 * The default format is {@code "%5$tT %3$-5s %2$-20s :: %6$s\n"}.
 * <ul>
 * <li>"%5$tT.%5$tL %3$-5s %2$-20s :: %6$s\n" for milliseconds.
 * <li>"%tF %5$tT.%5$tL %3$-5s %2$-20s :: %6$s\n" for date
 * </ul>
 * <p>
 * The log message formatting call is:
 * <pre>
 *     String.format(format, 
 *                   loggerName,                        // 1
 *                   loggerNameShort,                   // 2
 *                   record.getLevel(),                 // 3
 *                   Thread.currentThread().getName(),  // 4
 *                   new Date(record.getMillis()),      // 5
 *                   formatted$) ;                      // 6
 * </pre>
 * where {@code formatted$} is the {@link LogRecord} message string after parameters have been processed. 
 */
public class TextFormatter extends Formatter
{
    // %tT (%5$tT) is %5$tH:%5$tM:%5$tS
    // %tF is 2008-11-22 "%tY-%tm-%td"
    private String format = "%5$tT %3$-5s %2$-20s :: %6$s\n" ;
    
    public TextFormatter() {
        LogManager manager = LogManager.getLogManager() ;
        String cname = getClass().getName();
        
        String fmt = manager.getProperty(cname+".format") ;
        if ( fmt != null ) {
            if ( ! fmt.endsWith("\n") )
                fmt = fmt + "\n" ;
            format = fmt ;
        }
    }
    
    @Override
    public String format(LogRecord record) {
        String loggerName = record.getLoggerName();
        if(loggerName == null) {
            loggerName = "root";
        }
        
        int i = loggerName.lastIndexOf('.') ; 
        String loggerNameShort = loggerName.substring(i+1) ;
            
        String formatted$ = record.getMessage() ;
        if ( record.getParameters() != null )
            formatted$ = MessageFormat.format(formatted$, record.getParameters()) ;
        
        return String.format(format, 
                             loggerName,                        // 1
                             loggerNameShort,                   // 2
                             record.getLevel(),                 // 3
                             Thread.currentThread().getName(),  // 4
                             new Date(record.getMillis()),      // 5
                             formatted$) ;                      // 6
    }
}

@@ -935,24 +935,57 @@
 Formatter ;%0A
+import java.util.logging.Level ;%0A
 import java.
@@ -1006,24 +1006,24 @@
 ogManager ;%0A
-
 import java.
@@ -1251,16 +1251,36 @@
 $s%5Cn%22%7D.%0A
+ * %3Cp%3E%0A * Examples:%0A
  * %3Cul%3E%0A
@@ -1639,34 +1639,34 @@
         
-record.getLevel(),
+levelOutputName,  
         
@@ -3028,32 +3028,141 @@
 s()) ;%0A        %0A
+        Level level = record.getLevel() ;%0A        String levelOutputName = levelOutputName(level) ;%0A        %0A
         return S
@@ -3354,26 +3354,26 @@
     
-record.getLevel(),
+levelOutputName,  
     
@@ -3528,16 +3528,16 @@
    // 5%0A
-
         
@@ -3602,11 +3602,937 @@
  6%0A    %7D
+%0A%0A    /** By default use slf4j name.%0A     *  When used with slf4j, this reconstructs the slf4j name.%0A     */%0A    %0A    protected String levelOutputName(Level level) %7B%0A        //    FINEST  -%3E TRACE%0A        //    FINER   -%3E DEBUG%0A        //    FINE    -%3E DEBUG%0A        //    CONFIG  -%3E INFO%0A        //    INFO    -%3E INFO%0A        //    WARNING -%3E WARN%0A        //    SEVERE  -%3E ERROR%0A        if ( Level.WARNING.equals(level) )%0A            return %22WARN%22 ;%0A        if ( Level.SEVERE.equals(level) )%0A            return %22ERROR%22 ;%0A        if ( Level.INFO.equals(level) )%0A            return %22INFO%22 ;%0A        if ( Level.CONFIG.equals(level) )   // Keep name.%0A            return %22CONFIG%22 ;%0A        if ( Level.FINE.equals(level) )%0A            return %22DEBUG%22 ;%0A        if ( Level.FINER.equals(level) )%0A            return %22DEBUG%22 ;%0A        if ( Level.FINEST.equals(level) )%0A            return %22TRACE%22 ;%0A        return level.getName() ;%0A    %7D
 %0A%7D%0A

Use log message names that correspond to slf4j names.