Skip to content

Commit

Permalink
Merge pull request #26 from jonx8/logging
Browse files Browse the repository at this point in the history
Logging
  • Loading branch information
jonx8 authored Sep 23, 2023
2 parents 82268ef + fbab909 commit 9ad4f17
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 37 deletions.
24 changes: 19 additions & 5 deletions src/main/java/ru/etu/petci/Main.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,49 @@
package ru.etu.petci;


import ru.etu.petci.handlers.AddJobCommandHandler;
import ru.etu.petci.handlers.CommandHandler;
import ru.etu.petci.handlers.ContinueCommandHandler;
import ru.etu.petci.handlers.InitCommandHandler;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.Logger;

public class Main {
private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
private static final Map<String, CommandHandler> commandHandlersMap = new HashMap<>();
private static final Logger LOGGER;


static {
LOGGER.setLevel(Level.WARNING);
try (InputStream input = Main.class.getClassLoader().getResourceAsStream("logging.properties")) {
LogManager.getLogManager().readConfiguration(input);
} catch (IOException e) {
System.out.println(e.getMessage());
}
LOGGER = Logger.getLogger(Main.class.getName());

commandHandlersMap.put("init", new InitCommandHandler());
commandHandlersMap.put("continue", new ContinueCommandHandler());
commandHandlersMap.put("add", new AddJobCommandHandler());
}


public static void main(String[] args) {
int exitStatus;
if (args.length == 0) {
showHelp();
System.exit(1);
exitStatus = 1;
} else {
CommandHandler handler = commandHandlersMap.get(args[0].trim());
exitStatus = handler.handle(args);
}
CommandHandler handler = commandHandlersMap.get(args[0].trim());
System.exit(handler.handle(args));
LOGGER.log(Level.FINE, "The program finished with exit code {0}", exitStatus);
System.exit(exitStatus);
}


Expand Down
14 changes: 8 additions & 6 deletions src/main/java/ru/etu/petci/configuration/Configurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@

public final class Configurator {

public static final String JOBS_DIR = "jobs/";
public static final String REPOSITORY_PROPERTY = ".repo.properties";
public static final String JOBS_DIR = "petCI/jobs/";
public static final String LOGS_DIR = "petCI/logs/";

private static final Logger LOGGER = Logger.getLogger(Configurator.class.getName());
public static final String REPOSITORY_PROPERTY = "petCI/repo.properties";

static {
LOGGER.setLevel(Level.ALL);
}
private static final Logger LOGGER = Logger.getLogger(Configurator.class.getName());

private Configurator() {
}
Expand All @@ -40,6 +38,7 @@ public static void saveRepositoryConfig(String branchName, String lastHash) thro
properties.setProperty("last_hash", lastHash);
properties.setProperty("branch_name", branchName);
properties.store(writer, "Repository config");
LOGGER.log(Level.FINE, "File {0} was updated", REPOSITORY_PROPERTY);
}
}

Expand All @@ -60,9 +59,11 @@ public static List<Job> readJobsConfig() throws IOException {
try (var reader = new FileReader(jobProperty)) {
Properties properties = new Properties();
properties.load(reader);

String jobName = properties.getProperty("name");
String scriptName = properties.getProperty("script_name");
boolean isActive = Boolean.parseBoolean(properties.getProperty("active"));

if (jobName != null && scriptName != null) {
jobs.add(new Job(jobName, scriptName, isActive));
} else {
Expand All @@ -86,6 +87,7 @@ public static void saveJobConfig(Job job) throws IOException {
properties.setProperty("script_name", job.scriptName());
properties.setProperty("active", String.valueOf(job.isActive()));
properties.store(writer, "Job settings");
LOGGER.log(Level.FINE, "File {0} was updated", JOBS_DIR + job.name() + ".properties");
}
}
}
5 changes: 0 additions & 5 deletions src/main/java/ru/etu/petci/handlers/AddJobCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@
public class AddJobCommandHandler implements CommandHandler {
private static final Logger LOGGER = Logger.getLogger(AddJobCommandHandler.class.getName());


static {
LOGGER.setLevel(Level.INFO);
}

@Override
public int handle(String[] args) {
try (var scanner = new Scanner(System.in)) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ru/etu/petci/handlers/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public interface CommandHandler {
/**
* This method should handle a certain command which is entered by user.
*
* @param arg argument after name of the command. Arg is NotNull.
* @param args from the main method
* @return exit code for the program
*/
int handle(String[] args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public class ContinueCommandHandler implements CommandHandler {

private static final Logger LOGGER = Logger.getLogger(ContinueCommandHandler.class.getName());

static {
LOGGER.setLevel(Level.INFO);
}

@Override
public int handle(String[] args) {
try {
Expand All @@ -28,7 +24,7 @@ public int handle(String[] args) {
repositoryObserver.setExecutor(new JobsExecutor(jobs));
repositoryObserver.start();
} catch (InterruptedException e) {
LOGGER.severe("The program was interrupted");
LOGGER.warning("The program was interrupted");
Thread.currentThread().interrupt();
} catch (IOException e) {
LOGGER.severe(e.getMessage());
Expand Down
17 changes: 8 additions & 9 deletions src/main/java/ru/etu/petci/handlers/InitCommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
import java.util.logging.Logger;

import static ru.etu.petci.configuration.Configurator.JOBS_DIR;
import static ru.etu.petci.configuration.Configurator.LOGS_DIR;

public class InitCommandHandler implements CommandHandler {
private static final Logger LOGGER = Logger.getLogger(InitCommandHandler.class.getName());

static {
LOGGER.setLevel(Level.INFO);
}


@Override
public int handle(String[] args) {
Expand All @@ -30,12 +27,14 @@ public int handle(String[] args) {
}

try {
Configurator.saveRepositoryConfig(branchName);
if (new File(JOBS_DIR).mkdir()) {
LOGGER.log(Level.CONFIG, "Directory \"{0}\" has been created", JOBS_DIR);
} else {
LOGGER.log(Level.WARNING, "Failed while creating directory \"{0}\"", JOBS_DIR);
if (new File(JOBS_DIR).mkdirs()) {
LOGGER.log(Level.WARNING, "Unable to create {0} directory.", JOBS_DIR);
}
if (new File(LOGS_DIR).mkdirs()) {
LOGGER.log(Level.WARNING, "Unable to create {0} directory.", LOGS_DIR);
}
Configurator.saveRepositoryConfig(branchName);

} catch (IOException e) {
LOGGER.severe(e.getMessage());
return 1;
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/ru/etu/petci/observers/RepositoryObserver.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ public class RepositoryObserver {
private final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
private static final Logger LOGGER = Logger.getLogger(RepositoryObserver.class.getName());

static {
LOGGER.setLevel(Level.INFO);
}

public static RepositoryObserver of(String branchName, String lastHash) {
Objects.requireNonNull(branchName);
Expand Down Expand Up @@ -71,14 +68,13 @@ private void checkRepositoryUpdate() {
if (newHash.length() == 40 && !newHash.equals(lastHash)) {
LOGGER.log(Level.INFO, "Commits checked. New commit was found. Hash: {0}", newHash);
if (executor.runJobs()) {
LOGGER.fine("Jobs completed successfully. Update last commit hash.");
lastHash = newHash;
Configurator.saveRepositoryConfig(branchName, lastHash);
} else {
LOGGER.info("Running jobs failed. New commit has been rejected");
rejectCommit();
}
} else {
LOGGER.fine("Commits checked. No new commits found.");
}
} catch (IOException e) {
LOGGER.severe(e.getMessage());
Expand All @@ -103,6 +99,7 @@ private void rejectCommit() throws IOException {
throw new IOException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
LOGGER.warning("The program was interrupted.");
} finally {
if (gitProcess != null) {
gitProcess.destroy();
Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/ru/etu/petci/logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=INFO
java.util.logging.FileHandler.pattern=petCI/logs/%u.log
java.util.logging.FileHandler.limit=20000
java.util.logging.FileHandler.count=1
java.util.logging.FileHandler.maxLocks=5
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=FINE
java.util.logging.FileHandler.append=true

java.util.logging.ConsoleHandler.level=INFO
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%4$s: %5$s [%1$tc]%n
1 change: 0 additions & 1 deletion src/main/resources/ru/etu/petci/settings.properties

This file was deleted.

0 comments on commit 9ad4f17

Please sign in to comment.