Java Code Examples for java.util.logging.FileHandler#setLevel()

The following examples show how to use java.util.logging.FileHandler#setLevel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClientUpdater.java    From Open-Lowcode with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * tries to setup logs. Does not throw an exception even if there is an issue
 * (typically log folder missing for logs), so that upgrade can continue even if
 * issue with logs
 */
public static void attemptToSetupLogs() {
	try {
		Logger mainlogger = Logger.getLogger("");
		for (int i = 0; i < mainlogger.getHandlers().length; i++) {
			mainlogger.removeHandler(mainlogger.getHandlers()[i]);
		}
		logfilehandler = new FileHandler("./log/OpenLowcodeUpdater%g.log", 10000000, 1000, true);
		logfilehandler.setLevel(Level.FINEST);
		logfilehandler.setFormatter(new FileFormatter(true));

		Logger openlowcode = Logger.getLogger("org.openlowcode");
		openlowcode.addHandler(logfilehandler);
		openlowcode.setUseParentHandlers(false);
		openlowcode.setLevel(Level.ALL);

	} catch (Exception e) {
		logsetupwarning = "Exception while setting logs: " + e.getClass().getName() + " - " + e.getMessage();
	}
}
 
Example 2
Source File: GemFireXDDataExtractorImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void configureLogger() throws SecurityException, IOException {
  if (logger == null) {
    logger = Logger.getLogger(LOGGER_NAME);
  }
  logger.setUseParentHandlers(false);
  logFileHandler = new FileHandler(logFilePath);
  logFileHandler.setFormatter(new SimpleFormatter());
  Level logLevel = Level.INFO;
  try {
    logLevel = Level.parse(logLevelString);
  } catch (IllegalArgumentException e) {
    logInfo("Unrecognized log level :" + logLevelString + " defaulting to :" + logLevel);
  }
  logFileHandler.setLevel(logLevel);
  logger.addHandler(logFileHandler);
}
 
Example 3
Source File: LogService.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
private LogService(Logger logger) {
	super(logger);

	// setup a log file if the execution mode can access the filesystem (e.g. not for RA)
	// we want our logfile to look the same regardless of any user settings, so ignore
	// a possible user logging property config file
	if (RapidMiner.getExecutionMode().canAccessFilesystem()) {
		try {
			FileHandler logFileHandler = new FileHandler(FileSystemService.getLogFile().getAbsolutePath(), false);
			logFileHandler.setLevel(Level.ALL);
			logFileHandler.setFormatter(new SimpleFormatter());
			LogService.getRoot().addHandler(logFileHandler);
		} catch (IOException e) {
			LogService.getRoot().log(Level.WARNING, "com.rapidminer.logservice.logfile.failed_to_init", e.getMessage());
		}
	}
}
 
Example 4
Source File: RemoteWriteTest.java    From noa-libre with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Main entry point for the OpenOffice.org Remote Write Test.
 *
 * @param args arguments of the test
 *
 * @author Andreas Bröker
 * @date 17.08.2006
 */
public static void main(String[] args) {
  if(args.length < 2) {
    System.out.println("NOA Remote Write Test");
    System.out.println("-------------------");
    System.out.println("Usage:");
    System.out.println("RemoteWriteTest <host> <port>");
  }
  else { 
    LogManager.getLogManager().reset();
    ConsoleHandler consoleHandler = new ConsoleHandler();
    consoleHandler.setLevel(Level.FINEST);
    LOGGER.addHandler(consoleHandler);
    LOGGER.setLevel(Level.FINEST);
    
    try {
      FileHandler fileHandler = new FileHandler("log.xml");
      fileHandler.setLevel(Level.FINEST);
      LOGGER.addHandler(fileHandler);
    }
    catch (Throwable throwable) {
    }
    RemoteWriteTest remoteWriteTest = new RemoteWriteTest();
    remoteWriteTest.test(args[0], args[1]);
  }
}
 
Example 5
Source File: GemFireXDDataExtractorImpl.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void configureLogger() throws SecurityException, IOException {
  if (logger == null) {
    logger = Logger.getLogger(LOGGER_NAME);
  }
  logger.setUseParentHandlers(false);
  logFileHandler = new FileHandler(logFilePath);
  logFileHandler.setFormatter(new SimpleFormatter());
  Level logLevel = Level.INFO;
  try {
    logLevel = Level.parse(logLevelString);
  } catch (IllegalArgumentException e) {
    logInfo("Unrecognized log level :" + logLevelString + " defaulting to :" + logLevel);
  }
  logFileHandler.setLevel(logLevel);
  logger.addHandler(logFileHandler);
}
 
Example 6
Source File: PMController.java    From uima-uimaj with Apache License 2.0 6 votes vote down vote up
/**
 * Enables/disables PM log file. By default, the log file is disabled.
 * 
 * @param enable
 *          if <code>true</code>, the log file is enabled, otherwise it is disabled.
 */
public static void setLogFileEnabled(boolean enable) {
  if (enable) {
    Handler[] handlers = getLogger().getHandlers();
    if (handlers == null || handlers.length == 0) {
      // add default file handler
      try {
        FileHandler fileHandler = new FileHandler(LOG_FILE, false);
        fileHandler.setLevel(Level.ALL);
        fileHandler.setFormatter(new PMLogFormatter());
        getLogger().addHandler(fileHandler);
      } catch (Throwable err) {
        System.err.println("Error initializing log file " + PMController.class.getName() + ": "
                + err.toString());
      }
    }
  }
  __logFileEnabled = enable;
}
 
Example 7
Source File: LogsManager.java    From RepoSense with MIT License 5 votes vote down vote up
/**
 * Creates a {@code FileHandler} for the log file.
 *
 * @throws IOException if there are problems opening the file.
 */
private static FileHandler createFileHandler() throws IOException {
    Path path = logFolderLocation.resolve(LOG_FOLDER_NAME).resolve(LOG_FILE_NAME);
    FileHandler fileHandler = new FileHandler(path.toString(), MAX_FILE_SIZE_IN_BYTES, FILE_COUNT, true);
    fileHandler.setFormatter(new SimpleFormatter());
    fileHandler.setLevel(currentFileLogLevel);
    return fileHandler;
}
 
Example 8
Source File: OfficeBeanTest.java    From noa-libre with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Main entry point for the OpenOffice.org Bean Test.
 *
 * @param args arguments of the test
 *
 * @author Andreas Bröker
 * @date 21.05.2006
 */
public static void main(String[] args) throws OfficeApplicationException {

   LogManager.getLogManager().reset();
   ConsoleHandler consoleHandler = new ConsoleHandler();
   consoleHandler.setLevel(Level.FINEST);
   LOGGER.addHandler(consoleHandler);
   LOGGER.setLevel(Level.FINEST);

   try {
      FileHandler fileHandler = new FileHandler("log.xml");
      fileHandler.setLevel(Level.FINEST);
      LOGGER.addHandler(fileHandler);
   } catch (Throwable throwable) {
   }
   OfficeBeanTest testOfficeBean = new OfficeBeanTest();

   if (args.length == 0) {
      testOfficeBean.test(null);
   }else if (args.length == 1) {
      testOfficeBean.test(args[0]);
   }else if(args.length==4){
      testOfficeBean.remoteTestPDF(new File(args[2]), new File(args[3]), args[0], Integer.valueOf(args[1]));
   }else{
      System.out.println("usage:\nOfficeBeanTest host port source-odt target-pdf\nOfficeBeanTest officeHome");
   }
}
 
Example 9
Source File: OgarServer.java    From Ogar2-Server with GNU General Public License v3.0 5 votes vote down vote up
private void setupLogging() {
    log.setUseParentHandlers(false);

    LogFormatter formatter = new LogFormatter();

    ConsoleHandler ch = new ConsoleHandler();
    ch.setFormatter(formatter);
    if (isDebugging()) {
        log.setLevel(Level.FINEST);
        ch.setLevel(Level.FINEST);
    } else {
        log.setLevel(Level.INFO);
        ch.setLevel(Level.INFO);
    }
    log.addHandler(ch);

    try {
        FileHandler fh = new FileHandler("server.log");
        fh.setFormatter(formatter);
        if (isDebugging()) {
            fh.setLevel(Level.FINEST);
        } else {
            ch.setLevel(Level.INFO);
        }
        log.addHandler(fh);
    } catch (IOException ex) {
        log.log(Level.SEVERE, "Error while adding FileHandler to logger. Logs will not be output to a file.", ex);
    }

}
 
Example 10
Source File: TestRequestResponseLogging.java    From gitlab4j-api with MIT License 5 votes vote down vote up
@BeforeClass
public static void setup() throws Exception {

    String problems = "";

    if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
        problems += "TEST_HOST_URL cannot be empty\n";
    }

    if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
        problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
    }

    if (problems.isEmpty()) {

        tempLoggingFile = tempFolder.newFile("test-loging.log");

        loggingHandler = new FileHandler(tempLoggingFile.getAbsolutePath());
        loggingHandler.setFormatter(new SimpleFormatter());
        logger = Logger.getLogger(TestRequestResponseLogging.class.getName());
        logger.setUseParentHandlers(false);
        logger.addHandler(loggingHandler);
        loggingHandler.setLevel(Level.ALL);
        logger.setLevel(Level.ALL);

        gitLabApiWithEntityLogging = new GitLabApi(TEST_HOST_URL, TEST_PRIVATE_TOKEN);
        gitLabApiWithEntityLogging.enableRequestResponseLogging(logger, Level.INFO, 100);
        gitLabApiNoEntityLogging = new GitLabApi(TEST_HOST_URL, TEST_PRIVATE_TOKEN);
        gitLabApiNoEntityLogging.enableRequestResponseLogging(logger, Level.INFO);
        gitLabApiNoMaskingLogging = new GitLabApi(TEST_HOST_URL, TEST_PRIVATE_TOKEN);
        gitLabApiNoMaskingLogging.enableRequestResponseLogging(logger, Level.INFO, 100, new ArrayList<String>());

        gitLabApiWithoutLogging = new GitLabApi(TEST_HOST_URL, TEST_PRIVATE_TOKEN);

    } else {
        System.err.print(problems);
    }
}
 
Example 11
Source File: ResourceUsageAnalyzer.java    From bazel with Apache License 2.0 5 votes vote down vote up
public ResourceUsageAnalyzer(
    Set<String> resourcePackages,
    @NonNull Path rTxt,
    @NonNull Path classes,
    @NonNull Path manifest,
    @Nullable Path mapping,
    @NonNull Path resources,
    @Nullable Path logFile)
    throws DOMException, ParserConfigurationException {
  this.model = new ResourceShrinkerUsageModel();
  this.resourcePackages = resourcePackages;
  this.rTxt = rTxt;
  this.proguardMapping = mapping;
  this.classes = classes;
  this.mergedManifest = manifest;
  this.mergedResourceDir = resources;

  this.logger = Logger.getLogger(getClass().getName());
  logger.setLevel(Level.FINE);
  if (logFile != null) {
    try {
      FileHandler fileHandler = new FileHandler(logFile.toString());
      fileHandler.setLevel(Level.FINE);
      fileHandler.setFormatter(
          new Formatter() {
            @Override
            public String format(LogRecord record) {
              return record.getMessage() + "\n";
            }
          });
      logger.addHandler(fileHandler);
    } catch (SecurityException | IOException e) {
      logger.warning(String.format("Unable to open '%s' to write log.", logFile));
    }
  }
}
 
Example 12
Source File: ClientMainFrame.java    From Open-Lowcode with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Initiate console log and file log
 * 
 * @throws IOException if anything bad happens creating log giles
 */
public void initiateLog() throws IOException {
	Logger mainlogger = Logger.getLogger("");

	for (int i = 0; i < mainlogger.getHandlers().length; i++) {
		mainlogger.removeHandler(mainlogger.getHandlers()[i]);
	}
	if (!nolog) {
		ConsoleHandler consolehandler = new ConsoleHandler();
		consolehandler.setFormatter(new ConsoleFormatter());
		consolehandler.setLevel(Level.ALL);
		consolefilter = new OpenLowcodeLogFilter(Level.FINER, "Console Filter", consolehandler);
		consolehandler.setFilter(consolefilter);
		mainlogger.addHandler(consolehandler);
		File file = new File("./log/");
		if (!file.exists()) {
			boolean result = file.mkdir();
			if (!result)
				throw new RuntimeException("Trying to create log folder " + file.getPath() + ", does not work");
		}
		System.err.println("log folder = " + file.getAbsolutePath());
		FileHandler logfilehandler = new FileHandler("./log/OLcClient%g.log", 10000000, 1000, true);
		logfilefilter = new OpenLowcodeLogFilter(Level.FINER, "Log File Filter", logfilehandler);
		logfilehandler.setFilter(logfilefilter);

		logfilehandler.setLevel(Level.ALL);
		logfilehandler.setFormatter(new FileFormatter(true));
		mainlogger.addHandler(logfilehandler);

		mainlogger.setUseParentHandlers(false);
		
		Logger rootlogger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
		for (int i = 0; i < rootlogger.getHandlers().length; i++) {
			rootlogger.removeHandler(rootlogger.getHandlers()[i]);
		}
		rootlogger.addHandler(logfilehandler);
		rootlogger.setLevel(Level.ALL);

		rootlogger.addHandler(consolehandler);
		rootlogger.setUseParentHandlers(false);
	}
}
 
Example 13
Source File: OLcServerShutdown.java    From Open-Lowcode with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @param port
 */
public OLcServerShutdown(int port) {
	Logger mainlogger = Logger.getLogger("");
	try {
		for (int i = 0; i < mainlogger.getHandlers().length; i++) {
			mainlogger.removeHandler(mainlogger.getHandlers()[i]);
		}
		ConsoleHandler consolehandler = new ConsoleHandler();
		consolehandler.setFormatter(new ConsoleFormatter());
		consolehandler.setLevel(Level.FINER);
		mainlogger.addHandler(consolehandler);
		File file = new File("./log/");
		System.err.println("log folder = " + file.getAbsolutePath());
		FileHandler logfilehandler = new FileHandler("./log/OpenLowcodeClient%g.log", 10000000, 1000, true);
		logfilehandler.setLevel(Level.FINEST);
		logfilehandler.setFormatter(new FileFormatter(true));
		mainlogger.addHandler(logfilehandler);
		mainlogger.setUseParentHandlers(false);
		// ------------------------------------------------------------------------------
		logger.severe("Connection to Open-Lowcode Server on localhost port " + port + " to send shutdown message");
		Socket clientsocket = new Socket("localhost", port);
		MessageSimpleReader reader = new MessageSimpleReader(
				new BufferedReader(new InputStreamReader(clientsocket.getInputStream()), 9090));
		MessageBufferedWriter writer = new MessageBufferedWriter(
				new BufferedWriter(new OutputStreamWriter(clientsocket.getOutputStream())), false);
		@SuppressWarnings("unused")
		AESCommunicator aescommunicator = AESCommunicator.performServerHandshake(reader, writer);
		writer.startNewMessage();
		writer.startStructure("SHUTDOWN");
		writer.endStructure("SHUTDOWN");
		writer.endMessage();
		writer.flushMessage();
		logger.severe("Succesfully sent shutdown message to server on port " + port);
		reader.returnNextMessageStart();
		reader.returnNextStartStructure("SHUTDOWNOK");
		reader.returnNextEndStructure("SHUTDOWNOK");
		reader.returnNextEndMessage();

		logger.severe(
				"Got last message from server before shutdown, all connections are stopped. Server will stop in less than 50ms");
		reader.close();
		writer.close();
		clientsocket.close();
	} catch (Exception e) {
		logger.severe("could not execute correctly shutdown script: " + e.getMessage());
		for (int i = 0; i < e.getStackTrace().length; i++) {
			logger.severe(e.getStackTrace()[i].toString());
		}
	}
}