示例#1
0
 public void setLevel(final Level level) {
   logger.setLevel(org.apache.logging.log4j.Level.toLevel(level.levelStr));
 }
示例#2
0
 public void setPriority(final Priority priority) {
   logger.setLevel(org.apache.logging.log4j.Level.toLevel(priority.levelStr));
 }
  public org.slf4j.Logger getLogger(String name) {
    name = StringUtil2.replace(name.trim(), ' ', "_");
    org.slf4j.Logger result = map.get(name);
    if (result != null) return result;

    try {
      String fileName = dir + "/" + name + ".log";
      String fileNamePattern = dir + "/" + name + "%i.log";

      // create logger in log4j2
      // TODO: There are Builders that make this logger creation less awkward.
      Configuration config =
          new NullConfiguration(); // LOOK: Why are we using this? Why not DefaultConfiguration?
      PatternLayout layout =
          PatternLayout.createLayout(
              "%d{yyyy-MM-dd'T'HH:mm:ss.SSS Z} %-5p - %m%n", // String pattern
              config, // Configuration config
              null, // RegexReplacement replace
              null, // Charset charset
              true, // boolean alwaysWriteExceptions
              false, // boolean noConsoleNoAnsi
              null, // String header
              null // String footer
              );

      RollingFileAppender app =
          RollingFileAppender.createAppender(
              fileName, // String fileName
              fileNamePattern, // String filePattern
              "true", // String append
              name, // String name
              "true", // String bufferedIO
              null, // String bufferSizeStr
              "true", // String immediateFlush
              SizeBasedTriggeringPolicy.createPolicy(
                  Long.toString(maxSize)), // TriggeringPolicy policy
              DefaultRolloverStrategy.createStrategy( // RolloverStrategy strategy
                  Integer.toString(maxBackups), // String max
                  "1", // String min
                  "max", // String fileIndex
                  null, // String compressionLevelStr
                  config // Configuration config
                  ),
              layout, // Layout<? extends Serializable> layout
              null, // Filter filter
              "true", // String ignore
              "false", // String advertise
              null, // String advertiseURI
              config); // Configuration config

      app.start();

      /*LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
      Configuration conf = ctx.getConfiguration();
      LoggerConfig lconf = conf.getLoggerConfig(name);
      lconf.setAdditive(false); // otherwise, it also gets sent to root logger (threddsServlet.log)
      lconf.setLevel(level);
      lconf.addAppender(app, level, null);
      ctx.updateLoggers(conf);  */

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

      startupLog.info("LoggerFactorySpecial add logger= {} file= {}", name, fileName);

      result = org.slf4j.LoggerFactory.getLogger(name); // get wrapper in slf4j
      map.put(name, result);
      return result;

    } catch (Throwable ioe) {
      startupLog.error("LoggerFactorySpecial failed on " + name, ioe);

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