Exemplo n.º 1
0
 private synchronized void configurar() {
   if (configured) {
     //			System.err.println("configurar: configured == true");
     return;
   }
   //		System.err.println("configurar: configured == false");
   configured = true;
   // Si existe un log4j.xml / log4j.properties usarlo, si no configurar en duro
   Logger root = Logger.getRootLogger();
   if (root.getAllAppenders().hasMoreElements()) {
     System.out.println("PLoger.configurar: log4j.xml/log4j.properties");
   } else {
     // Configuracion desktop y autómatas
     root.setLevel(Level.INFO);
     Appender appender = null;
     String iniciarEn = null;
     if (System.getProperty("iniciarEn") != null) { // autómatas
       if (new File(System.getProperty("iniciarEn")).exists()) {
         iniciarEn = System.getProperty("iniciarEn");
       } else {
         System.out.println(
             System.getProperty("iniciarEn")
                 + " no existe, se va a utilizar el directorio corriente.");
       }
       try {
         String pattern = System.getProperty("PLogger.pattern", "%-4r %d [%t] %-5p %c %x - %m%n");
         String logFile = System.getProperty("PLogger.logFile", "automata.log");
         DailyRollingFileAppender drfa =
             new DailyRollingFileAppender(
                 new PatternLayout(pattern),
                 (iniciarEn == null ? "" : (iniciarEn + File.separator)) + logFile,
                 "'.'yyyy-MM-dd");
         drfa.setAppend(true);
         appender = drfa;
       } catch (IOException e) {
         e.printStackTrace();
       }
     } else { // desktops
       appender = new ConsoleAppender();
     }
     if (appender != null) {
       root.addAppender(appender);
     }
   }
 }
Exemplo n.º 2
0
 private static Appender buildAppender(String name, String fileName, String pattern) {
   DailyRollingFileAppender appender = new DailyRollingFileAppender();
   appender.setName(name);
   appender.setAppend(true);
   appender.setEncoding("GBK");
   appender.setLayout(new PatternLayout(pattern));
   appender.setFile(new File(getLogPath(), fileName).getAbsolutePath());
   appender.activateOptions(); // 很重要,否则原有日志内容会被清空
   return appender;
 }
Exemplo n.º 3
0
  public static Logger getLogger(String file) {

    Logger log = logCache.get(file);
    if (null != log) {
      return log;
    } else {
      log = Logger.getLogger(file);
      DailyRollingFileAppender appender = new DailyRollingFileAppender();
      Layout layout = new PatternLayout();
      appender.setFile(file);
      appender.setEncoding("UTF-8");
      appender.activateOptions();
      appender.setDatePattern("'.'yyyy-MM-dd");
      appender.setLayout(layout);
      log.addAppender(appender);
      logCache.put(file, log);
    }

    return log;
  }