@Override public void start(String[] args) throws Exception { System.setProperty("java.awt.headless", "true"); jrds.JrdsLoggerConfiguration.initLog4J(); Properties pm = new Properties(); File propFile = new File(propFileName); if(propFile.isFile()) pm.load(new FileReader(propFile)); pm.setProperty("withjmx", "yes"); jrds.Configuration.configure(pm); jrds.JrdsLoggerConfiguration.configure(jrds.Configuration.get().getPropertiesManager()); if(jrds.Configuration.get().getPropertiesManager().withjmx) { doJmx(jrds.Configuration.get().getPropertiesManager()); Management.register(propFile); } // Make it wait on himself to wait forever try { Thread.currentThread().join(); System.out.print("joined"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } }
if(!isLogOwner()) return; configureLogger(logger, pm.loglevel);
JrdsLoggerConfiguration.configure(this); } catch (IOException e1) { logger.error("Unable to set log file to " + this.logfile + ": " + e1); JrdsLoggerConfiguration.setExternal();
public void start(String[] args) throws Exception { PropertiesManager pm = new PropertiesManager(new File(propFile)); jrds.JrdsLoggerConfiguration.configure(pm); System.getProperties().setProperty("java.awt.headless", "true"); System.getProperties().putAll(pm); HostsList hl = new HostsList(pm); logger.debug("Scanning dir"); for(Timer t: hl.getTimers()) { t.collectAll(); } }
public static void main(String[] args) throws IOException { jrds.JrdsLoggerConfiguration.initLog4J();
public static void catchLogger(String logger, org.apache.log4j.Level level) { // If not already configured, we filter it JrdsLoggerConfiguration.configureLogger(logger, level); java.util.logging.Logger jilogger = java.util.logging.Logger.getLogger(logger); jilogger.setUseParentHandlers(false); for(Handler h: jilogger.getHandlers()) { jilogger.removeHandler(h); } jilogger.addHandler(new JuliToLog4jHandler()); // Log4j will filter jilogger.setLevel(Level.ALL); }
/** * This method is used to join other logger branch with the jrds' one and * use same setting if it's not already defined * * @param logname the logger name * @param level the desired default level for this logger */ static public void configureLogger(String logname, Level level) { // Do nothing if jrds is not allowed to setup logs if(!isLogOwner()) return; Logger externallogger = LogManager.getLoggerRepository().exists(logname); // Change level only for new logger if(externallogger == null) { externallogger = Logger.getLogger(logname); externallogger.setLevel(level); } // Replace the appender, not optionally add it if(jrdsAppender != null) { Logger logger = Logger.getLogger(logname); Appender oldApp = logger.getAppender(jrdsAppender.getName()); if(oldApp != null) logger.removeAppender(oldApp); logger.addAppender(jrdsAppender); logger.setAdditivity(false); } // Keep the new logger name rootLoggers.add(logname); }
static public void configure() throws IOException { Locale.setDefault(new Locale("POSIX")); System.getProperties().setProperty("java.awt.headless", "true"); LogManager.resetConfiguration(); // resetConfiguration is not enough @SuppressWarnings("unchecked") List<Logger> loggers = (List<Logger>) Collections.list(LogManager.getCurrentLoggers()); for(Logger l: loggers) { l.removeAllAppenders(); l.setLevel(Level.OFF); } JrdsLoggerConfiguration.jrdsAppender = new ConsoleAppender(new org.apache.log4j.PatternLayout(JrdsLoggerConfiguration.DEFAULTLAYOUT), ConsoleAppender.SYSTEM_OUT); JrdsLoggerConfiguration.jrdsAppender.setName(JrdsLoggerConfiguration.APPENDERNAME); JrdsLoggerConfiguration.initLog4J(); }
/** * The method used to prepare a minimal set of logging configuration. This * should be used once. It does nothing if it detect that a appender already * exist for the logger <code>jrds</code>. The default logger is the system * error output and the default level is error. * * @throws IOException */ static public void initLog4J() throws IOException { // Do nothing if jrds is not allowed to setup logs if(!isLogOwner()) return; if(jrdsAppender == null) { jrdsAppender = new ConsoleAppender(new org.apache.log4j.SimpleLayout(), DEFAULTLOGFILE); jrdsAppender.setName(APPENDERNAME); } // Configure all the manager logger // Default level is debug, not a very good idea for(String loggerName: rootLoggers) { configureLogger(loggerName, Level.ERROR); } }
@Test public void testOutsideConfiguration() throws IOException { LogManager.getLoggerRepository().resetConfiguration(); ta.setName(TANAME); JrdsLoggerConfiguration.jrdsAppender = ta; Logger.getRootLogger().addAppender(ta); JrdsLoggerConfiguration.initLog4J(); Assert.assertNotNull(LogManager.getLoggerRepository().exists("jrds")); PropertiesManager pm = Tools.makePm(testFolder, "loglevel=error"); JrdsLoggerConfiguration.configure(pm); Logger l = Logger.getLogger("jrds"); l.error("A message"); l.debug("A debug message"); Assert.assertEquals(2, logs.size()); }
@Test public void testConfiguration() throws IOException { LogManager.getLoggerRepository().resetConfiguration(); JrdsLoggerConfiguration.initLog4J(); Logger jrdsLogger = LogManager.getLoggerRepository().exists("jrds"); Assert.assertNotNull(jrdsLogger); PropertiesManager pm = Tools.makePm(testFolder, "loglevel=warn"); JrdsLoggerConfiguration.configure(pm); Assert.assertEquals(pm.loglevel, jrdsLogger.getLevel()); }