Added Logging class.

This commit is contained in:
Andrew Lalis 2018-07-01 08:26:29 +02:00
parent fc7052b95c
commit 16b47f7e40
4 changed files with 78 additions and 6 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea/*
target/*
*.iml
log/*

View File

@ -1,11 +1,13 @@
package nl.andrewlalis;
import nl.andrewlalis.model.Team;
import nl.andrewlalis.util.Logging;
import nl.andrewlalis.util.TeamGenerator;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import nl.andrewlalis.util.CommandLine;
@ -14,18 +16,27 @@ import nl.andrewlalis.util.CommandLine;
*/
public class Main {
private static final Logger logger = Logger.getGlobal();
public static void main(String[] args) throws IOException {
System.out.println("Initializer for Github Repositories in Educational Organizations.");
// Parsed command line arguments.
Map<String, String> userOptions = CommandLine.parseArgs(args);
// Initialize logger.
try {
Logging.setup(true); // TODO: Replace true with command line arg.
} catch (IOException e) {
logger.severe("Unable to save log to file.");
}
logger.info("Initializer for Github Repositories in Educational Organizations.");
List<Team> teams = TeamGenerator.generateFromCSV(
userOptions.get("input"),
Integer.parseInt(userOptions.get("teamsize"))
);
System.out.println(teams);
logger.info("Teams created: " + teams);
}
}

View File

@ -0,0 +1,31 @@
package nl.andrewlalis.util;
import java.io.IOException;
import java.util.logging.*;
/**
* Responsible for creating logs to standard output and writing to files.
*/
public class Logging {
private static FileHandler outputFile;
private static SimpleFormatter formatter;
public static void setup(boolean verbose) throws IOException {
Logger logger = Logger.getGlobal();
if (verbose) {
logger.setLevel(Level.FINEST);
} else {
logger.setLevel(Level.INFO);
}
outputFile = new FileHandler("log/latest.txt");
formatter = new SimpleFormatter();
outputFile.setFormatter(formatter);
logger.addHandler(outputFile);
}
}

View File

@ -8,14 +8,43 @@ import org.apache.commons.csv.CSVRecord;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.logging.Logger;
public class TeamGenerator {
public static List<Team> generateFromCSV(String filename, int teamSize) throws IOException {
System.out.println("Generating teams of size " + teamSize);
private static final Logger logger = Logger.getLogger(TeamGenerator.class.getName());
static {
logger.setParent(Logger.getGlobal());
}
/**
* Creates a list of teams by reading a CSV file of a certain format. The format for each column is as follows:
* 1. Timestamp - The date and time the record was entered.
* 2. Username - The email address.
* 3. Name - The student's name.
* 4. Student Number
* 5. Github Username
* 6. I have chosen a partner. (Yes / No) If yes:
* 7. Your Partner's Student Number
* @param filename The CSV file to load from.
* @param teamSize The preferred teamsize used in creating teams.
* @return A list of teams.
* @throws IOException If the file is unable to be read.
* @throws IllegalArgumentException If an invalid teamsize is given.
*/
public static List<Team> generateFromCSV(String filename, int teamSize) throws IOException, IllegalArgumentException {
logger.info("Generating teams of size " + teamSize);
if (teamSize < 1) {
logger.severe("Invalid team size.");
throw new IllegalArgumentException("Team size must be greater than or equal to 1. Got " + teamSize);
}
logger.finest("Parsing CSV file.");
Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(new FileReader(filename));
logger.finest("Reading all records into map.");
Map<Integer, Student> studentMap = readAllStudents(records, teamSize);
logger.finest("Generating all valid teams from student map.");
return generateAllValidTeams(studentMap, teamSize);
}