List of usage examples for java.util.logging SimpleFormatter SimpleFormatter
SimpleFormatter
From source file:com.nilostep.xlsql.database.xlInstance.java
private xlInstance(String cfg) throws xlException { logger = Logger.getLogger(this.getClass().getName()); instance = this; try {//from ww w . j a v a 2s. c om PropertiesConfiguration config = new PropertiesConfiguration(); config.load(this.getClass().getResourceAsStream(cfg + ".properties")); this.config = config; engine = config.getString("general.engine"); logger.info("Configuration engine: " + engine + " loaded"); } catch (ConfigurationException e) { e.printStackTrace(); throw new xlException(e.getMessage()); } try { boolean append = true; FileHandler loghandler = new FileHandler(getLog(), append); loghandler.setFormatter(new SimpleFormatter()); logger.addHandler(loghandler); } catch (IOException e) { throw new xlException("error while creating logfile"); } logger.info("Instance created with engine " + getEngine()); }
From source file:org.openstreetmap.josm.data.cache.JCSCacheManager.java
@SuppressWarnings("resource") private static void initialize() throws IOException { File cacheDir = new File(Main.pref.getCacheDirectory(), "jcs"); if (!cacheDir.exists() && !cacheDir.mkdirs()) throw new IOException("Cannot access cache directory"); File cacheDirLockPath = new File(cacheDir, ".lock"); if (!cacheDirLockPath.exists() && !cacheDirLockPath.createNewFile()) { LOG.log(Level.WARNING, "Cannot create cache dir lock file"); }/*from w w w . j a v a 2 s . c om*/ cacheDirLock = new FileOutputStream(cacheDirLockPath).getChannel().tryLock(); if (cacheDirLock == null) LOG.log(Level.WARNING, "Cannot lock cache directory. Will not use disk cache"); // raising logging level gives ~500x performance gain // http://westsworld.dk/blog/2008/01/jcs-and-performance/ final Logger jcsLog = Logger.getLogger("org.apache.commons.jcs"); jcsLog.setLevel(Level.INFO); jcsLog.setUseParentHandlers(false); // we need a separate handler from Main's, as we downgrade LEVEL.INFO to DEBUG level Arrays.stream(jcsLog.getHandlers()).forEach(jcsLog::removeHandler); jcsLog.addHandler(new Handler() { final SimpleFormatter formatter = new SimpleFormatter(); @Override public void publish(LogRecord record) { String msg = formatter.formatMessage(record); if (record.getLevel().intValue() >= Level.SEVERE.intValue()) { Logging.error(msg); } else if (record.getLevel().intValue() >= Level.WARNING.intValue()) { Logging.warn(msg); // downgrade INFO level to debug, as JCS is too verbose at INFO level } else if (record.getLevel().intValue() >= Level.INFO.intValue()) { Logging.debug(msg); } else { Logging.trace(msg); } } @Override public void flush() { // nothing to be done on flush } @Override public void close() { // nothing to be done on close } }); // this could be moved to external file Properties props = new Properties(); // these are default common to all cache regions // use of auxiliary cache and sizing of the caches is done with giving proper geCache(...) params // CHECKSTYLE.OFF: SingleSpaceSeparator props.setProperty("jcs.default.cacheattributes", CompositeCacheAttributes.class.getCanonicalName()); props.setProperty("jcs.default.cacheattributes.MaxObjects", DEFAULT_MAX_OBJECTS_IN_MEMORY.get().toString()); props.setProperty("jcs.default.cacheattributes.UseMemoryShrinker", "true"); props.setProperty("jcs.default.cacheattributes.DiskUsagePatternName", "UPDATE"); // store elements on disk on put props.setProperty("jcs.default.elementattributes", CacheEntryAttributes.class.getCanonicalName()); props.setProperty("jcs.default.elementattributes.IsEternal", "false"); props.setProperty("jcs.default.elementattributes.MaxLife", Long.toString(maxObjectTTL)); props.setProperty("jcs.default.elementattributes.IdleTime", Long.toString(maxObjectTTL)); props.setProperty("jcs.default.elementattributes.IsSpool", "true"); // CHECKSTYLE.ON: SingleSpaceSeparator CompositeCacheManager cm = CompositeCacheManager.getUnconfiguredInstance(); cm.configure(props); cacheManager = cm; }
From source file:BSxSB.Controllers.StudentController.java
@RequestMapping(value = "/register", method = RequestMethod.POST) public String register(Model model, @RequestParam(value = "firstName") String firstName, @RequestParam(value = "lastName") String lastName, @RequestParam(value = "email") String email, @RequestParam(value = "password") String password, @RequestParam(value = "school") String school) { try {/* w w w. j a v a2 s. co m*/ //Initialize the file that the logger writes to. Handler handler = new FileHandler("%tBSxSBStudentAccount.log", true); handler.setFormatter(new SimpleFormatter()); logger.addHandler(handler); List<Schools> schools = SchoolDAO.allSchools(); if (schools != null) { model.addAttribute("school", schools); } Students student = StudentDAO.getStudent(email); if (firstName.isEmpty() || lastName.isEmpty() || email.isEmpty() || password.isEmpty()) { model.addAttribute("fillout", "Please fill out all Required Fields"); logger.info("A field was not filled."); } else if (student != null) { model.addAttribute("taken", "The email address is taken"); logger.info("Email address was taken."); } else { StudentDAO.register(firstName, lastName, email, password, school); model.addAttribute("registered", "You have been successfully registered. Please Login"); logger.info("Successfully registered an account with email " + email); } handler.close(); logger.removeHandler(handler); } catch (IOException ex) { logger.log(Level.SEVERE, null, ex); } catch (SecurityException ex) { logger.log(Level.SEVERE, null, ex); } return "index"; }
From source file:de.teamgrit.grit.entities.Controller.java
/** * Instantiates a new controller./* w w w . j a v a2s .c o m*/ */ private Controller() { m_courses = new HashMap<>(); m_connections = new HashMap<>(); try { // create log directory if it does not exist if (!Paths.get("log").toFile().exists()) { File logFolder = new File("log"); logFolder.mkdir(); } // Tell the logger to log there. FileHandler fh = new FileHandler(Paths.get("log", "system.log").toString(), true); LOGGER.addHandler(fh); // log in human readable format fh.setFormatter(new SimpleFormatter()); // do not log to console LOGGER.setUseParentHandlers(false); } catch (SecurityException | IOException e) { System.err.println("Could not start logger on log/system.log: " + e.getMessage()); e.printStackTrace(); } try { m_config = new Configuration(CONFIG_LOCATION.toFile()); } catch (ConfigurationException | FileNotFoundException e) { LOGGER.severe("Error while creating Controller: " + e.getMessage()); } }
From source file:eu.abc4trust.abce.pertubationtests.tud.section2.PA_II_2_2_1malformedCEuri.java
@BeforeClass public static void setupLogger() throws SecurityException, IOException { Handler fh = new FileHandler("PA-Section-2.2.1-malformedCE.log"); SimpleFormatter simpleFormatter = new SimpleFormatter(); fh.setFormatter(simpleFormatter);//from ww w .j a v a2s. com logger.addHandler(fh); }
From source file:com.valygard.aohruthless.Joystick.java
/** * Not satisfied with global logging, which can become cluttered and messy * with many plugins or players using commands, etc, the Bukkit logger will * log all relevant information to a log file. This log file can be found in * the plugin data folder, in the subdirectory <i>logs</i>. * <p>/*from w w w. j av a 2 s. c o m*/ * The log file, <i>joystick.log</i>, will not be overriden. The logger will * simply append new information if the file already exists. All messages * will be logged in the format:<br> * [month-day-year hr-min-sec] (level): (message), where level is the * {@link LogRecord#getLevel()} and the message is the * {@link LogRecord#getMessage()}. * </p> * * @return the created FileHandler, null if a SecurityException or * IOException were thrown and handled. */ private FileHandler setupLogger() { File dir = new File(getDataFolder() + File.separator + "logs"); dir.mkdirs(); try { FileHandler handler = new FileHandler(dir + File.separator + "joystick.log", true); getLogger().addHandler(handler); handler.setFormatter(new SimpleFormatter() { @Override public String format(LogRecord record) { Date date = new Date(); SimpleDateFormat df = new SimpleDateFormat("[MM-dd-yyyy HH:mm:ss]"); return df.format(date) + " " + record.getLevel() + ":" // org.apache.commons.lang + StringUtils.replace(record.getMessage(), "[Joystick]", "") + System.getProperty("line.separator"); } }); return handler; } catch (SecurityException | IOException e) { e.printStackTrace(); return null; } }
From source file:org.silverpeas.core.test.rule.CommonAPI4Test.java
public void setLoggerLevel(Level level) { final ConsoleHandler handler = new ConsoleHandler(); setLoggerHandler(handler);/*from w w w . j a v a2s . c om*/ handler.setFormatter(new SimpleFormatter()); switch (level) { case INFO: Logger.getLogger(ROOT_NAMESPACE).setLevel(java.util.logging.Level.INFO); handler.setLevel(java.util.logging.Level.INFO); break; case DEBUG: Logger.getLogger(ROOT_NAMESPACE).setLevel(java.util.logging.Level.FINE); handler.setLevel(java.util.logging.Level.FINE); break; case WARNING: Logger.getLogger(ROOT_NAMESPACE).setLevel(java.util.logging.Level.WARNING); handler.setLevel(java.util.logging.Level.WARNING); break; case ERROR: Logger.getLogger(ROOT_NAMESPACE).setLevel(java.util.logging.Level.SEVERE); handler.setLevel(java.util.logging.Level.SEVERE); break; } }
From source file:BSxSB.Controllers.AdminController.java
@RequestMapping(value = "/admineditscheduleblocks", method = RequestMethod.POST) public String editScheduleBlocks(Model model, @RequestParam(value = "schoolID") String schoolID) { try {//from ww w.j a va 2 s . com //Initialize the file that the logger writes to. Handler handler = new FileHandler("%tBSxSBAdminScheduleBlocks.log", true); handler.setFormatter(new SimpleFormatter()); logger.addHandler(handler); logger.info("Admin Viewing List of School's Schedule Blocks."); int schoolID2 = Integer.parseInt(schoolID); Schools school = SchoolDAO.getSchool(schoolID2); List<Scheduleblocks> sbs = ScheduleBlockDAO.getSchoolsScheduleBlocks(schoolID2); model.addAttribute("school", school); model.addAttribute("scheduleblocks", sbs); logger.info("School's schedule blocks successfully updated to model."); handler.close(); logger.removeHandler(handler); } catch (IOException ex) { logger.log(Level.SEVERE, null, ex); } catch (SecurityException ex) { logger.log(Level.SEVERE, null, ex); } return "admineditscheduleblocks"; }
From source file:eu.abc4trust.abce.pertubationtests.tud.section2.PA_II_2_2_7randomUID.java
@BeforeClass public static void setupLogger() throws SecurityException, IOException { Handler fh = new FileHandler("PA-Section-2.2.7randomUID.log"); SimpleFormatter simpleFormatter = new SimpleFormatter(); fh.setFormatter(simpleFormatter);//from w w w .j a v a2 s . co m logger.addHandler(fh); }
From source file:com.basp.trabajo_al_minuto.model.business.BusinessUtils.java
public static void writeLog(String file, List<LogRecord> records) throws BusinessException { try {/* w w w . j a v a 2 s .c o m*/ Logger logger = Logger.getAnonymousLogger(); SimpleFormatter formatter = new SimpleFormatter(); FileHandler fh = new FileHandler(file, Boolean.TRUE); fh.setFormatter(formatter); logger.addHandler(fh); logger.setUseParentHandlers(false); records.stream().forEach((logRecord) -> { logger.log(logRecord); }); } catch (IOException | SecurityException ex) { Logger.getLogger(BusinessUtils.class.getName()).log(Level.SEVERE, "BusinessUtils.writeLog Exception", ex); throw new BusinessException(ex); } }