Example usage for java.util.logging Handler setFormatter

List of usage examples for java.util.logging Handler setFormatter

Introduction

In this page you can find the example usage for java.util.logging Handler setFormatter.

Prototype

public synchronized void setFormatter(Formatter newFormatter) throws SecurityException 

Source Link

Document

Set a Formatter .

Usage

From source file:BSxSB.Controllers.StudentController.java

@RequestMapping(value = "/generateschedule", method = RequestMethod.GET)
public String generateSchedule(Model model, @RequestParam("instructors") String instructors) {
    try {/*w  w  w. j  ava 2  s . c om*/
        //Initialize the file that the logger writes to.
        Handler handler = new FileHandler("%tBSxSBStudentGenerateCourses.log", true);
        handler.setFormatter(new SimpleFormatter());
        logger.addHandler(handler);
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        String name = auth.getName();
        Students currentStudent = StudentDAO.getStudent(name);
        Schools currentSchool = SchoolDAO.getSchool(currentStudent.getSchoolid());
        Generationcriteria gencriteria = GenerationcriteriaDAO
                .getGenerationCriteria(currentStudent.getStudentid());
        String[] courseids = gencriteria.getCourseids().split(",");
        List<Courses> genCourses = new ArrayList<>();
        List<Scheduleblocks> genscheduleblocks = new ArrayList<>();
        if (!gencriteria.getCourseids().isEmpty()) {
            for (String courseid : courseids) {
                Courses genCourse = CourseDAO.getCourse(Integer.parseInt(courseid));
                genCourses.add(genCourse);
                genscheduleblocks.add(ScheduleBlockDAO.getScheduleBlock(genCourse.getScheduleblockid()));
            }
        }
        List<List<Courses>> conflictCourses = new ArrayList<>();
        String lunches = gencriteria.getLunch();
        String[] lunch = lunches.split(",");
        for (int i = 0; i < lunch.length; i++) {
            if (lunch[i].equals("monday")) {
                lunch[i] = "1";
            } else if (lunch[i].equals("tuesday")) {
                lunch[i] = "2";
            } else if (lunch[i].equals("wednesday")) {
                lunch[i] = "3";
            } else if (lunch[i].equals("thursday")) {
                lunch[i] = "4";
            } else if (lunch[i].equals("friday")) {
                lunch[i] = "5";
            } else if (lunch[i].equals("saturday")) {
                lunch[i] = "6";
            } else if (lunch[i].equals("sunday")) {
                lunch[i] = "7";
            }
        }
        String lunchperiods = currentSchool.getLunchrange();
        String[] temp = lunchperiods.split("-");
        int length = Integer.parseInt(temp[1]) - Integer.parseInt(temp[0]);
        int[] lunchperiod = new int[length + 1];
        int low = Integer.parseInt(temp[0]);
        for (int i = 0; i <= length; i++) {
            lunchperiod[i] = low;
            low++;
        }
        int conlunchcount = 0;
        List<List<Courses>> conflictLunches = new ArrayList<>();
        for (int i = 0; i < genscheduleblocks.size(); i++) {
            Scheduleblocks firstBlock = genscheduleblocks.get(i);
            conflictLunch: {
                for (int period : lunchperiod) {
                    if (firstBlock.getPeriod().equals(period)) {
                        String[] gendays = firstBlock.getDays().split(",");
                        for (String days : lunch) {
                            for (String genday : gendays) {
                                if (days.equals(genday)) {
                                    List<Courses> conflictCourse = new ArrayList<>();
                                    conflictCourse.add(genCourses.get(i));
                                    Courses lunchCourse = new Courses(0, "lunch", "", "", 0);
                                    conflictCourse.add(lunchCourse);
                                    conflictLunches.add(conflictCourse);
                                    conlunchcount++;
                                    break conflictLunch;
                                }
                            }
                        }
                    }
                }
            }
            for (int i2 = i + 1; i2 < genscheduleblocks.size(); i2++) {
                Scheduleblocks secondBlock = genscheduleblocks.get(i2);
                checkConflict: {
                    if (firstBlock.getPeriod().equals(secondBlock.getPeriod())) {
                        String[] days = firstBlock.getDays().split(",");
                        String[] days2 = secondBlock.getDays().split(",");
                        for (String d : days) {
                            for (String d2 : days2) {
                                if (d.equals(d2)) {
                                    Courses genCourse = genCourses.get(i);
                                    Courses genCourse2 = genCourses.get(i2);
                                    String[] semesters = genCourse.getSemester().split(",");
                                    String[] semesters2 = genCourse2.getSemester().split(",");
                                    for (String s : semesters) {
                                        for (String s2 : semesters2) {
                                            if (s.equals(s2)) {
                                                List<Courses> conflictCourse = new ArrayList<>();
                                                conflictCourse.add(genCourse);
                                                conflictCourse.add(genCourse2);
                                                conflictCourses.add(conflictCourse);
                                                break checkConflict;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.print(conlunchcount + " " + lunchperiod.length);
        if (conlunchcount >= lunchperiod.length) {

            for (List<Courses> conlunch : conflictLunches) {
                System.out.print(conlunch);
                conflictCourses.add(conlunch);
            }
        }
        if (conflictCourses.isEmpty()) {
            String[] instructor = instructors.split(",");
            for (int i = 0; i < genCourses.size(); i++) {
                Courses course1 = genCourses.get(i);
                String courseiden = course1.getCourseidentifier();
                for (int i2 = i + 1; i2 < genCourses.size(); i2++) {
                    Courses course2 = genCourses.get(i2);
                    checkCourse: {
                        if (courseiden.equals(course2.getCourseidentifier())) {
                            if (instructors.isEmpty()) {
                                List<Students> friends = StudentDAO.getFriends(currentStudent.getStudentid());
                                int friendcourse1 = 0;
                                int friendcourse2 = 0;
                                for (Students friend : friends) {
                                    if (RegistrationDAO.isRegistered(course1, friend)) {
                                        friendcourse1++;
                                    } else if (RegistrationDAO.isRegistered(course2, friend)) {
                                        friendcourse2++;
                                    }
                                }
                                if (friendcourse1 >= friendcourse2) {
                                    genCourses.remove(i2);
                                    break checkCourse;
                                } else if (friendcourse1 < friendcourse2) {
                                    genCourses.remove(i);
                                    break checkCourse;
                                } else {
                                    genCourses.remove(i);
                                    break checkCourse;
                                }
                            } else {
                                for (String inst : instructor) {
                                    if (course1.getInstructor().equals(inst)) {
                                        genCourses.remove(i2);
                                        break checkCourse;
                                    } else if (course2.getInstructor().equals(inst)) {
                                        genCourses.remove(i);
                                        break checkCourse;
                                    } else {
                                        List<Students> friends = StudentDAO
                                                .getFriends(currentStudent.getStudentid());
                                        int friendcourse1 = 0;
                                        int friendcourse2 = 0;
                                        for (Students friend : friends) {
                                            if (RegistrationDAO.isRegistered(course1, friend)) {
                                                friendcourse1++;
                                            } else if (RegistrationDAO.isRegistered(course2, friend)) {
                                                friendcourse2++;
                                            }
                                        }
                                        if (friendcourse1 >= friendcourse2) {
                                            genCourses.remove(i2);
                                            break checkCourse;
                                        } else if (friendcourse1 < friendcourse2) {
                                            genCourses.remove(i);
                                            break checkCourse;
                                        } else {
                                            genCourses.remove(i);
                                            break checkCourse;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            List<List<Courses[]>> semesters = new ArrayList<>();
            List<Students> friends = StudentDAO.getFriends(currentStudent.getStudentid());
            for (Courses course : genCourses) {
                for (Students friend : friends) {
                    List<Courses> friendCourses = CourseDAO.getCoursesForStudent(friend.getStudentid());
                    for (Courses friendCourse : friendCourses) {
                        if (friendCourse.getCourseid() == (course.getCourseid())) {
                            if (course.getFriends() != null) {
                                course.setFriends(course.getFriends() + " " + friend.getFirstname() + " "
                                        + friend.getLastname());
                            } else {
                                course.setFriends(friend.getFirstname() + " " + friend.getLastname());
                            }
                        }
                    }
                }
            }
            for (int s = 0; s < currentSchool.getNumsemesters(); s++) {
                List<Courses[]> schedule = new ArrayList<>();
                for (int i = 0; i < currentSchool.getNumperiods(); i++) {
                    Courses[] period = new Courses[7];
                    for (Courses course : genCourses) {
                        Scheduleblocks sb = ScheduleBlockDAO.getScheduleBlock(course.getScheduleblockid());
                        if (sb.getPeriod() == i + 1) {
                            String[] days = sb.getDays().split(",");
                            String[] semester = course.getSemester().split(",");
                            for (String sem : semester) {
                                if (Integer.parseInt(sem) == s + 1) {
                                    for (String day : days) {
                                        period[Integer.parseInt(day) - 1] = course;
                                    }
                                }
                            }
                        }
                    }
                    schedule.add(period);
                }
                semesters.add(schedule);
            }
            model.addAttribute("semester", semesters);
            logger.info("Generated schedule complete.");
        } else {
            model.addAttribute("conflictCourses", conflictCourses);
            logger.info("Conflicting courses displayed.");
        }

        List<Schools> schoolyears = SchoolDAO.getSchoolSameName(currentSchool.getSchoolname());
        model.addAttribute("schoolyears", schoolyears);
        handler.close();
        logger.removeHandler(handler);
    } catch (IOException ex) {
        logger.log(Level.SEVERE, null, ex);
    } catch (SecurityException ex) {
        logger.log(Level.SEVERE, null, ex);
    }
    return "studentviewgenerated";
}

From source file:com.cisco.oss.foundation.logging.FoundationLogger.java

private static void setupJULSupport(URL resource) {
    boolean julSupportEnabled = Boolean.valueOf(log4jConfigProps
            .getProperty(FoundationLoggerConstants.Foundation_JUL_SUPPORT_ENABLED.toString(), "false"));
    if (julSupportEnabled) {
        String appenderRef = log4jConfigProps
                .getProperty(FoundationLoggerConstants.Foundation_JUL_APPENDER_REF.toString());

        if (StringUtils.isBlank(appenderRef)) {

            Enumeration allAppenders = Logger.getRootLogger().getAllAppenders();

            while (allAppenders.hasMoreElements()) {

                Appender appender = (Appender) allAppenders.nextElement();

                if (appender instanceof FileAppender) {
                    appenderRef = appender.getName();
                    getLogger(FoundationLogger.class)
                            .info("*** Using '" + appenderRef + "' as the Java util logging appender ref ***");
                    System.err.println(
                            "*** Using '" + appenderRef + "' as the Java util logging appender ref ***");
                    break;
                }/*from w w w .  j  a va 2 s  .  co  m*/
            }
        }

        if (StringUtils.isBlank(appenderRef)) {
            throw new IllegalArgumentException(
                    "Java util support was enabled but couldn't find a matching appender under the '"
                            + FoundationLoggerConstants.Foundation_JUL_APPENDER_REF.toString() + "' key.");
        }

        Handler handler = null;

        Appender appender = Logger.getRootLogger().getAppender(appenderRef);
        if (appender == null) {

            Enumeration allAppenders = Logger.getRootLogger().getAllAppenders();

            while (allAppenders.hasMoreElements()) {

                Appender tempAppender = (Appender) allAppenders.nextElement();

                if (tempAppender instanceof AsyncAppender) {

                    AsyncAppender asyncAppender = (AsyncAppender) tempAppender;
                    Enumeration asyncAppenderAllAppenders = asyncAppender.getAllAppenders();

                    while (asyncAppenderAllAppenders.hasMoreElements()) {

                        Appender asyncTempAppender = (Appender) asyncAppenderAllAppenders.nextElement();

                        if (appenderRef.equals(asyncTempAppender.getName())) {
                            appender = asyncTempAppender;
                            break;
                        }
                    }
                    if (appender != null) {
                        break;
                    }
                }
            }
        }

        if (appender instanceof FileAppender) {
            try {
                handler = new FileHandler(((FileAppender) appender).getFile());
            } catch (IOException e) {
                throw new IllegalArgumentException(
                        "IOException encountered when trying to setup jul logging: " + e, e);
            }
        } else if (appender instanceof ConsoleAppender) {
            handler = new ConsoleHandler();
        } else {
            getLogger(FoundationLogger.class)
                    .error("got a reference to an unsupported appender: " + appenderRef);
        }

        if (handler != null) {

            //                System.setProperty("java.util.logging.config.file",resource.getPath());

            java.util.logging.LogManager.getLogManager().reset();
            try {
                java.util.logging.LogManager.getLogManager().readConfiguration(resource.openStream());
            } catch (IOException e) {
                throw new IllegalArgumentException(
                        "IOException encountered when trying to read log4j properties file: " + e, e);
            }

            handler.setLevel(java.util.logging.Level.FINEST);
            handler.setFormatter(new FoundationLogFormatter());

            java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger("");
            rootLogger.addHandler(handler);
            rootLogger.setLevel(java.util.logging.Level.SEVERE);

            Properties julLoggerSubset = getPropertiesSubset("jul.logger");
            if (!julLoggerSubset.isEmpty()) {
                Set<Object> keySet = julLoggerSubset.keySet();
                for (Object key : keySet) {
                    java.util.logging.Logger logger = java.util.logging.Logger.getLogger((String) key);
                    logger.setLevel(java.util.logging.Level.parse((String) julLoggerSubset.get(key)));
                }
            }
        }

    }
}