package thredds.util;

import org.apache.log4j.*;
import org.slf4j.Logger;
import ucar.nc2.util.log.LoggerFactory;
import ucar.unidata.util.StringUtil2;

import java.io.IOException;

/**
 * A LoggerFactory that uses log4j to create and configure a special RollingFileAppender
 * specific to this name.
 * used by InvDatasetFeatureCollection to create a log for each feature collection.
 *
 * @author caron
 * @since 3/27/13
 */
public class LoggerFactorySpecial implements LoggerFactory {
  private String dir = ".";
  private Level level = Level.INFO;

  public LoggerFactorySpecial(Level lev) {
    dir = System.getProperty("tds.log.dir");
    level = lev;
  }

  @Override
  public Logger getLogger(String name) {
    name = StringUtil2.replace(name.trim(), ' ', "_");
    try {
      //create logger in log4j
      Layout layout = new PatternLayout("%d{yyyy-MM-dd'T'HH:mm:ss.SSS Z} %-5p - %m%n");

      String loggerName = name + ".log";
      RollingFileAppender app = new RollingFileAppender(layout, loggerName);
      app.setMaxBackupIndex(5);            // 5 rollovers
      app.setMaximumFileSize(1000 * 1000); // 1 Mb
      app.setFile(dir + "/" + loggerName);
      app.activateOptions();

      org.apache.log4j.Logger log4j = LogManager.getLogger(name);
      log4j.addAppender(app);
      log4j.setLevel(level);
      log4j.setAdditivity(false); // otherwise, it also gets sent to root logger (threddsServlet.log)

      // get wrapper in slf4j
      return org.slf4j.LoggerFactory.getLogger(name);

    } catch (IOException ioe) {

      // standard slf4j - rely on external configuration
      return org.slf4j.LoggerFactory.getLogger(name);
    }
  }
}

@@ -482,16 +482,116 @@
 ctory %7B%0A
+  static private org.slf4j.Logger startupLog = org.slf4j.LoggerFactory.getLogger(%22serverStartup%22);%0A%0A
   privat
@@ -610,16 +610,16 @@
  = %22.%22;%0A
-
   privat
@@ -861,24 +861,167 @@
  ' ', %22_%22);%0A
+    String fileName =  dir + %22/%22 + name + %22.log%22;%0A    startupLog.info(%22LoggerFactorySpecial try to add logger= %7B%7D file= %7B%7D%22, name, fileName);%0A%0A
     try %7B%0A  
@@ -1142,49 +1142,8 @@
 );%0A%0A
-      String loggerName = name + %22.log%22;%0A
     
@@ -1202,22 +1202,20 @@
 layout, 
-logger
+file
 Name);%0A 
@@ -1344,26 +1344,12 @@
 ile(
-dir + %22/%22 + logger
+file
 Name
@@ -1609,16 +1609,82 @@
 t.log)%0A%0A
+      startupLog.info(%22LoggerFactorySpecial added ok %7B%7D%22, name);%0A%0A
       //
@@ -1756,24 +1756,24 @@
 ger(name);%0A%0A
-
     %7D catch 
@@ -1791,16 +1791,85 @@
 n ioe) %7B
+%0A      startupLog.error(%22LoggerFactorySpecial failed on %22+name, ioe);
 %0A%0A      

fix tds featureCollection logging