Permalink
Browse files

Improve logging and error handling

  • Loading branch information...
Derpthemeus committed Dec 28, 2017
1 parent 6a61aaa commit c808aa98bad53fc85355904ec147024425fcd922
Showing with 61 additions and 36 deletions.
  1. +1 −0 .gitignore
  2. +2 −0 build.gradle
  3. +8 −5 src/main/java/com/derpthemeus/runeCoach/SetupSeedData.java
  4. +6 −3 src/main/java/com/derpthemeus/runeCoach/databasePopulator/PopulatorThread.java
  5. +7 −2 src/main/java/com/derpthemeus/runeCoach/databasePopulator/PopulatorThreadSupervisor.java
  6. +2 −2 ...main/java/com/derpthemeus/runeCoach/databasePopulator/populatorThreads/MatchDownloaderThread.java
  7. +2 −2 src/main/java/com/derpthemeus/runeCoach/databasePopulator/populatorThreads/MatchFinderThread.java
  8. +4 −4 src/main/java/com/derpthemeus/runeCoach/databasePopulator/populatorThreads/StatAggregatorThread.java
  9. +2 −2 .../com/derpthemeus/runeCoach/databasePopulator/populatorThreads/SummonerAccountIdUpdaterThread.java
  10. +2 −2 src/main/java/com/derpthemeus/runeCoach/databasePopulator/populatorThreads/SummonerFinderThread.java
  11. +2 −2 ...ava/com/derpthemeus/runeCoach/databasePopulator/populatorThreads/SummonerLeagueUpdaterThread.java
  12. +1 −2 ...java/com/derpthemeus/runeCoach/databasePopulator/threadSupervisors/MatchDownloaderSupervisor.java
  13. +1 −2 ...ain/java/com/derpthemeus/runeCoach/databasePopulator/threadSupervisors/MatchFinderSupervisor.java
  14. +1 −2 .../java/com/derpthemeus/runeCoach/databasePopulator/threadSupervisors/StatAggregatorSupervisor.java
  15. +1 −2 ...derpthemeus/runeCoach/databasePopulator/threadSupervisors/SummonerAccountIdUpdaterSupervisor.java
  16. +1 −2 .../java/com/derpthemeus/runeCoach/databasePopulator/threadSupervisors/SummonerFinderSupervisor.java
  17. +1 −2 ...om/derpthemeus/runeCoach/databasePopulator/threadSupervisors/SummonerLeagueUpdaterSupervisor.java
  18. +17 −0 src/main/resources/log4j2.xml
View
@@ -2,3 +2,4 @@
.idea/
lib/
settings.gradle
logs/
View
@@ -14,6 +14,8 @@ dependencies {
compile "com.github.stelar7:L4J8:master"
compile "mysql:mysql-connector-java:5.1.6"
compile "org.hibernate:hibernate-core:5.2.12.Final"
compile "org.apache.logging.log4j:log4j-api:2.10.0"
compile "org.apache.logging.log4j:log4j-core:2.10.0"
}
jar {
@@ -4,6 +4,8 @@
import no.stelar7.api.l4j8.basic.utils.Utils;
import no.stelar7.api.l4j8.pojo.match.Match;
import no.stelar7.api.l4j8.pojo.match.ParticipantIdentity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
@@ -18,9 +20,10 @@
* Adds players from seed data (matches are from before Runes Reforged, and cannot be used) to the database
*/
public class SetupSeedData {
public static void main(String[] args) {
private static final Logger logger = LogManager.getLogger();
public static void main(String[] args) {
Configuration config = new Configuration()
.configure()
.setProperty("hibernate.connection.username", System.getenv("MYSQL_USERNAME"))
@@ -34,8 +37,8 @@ public static void main(String[] args) {
try {
URL url = new URL("https://s3-us-west-1.amazonaws.com/riot-developer-portal/seed-data/matches" + seedFile + ".json");
reader = new InputStreamReader(url.openStream());
} catch (IOException e) {
e.printStackTrace();
} catch (IOException ex) {
logger.error("Error downloading file " + seedFile, ex);
continue;
}
@@ -59,11 +62,11 @@ public static void main(String[] args) {
if (tx != null) {
tx.markRollbackOnly();
}
ex.printStackTrace();
logger.error("Error adding summoner" + summonerEntity.getSummonerId() + " to database (they may have already been added)", ex);
}
}
}
System.out.println("Finished file " + seedFile);
logger.info("Finished processing file " + seedFile + "/10");
}
}
@@ -1,12 +1,15 @@
package com.derpthemeus.runeCoach.databasePopulator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public abstract class PopulatorThread<T extends PopulatorThread> extends Thread {
private boolean keepAlive = true;
// TODO handle exceptions better
protected void handleException(Exception ex) {
ex.printStackTrace();
protected Logger getLogger() {
return LogManager.getLogger();
}
/**
@@ -2,6 +2,8 @@
import no.stelar7.api.l4j8.basic.APICredentials;
import no.stelar7.api.l4j8.impl.L4J8;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
@@ -49,13 +51,16 @@ public T spawnThread() {
thread.start();
return thread;
} catch (Exception ex) {
System.err.println("Error spawning new PopulatorThread:");
ex.printStackTrace();
getLogger().error("Error spawning new PopulatorThread:", ex);
}
return null;
}
public List<T> getRunningThreads() {
return runningThreads;
}
protected Logger getLogger() {
return LogManager.getLogger();
}
}
@@ -32,7 +32,7 @@ public void runOperation() {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
handleException(ex);
getLogger().error(ex);
}
return;
}
@@ -92,7 +92,7 @@ public void runOperation() {
if (tx != null) {
tx.markRollbackOnly();
}
handleException(ex);
getLogger().error("Error downloading match " + matchEntity.getMatchId());
}
matchEntity = null;
}
@@ -33,7 +33,7 @@ public void runOperation() {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
handleException(ex);
getLogger().error(ex);
}
return;
}
@@ -83,7 +83,7 @@ public void runOperation() {
if (tx != null) {
tx.markRollbackOnly();
}
handleException(ex);
getLogger().error("Error processing match history for summoner ID " + summoner.getSummonerId(), ex);
}
summoner = null;
}
@@ -20,11 +20,11 @@
public void runOperation() {
// Use the latest patch if one hasn't been specified
if (patch == null) {
System.out.println("StatAggregatorThread #" + this.getId() + " is defaulting to most recent patch");
getLogger().info("Defaulting to most recent patch");
try {
patch = DDragonManager.convertToShortVersion(DDragonManager.getLatestVersion());
} catch (IOException ex) {
handleException(ex);
getLogger().error("Error getting DDragon version", ex);
return;
}
}
@@ -35,7 +35,7 @@ public void runOperation() {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
handleException(ex);
getLogger().error(ex);
}
return;
}
@@ -71,7 +71,7 @@ public void runOperation() {
if (tx != null) {
tx.markRollbackOnly();
}
handleException(ex);
getLogger().error("Error aggregating stats for perk " + stat.getPerkId() + " on champion " + stat.getChampionId() + " during patch " + stat.getPatch(), ex);
}
stat = null;
}
@@ -26,7 +26,7 @@ public void runOperation() {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
handleException(ex);
getLogger().error(ex);
}
return;
}
@@ -46,7 +46,7 @@ public void runOperation() {
if (tx != null) {
tx.markRollbackOnly();
}
handleException(ex);
getLogger().error("Error updating account ID for summoner " + summonerEntity.getSummonerId(), ex);
}
summonerEntity = null;
}
@@ -32,7 +32,7 @@ public void runOperation() {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
handleException(ex);
getLogger().error(ex);
}
return;
}
@@ -81,7 +81,7 @@ public void runOperation() {
if (tx != null) {
tx.markRollbackOnly();
}
handleException(ex);
getLogger().error("Error finding summoners for league " + leagueEntity.getUuid(), ex);
}
leagueEntity = null;
}
@@ -31,7 +31,7 @@ public void runOperation() {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
handleException(ex);
getLogger().error(ex);
}
return;
}
@@ -69,7 +69,7 @@ public void runOperation() {
if (tx != null) {
tx.markRollbackOnly();
}
handleException(ex);
getLogger().error("Error updating league for summoner " + summoner.getSummonerId(), ex);
}
summoner = null;
}
@@ -52,8 +52,7 @@ public synchronized MatchEntity getMatchToDownload() {
List<MatchEntity> matches = query.setMaxResults(200).getResultList();
matchesToDownload.addAll(matches);
} catch (Exception ex) {
// TODO handle exception
ex.printStackTrace();
getLogger().error("Error refilling lazy list", ex);
}
}
return matchesToDownload.poll();
@@ -58,8 +58,7 @@ public synchronized SummonerEntity getSummonerToCheck() {
List<SummonerEntity> summoners = query.setMaxResults(30).getResultList();
summonersToCheck.addAll(summoners);
} catch (Exception ex) {
// TODO handle exception
ex.printStackTrace();
getLogger().error("Error refilling lazy list", ex);
}
}
return summonersToCheck.poll();
@@ -91,8 +91,7 @@ public synchronized AggregatedChampionStatsEntity getStatToAggregate(String patc
if (tx != null) {
tx.markRollbackOnly();
}
// TODO handle exception
ex.printStackTrace();
getLogger().error("Error refilling lazy list", ex);
}
}
@@ -51,8 +51,7 @@ public synchronized SummonerEntity getSummonerToUpdate() {
List<SummonerEntity> summoners = query.setMaxResults(50).getResultList();
summonersToCheck.addAll(summoners);
} catch (Exception ex) {
// TODO handle exception
ex.printStackTrace();
getLogger().error("Error refilling lazy list", ex);
}
}
return summonersToCheck.poll();
@@ -55,8 +55,7 @@ public synchronized LeagueEntity getLeagueToCheck() {
List<LeagueEntity> leagues = query.setMaxResults(40).getResultList();
leaguesToCheck.addAll(leagues);
} catch (Exception ex) {
// TODO handle exception
ex.printStackTrace();
getLogger().error("Error refilling lazy list", ex);
}
}
return leaguesToCheck.poll();
@@ -59,8 +59,7 @@ public synchronized SummonerEntity getSummonerToUpdate() {
List<SummonerEntity> summoners = query.setMaxResults(50).getResultList();
summonersToCheck.addAll(summoners);
} catch (Exception ex) {
// TODO handle exception
ex.printStackTrace();
getLogger().error("Error refilling lazy list", ex);
}
}
return summonersToCheck.poll();
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<File name="LOGFILE" fileName="./logs/INFO.log">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="LOGFILE"/>
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>

0 comments on commit c808aa9

Please sign in to comment.