Skip to content

Commit d4ca148

Browse files
committed
Force each class to use its own Logger instance for better logs (Fixes #2070)
1 parent a21303f commit d4ca148

File tree

79 files changed

+1167
-816
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+1167
-816
lines changed

src/main/java/com/rarchives/ripme/App.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
*/
4444
public class App {
4545

46-
public static final Logger logger = LogManager.getLogger(App.class);
46+
private static final Logger logger = LogManager.getLogger(App.class);
4747
public static String stringToAppendToFoldername = null;
4848
private static final History HISTORY = new History();
4949

@@ -140,7 +140,7 @@ private static void handleArguments(String[] args) throws IOException {
140140
Utils.setConfigString("history.location", historyLocation);
141141
logger.info("Set history file to " + historyLocation);
142142
}
143-
143+
144144
//Allow file overwriting
145145
if (cl.hasOption('w')) {
146146
Utils.setConfigBoolean("file.overwrite", true);
@@ -173,7 +173,7 @@ private static void handleArguments(String[] args) throws IOException {
173173
// change the default rips directory
174174
Utils.setConfigString("rips.directory", cl.getOptionValue('l'));
175175
}
176-
176+
177177
//Re-rip <i>all</i> previous albums
178178
if (cl.hasOption('r')) {
179179
// Re-rip all via command-line
@@ -386,7 +386,7 @@ private static void loadHistory() throws IOException {
386386
}
387387
}
388388

389-
/*
389+
/*
390390
* @see MainWindow.saveHistory
391391
*/
392392
private static void saveHistory() {

src/main/java/com/rarchives/ripme/ripper/AbstractHTMLRipper.java

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
import java.io.FileOutputStream;
55
import java.io.IOException;
66
import java.io.UnsupportedEncodingException;
7-
import java.net.*;
7+
import java.net.MalformedURLException;
8+
import java.net.URI;
9+
import java.net.URISyntaxException;
10+
import java.net.URL;
811
import java.nio.charset.StandardCharsets;
912
import java.nio.file.Files;
1013
import java.nio.file.Path;
@@ -16,19 +19,23 @@
1619
import java.util.List;
1720
import java.util.Map;
1821

22+
import org.apache.logging.log4j.LogManager;
23+
import org.apache.logging.log4j.Logger;
1924
import org.jsoup.nodes.Document;
2025

21-
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
22-
import com.rarchives.ripme.utils.Utils;
2326
import com.rarchives.ripme.ui.MainWindow;
2427
import com.rarchives.ripme.ui.RipStatusMessage;
28+
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
2529
import com.rarchives.ripme.utils.Http;
30+
import com.rarchives.ripme.utils.Utils;
2631

2732
/**
2833
* Simplified ripper, designed for ripping from sites by parsing HTML.
2934
*/
3035
public abstract class AbstractHTMLRipper extends AbstractRipper {
3136

37+
private static final Logger logger = LogManager.getLogger(AbstractHTMLRipper.class);
38+
3239
private final Map<URL, File> itemsPending = Collections.synchronizedMap(new HashMap<>());
3340
private final Map<URL, Path> itemsCompleted = Collections.synchronizedMap(new HashMap<>());
3441
private final Map<URL, String> itemsErrored = Collections.synchronizedMap(new HashMap<>());
@@ -116,7 +123,7 @@ protected boolean pageContainsAlbums(URL url) {
116123
public void rip() throws IOException, URISyntaxException {
117124
int index = 0;
118125
int textindex = 0;
119-
LOGGER.info("Retrieving " + this.url);
126+
logger.info("Retrieving " + this.url);
120127
sendUpdate(STATUS.LOADING_RESOURCE, this.url.toExternalForm());
121128
var doc = getCachedFirstPage();
122129

@@ -128,20 +135,20 @@ public void rip() throws IOException, URISyntaxException {
128135

129136
// We set doc to null here so the while loop below this doesn't fire
130137
doc = null;
131-
LOGGER.debug("Adding items from " + this.url + " to queue");
138+
logger.debug("Adding items from " + this.url + " to queue");
132139
}
133140

134141
List<String> doclocation = new ArrayList<>();
135142

136-
LOGGER.info("Got doc location " + doc.location());
143+
logger.info("Got doc location " + doc.location());
137144

138145
while (doc != null) {
139146

140-
LOGGER.info("Processing a doc...");
147+
logger.info("Processing a doc...");
141148

142149
// catch if we saw a doc location already, save the ones seen in a list
143150
if (doclocation.contains(doc.location())) {
144-
LOGGER.info("Already processed location " + doc.location() + " breaking");
151+
logger.info("Already processed location " + doc.location() + " breaking");
145152
break;
146153
}
147154
doclocation.add(doc.location());
@@ -151,7 +158,7 @@ public void rip() throws IOException, URISyntaxException {
151158
break;
152159
}
153160

154-
LOGGER.info("retrieving urls from doc");
161+
logger.info("retrieving urls from doc");
155162

156163
List<String> imageURLs = getURLsFromPage(doc);
157164
// If hasASAPRipping() returns true then the ripper will handle downloading the files
@@ -170,25 +177,25 @@ public void rip() throws IOException, URISyntaxException {
170177

171178
for (String imageURL : imageURLs) {
172179
index += 1;
173-
LOGGER.debug("Found image url #" + index + ": '" + imageURL + "'");
180+
logger.debug("Found image url #" + index + ": '" + imageURL + "'");
174181
downloadURL(new URI(imageURL).toURL(), index);
175182
if (isStopped() || isThisATest()) {
176183
break;
177184
}
178185
}
179186
}
180187
if (hasDescriptionSupport() && Utils.getConfigBoolean("descriptions.save", false)) {
181-
LOGGER.debug("Fetching description(s) from " + doc.location());
188+
logger.debug("Fetching description(s) from " + doc.location());
182189
List<String> textURLs = getDescriptionsFromPage(doc);
183190
if (!textURLs.isEmpty()) {
184-
LOGGER.debug("Found description link(s) from " + doc.location());
191+
logger.debug("Found description link(s) from " + doc.location());
185192
for (String textURL : textURLs) {
186193
if (isStopped() || isThisATest()) {
187194
break;
188195
}
189196

190197
textindex += 1;
191-
LOGGER.debug("Getting description from " + textURL);
198+
logger.debug("Getting description from " + textURL);
192199
String[] tempDesc = getDescription(textURL,doc);
193200

194201
if (tempDesc != null) {
@@ -204,11 +211,11 @@ public void rip() throws IOException, URISyntaxException {
204211
+ ".txt").exists();
205212

206213
if (Utils.getConfigBoolean("file.overwrite", false) || !fileExists) {
207-
LOGGER.debug("Got description from " + textURL);
214+
logger.debug("Got description from " + textURL);
208215
saveText(url, "", tempDesc[0], textindex, (tempDesc.length > 1 ? tempDesc[1] : filename));
209216
sleep(descSleepTime());
210217
} else {
211-
LOGGER.debug("Description from " + textURL + " already exists.");
218+
logger.debug("Description from " + textURL + " already exists.");
212219
}
213220
}
214221

@@ -224,14 +231,14 @@ public void rip() throws IOException, URISyntaxException {
224231
sendUpdate(STATUS.LOADING_RESOURCE, "next page");
225232
doc = getNextPage(doc);
226233
} catch (IOException e) {
227-
LOGGER.info("Can't get next page: " + e.getMessage());
234+
logger.info("Can't get next page: " + e.getMessage());
228235
break;
229236
}
230237
}
231238

232239
// If they're using a thread pool, wait for it.
233240
if (getThreadPool() != null) {
234-
LOGGER.debug("Waiting for threadpool " + getThreadPool().getClass().getName());
241+
logger.debug("Waiting for threadpool " + getThreadPool().getClass().getName());
235242
getThreadPool().waitForThreads();
236243
}
237244
waitForThreads();
@@ -296,12 +303,12 @@ private boolean saveText(URL url, String subdirectory, String text, int index, S
296303
out.write(text.getBytes());
297304
out.close();
298305
} catch (IOException e) {
299-
LOGGER.error("[!] Error creating save file path for description '" + url + "':", e);
306+
logger.error("[!] Error creating save file path for description '" + url + "':", e);
300307
return false;
301308
}
302-
LOGGER.debug("Downloading " + url + "'s description to " + saveFileAs);
309+
logger.debug("Downloading " + url + "'s description to " + saveFileAs);
303310
if (!saveFileAs.getParentFile().exists()) {
304-
LOGGER.info("[+] Creating directory: " + saveFileAs.getParent());
311+
logger.info("[+] Creating directory: " + saveFileAs.getParent());
305312
saveFileAs.getParentFile().mkdirs();
306313
}
307314
return true;
@@ -355,7 +362,7 @@ public boolean addURLToDownload(URL url, Path saveAs, String referrer, Map<Strin
355362
|| itemsCompleted.containsKey(url)
356363
|| itemsErrored.containsKey(url) )) {
357364
// Item is already downloaded/downloading, skip it.
358-
LOGGER.info("[!] Skipping " + url + " -- already attempted: " + Utils.removeCWD(saveAs));
365+
logger.info("[!] Skipping " + url + " -- already attempted: " + Utils.removeCWD(saveAs));
359366
return false;
360367
}
361368
if (shouldIgnoreURL(url)) {
@@ -370,7 +377,7 @@ public boolean addURLToDownload(URL url, Path saveAs, String referrer, Map<Strin
370377
Files.write(urlFile, text.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
371378
itemsCompleted.put(url, urlFile);
372379
} catch (IOException e) {
373-
LOGGER.error("Error while writing to " + urlFile, e);
380+
logger.error("Error while writing to " + urlFile, e);
374381
}
375382
}
376383
else {
@@ -423,7 +430,7 @@ public void downloadCompleted(URL url, Path saveAs) {
423430

424431
checkIfComplete();
425432
} catch (Exception e) {
426-
LOGGER.error("Exception while updating observer: ", e);
433+
logger.error("Exception while updating observer: ", e);
427434
}
428435
}
429436

@@ -486,20 +493,20 @@ public void setWorkingDir(URL url) throws IOException, URISyntaxException {
486493
path += File.separator;
487494
}
488495
String title = getAlbumTitle(this.url);
489-
LOGGER.debug("Using album title '" + title + "'");
496+
logger.debug("Using album title '" + title + "'");
490497

491498
title = Utils.filesystemSafe(title);
492499
path += title;
493500
path = Utils.getOriginalDirectory(path) + File.separator; // check for case sensitive (unix only)
494501

495502
this.workingDir = new File(path);
496503
if (!this.workingDir.exists()) {
497-
LOGGER.info("[+] Creating directory: " + Utils.removeCWD(this.workingDir.toPath()));
504+
logger.info("[+] Creating directory: " + Utils.removeCWD(this.workingDir.toPath()));
498505
if (!this.workingDir.mkdirs()) {
499506
throw new IOException("Failed creating dir: \"" + this.workingDir + "\"");
500507
}
501508
}
502-
LOGGER.debug("Set working directory to: " + this.workingDir);
509+
logger.debug("Set working directory to: " + this.workingDir);
503510
}
504511

505512
/**

src/main/java/com/rarchives/ripme/ripper/AbstractJSONRipper.java

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package com.rarchives.ripme.ripper;
22

3-
import com.rarchives.ripme.ui.RipStatusMessage;
4-
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
5-
import com.rarchives.ripme.utils.Utils;
6-
import org.json.JSONObject;
7-
83
import java.io.File;
94
import java.io.IOException;
105
import java.net.MalformedURLException;
@@ -21,11 +16,21 @@
2116
import java.util.List;
2217
import java.util.Map;
2318

19+
import org.apache.logging.log4j.LogManager;
20+
import org.apache.logging.log4j.Logger;
21+
import org.json.JSONObject;
22+
23+
import com.rarchives.ripme.ui.RipStatusMessage;
24+
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
25+
import com.rarchives.ripme.utils.Utils;
26+
2427
/**
2528
* Simplified ripper, designed for ripping from sites by parsing JSON.
2629
*/
2730
public abstract class AbstractJSONRipper extends AbstractRipper {
28-
31+
32+
private static final Logger logger = LogManager.getLogger(AbstractJSONRipper.class);
33+
2934
private Map<URL, File> itemsPending = Collections.synchronizedMap(new HashMap<URL, File>());
3035
private Map<URL, Path> itemsCompleted = Collections.synchronizedMap(new HashMap<URL, Path>());
3136
private Map<URL, String> itemsErrored = Collections.synchronizedMap(new HashMap<URL, String>());
@@ -65,18 +70,18 @@ public URL sanitizeURL(URL url) throws MalformedURLException, URISyntaxException
6570
@Override
6671
public void rip() throws IOException, URISyntaxException {
6772
int index = 0;
68-
LOGGER.info("Retrieving " + this.url);
73+
logger.info("Retrieving " + this.url);
6974
sendUpdate(STATUS.LOADING_RESOURCE, this.url.toExternalForm());
7075
JSONObject json = getFirstPage();
7176

7277
while (json != null) {
7378
List<String> imageURLs = getURLsFromJSON(json);
74-
79+
7580
if (alreadyDownloadedUrls >= Utils.getConfigInteger("history.end_rip_after_already_seen", 1000000000) && !isThisATest()) {
7681
sendUpdate(STATUS.DOWNLOAD_COMPLETE, "Already seen the last " + alreadyDownloadedUrls + " images ending rip");
7782
break;
7883
}
79-
84+
8085
// Remove all but 1 image
8186
if (isThisATest()) {
8287
while (imageURLs.size() > 1) {
@@ -92,9 +97,9 @@ public void rip() throws IOException, URISyntaxException {
9297
if (isStopped()) {
9398
break;
9499
}
95-
100+
96101
index += 1;
97-
LOGGER.debug("Found image url #" + index+ ": " + imageURL);
102+
logger.debug("Found image url #" + index+ ": " + imageURL);
98103
downloadURL(new URI(imageURL).toURL(), index);
99104
}
100105

@@ -106,14 +111,14 @@ public void rip() throws IOException, URISyntaxException {
106111
sendUpdate(STATUS.LOADING_RESOURCE, "next page");
107112
json = getNextPage(json);
108113
} catch (IOException | URISyntaxException e) {
109-
LOGGER.info("Can't get next page: " + e.getMessage());
114+
logger.info("Can't get next page: " + e.getMessage());
110115
break;
111116
}
112117
}
113118

114119
// If they're using a thread pool, wait for it.
115120
if (getThreadPool() != null) {
116-
LOGGER.debug("Waiting for threadpool " + getThreadPool().getClass().getName());
121+
logger.debug("Waiting for threadpool " + getThreadPool().getClass().getName());
117122
getThreadPool().waitForThreads();
118123
}
119124
waitForThreads();
@@ -126,11 +131,11 @@ protected String getPrefix(int index) {
126131
}
127132
return prefix;
128133
}
129-
134+
130135
/*
131136
* ------ Methods copied from AlbumRipper ------
132137
*/
133-
138+
134139
protected boolean allowDuplicates() {
135140
return false;
136141
}
@@ -159,7 +164,7 @@ public boolean addURLToDownload(URL url, Path saveAs, String referrer, Map<Strin
159164
|| itemsCompleted.containsKey(url)
160165
|| itemsErrored.containsKey(url) )) {
161166
// Item is already downloaded/downloading, skip it.
162-
LOGGER.info("[!] Skipping " + url + " -- already attempted: " + Utils.removeCWD(saveAs));
167+
logger.info("[!] Skipping " + url + " -- already attempted: " + Utils.removeCWD(saveAs));
163168
return false;
164169
}
165170
if (shouldIgnoreURL(url)) {
@@ -174,7 +179,7 @@ public boolean addURLToDownload(URL url, Path saveAs, String referrer, Map<Strin
174179
Files.write(urlFile, text.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
175180
itemsCompleted.put(url, urlFile);
176181
} catch (IOException e) {
177-
LOGGER.error("Error while writing to " + urlFile, e);
182+
logger.error("Error while writing to " + urlFile, e);
178183
}
179184
}
180185
else {
@@ -227,7 +232,7 @@ public void downloadCompleted(URL url, Path saveAs) {
227232

228233
checkIfComplete();
229234
} catch (Exception e) {
230-
LOGGER.error("Exception while updating observer: ", e);
235+
logger.error("Exception while updating observer: ", e);
231236
}
232237
}
233238

@@ -292,16 +297,16 @@ public void setWorkingDir(URL url) throws IOException, URISyntaxException {
292297
} else {
293298
title = super.getAlbumTitle(this.url);
294299
}
295-
LOGGER.debug("Using album title '" + title + "'");
300+
logger.debug("Using album title '" + title + "'");
296301

297302
title = Utils.filesystemSafe(title);
298303
wd = wd.resolve(title);
299304
if (!Files.exists(wd)) {
300-
LOGGER.info("[+] Creating directory: " + Utils.removeCWD(wd));
305+
logger.info("[+] Creating directory: " + Utils.removeCWD(wd));
301306
Files.createDirectory(wd);
302307
}
303308
this.workingDir = wd.toFile();
304-
LOGGER.info("Set working directory to: {}", this.workingDir);
309+
logger.info("Set working directory to: {}", this.workingDir);
305310
}
306311

307312
/**
@@ -329,5 +334,5 @@ public String getStatusText() {
329334
return sb.toString();
330335
}
331336

332-
337+
333338
}

0 commit comments

Comments
 (0)