コード例 #1
0
 /**
  * Not a real test, just make sure we can compile access to the typed manager.
  *
  * @throws IOException
  */
 @Test
 public void testAccessManagerWithStrings() throws IOException {
   try (final LoggerContext ctx = LoggerContext.getContext(false)) {
     final Configuration config = ctx.getConfiguration();
     final File file = File.createTempFile("RollingFileAppenderAccessTest", ".tmp");
     file.deleteOnExit();
     final RollingFileAppender appender =
         RollingFileAppender.createAppender(
             file.getCanonicalPath(),
             "FilePattern",
             null,
             "Name",
             null,
             null,
             null,
             OnStartupTriggeringPolicy.createPolicy(1),
             null,
             null,
             null,
             null,
             null,
             null,
             config);
     final RollingFileManager manager = appender.getManager();
     // Since the RolloverStrategy and TriggeringPolicy are immutable, we could also use generics
     // to type their
     // access.
     Assert.assertNotNull(manager.getRolloverStrategy());
     Assert.assertNotNull(manager.getTriggeringPolicy());
   }
 }
コード例 #2
0
  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);
    }
  }