Skip to content

Commit 7a18c01

Browse files
committed
feat: Changing logging approach - to use different frameworks for FX and CLI modes.
1 parent 7a62b21 commit 7a18c01

File tree

8 files changed

+100
-19
lines changed

8 files changed

+100
-19
lines changed

src/main/java/com/github/exadmin/cyberferret/CyberFerretCLI.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.exadmin.cyberferret;
22

3+
import com.github.exadmin.cyberferret.cli.RunnableCheckOnlineDictionaryProxy;
34
import com.github.exadmin.cyberferret.utils.ConsoleUtils;
45
import com.github.exadmin.cyberferret.utils.FileUtils;
56

@@ -26,12 +27,12 @@ private static void terminateAppWithErrorCode() {
2627

2728
public static void main(String[] args) {
2829
// Overall logic
29-
// step1: Check required program arguments are set
30-
// step2: Check required system env variables are set
31-
// step2: Ensure actual dictionary is downloaded
32-
// step3: Download dictionary if required
33-
// step4: Decrypt dictionary
34-
// step5: Run check over the git-repository
30+
// Step1: Check required program arguments are set
31+
// Step2: Check required system env variables are set
32+
// Step3: Ensure actual dictionary is downloaded
33+
// Step4: Download dictionary if required
34+
// Step5: Decrypt dictionary
35+
// Step6: Run check over the git-repository
3536

3637

3738
// Step1: Check required program arguments are set
@@ -48,14 +49,18 @@ public static void main(String[] args) {
4849
terminateAppWithErrorCode();
4950
}
5051

51-
// step2: Check required system env variables are set
52+
// Step2: Check required system env variables are set
5253
final String pass = System.getenv(SYS_ENV_VAR_PASSWORD);
5354
if (pass == null || pass.isEmpty()) {
5455
ConsoleUtils.error("Missing environment variable {}", SYS_ENV_VAR_PASSWORD);
5556
printUsage();
5657
terminateAppWithErrorCode();
5758
}
5859

60+
// Step3: Ensure actual dictionary is downloaded
61+
Runnable dictionaryDownloader = new RunnableCheckOnlineDictionaryProxy();
62+
dictionaryDownloader.run();
63+
5964
ConsoleUtils.debug("Scan is fakely completed");
6065
}
6166
}

src/main/java/com/github/exadmin/cyberferret/async/ARunnable.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import org.slf4j.Logger;
44

5-
public abstract class ARunnable implements Runnable {
5+
public abstract class ARunnable implements Runnable, Loggable {
66
protected Runnable beforeStart;
77
protected Runnable afterFinished;
88

@@ -15,7 +15,7 @@ public void setAfterFinished(Runnable afterFinished) {
1515
}
1616

1717
protected abstract void _run() throws Exception;
18-
protected abstract Logger getLog();
18+
1919

2020
@Override
2121
public final void run() {
@@ -33,4 +33,29 @@ public final void startNowInNewThread() {
3333
thread.setDaemon(true);
3434
thread.start();
3535
}
36+
37+
@Override
38+
public void logError(String msg, Object... binds) {
39+
if (getLog() != null) getLog().error(msg, binds);
40+
}
41+
42+
@Override
43+
public void logWarn(String msg, Object... binds) {
44+
if (getLog() != null) getLog().warn(msg, binds);
45+
}
46+
47+
@Override
48+
public void logInfo(String msg, Object... binds) {
49+
if (getLog() != null) getLog().info(msg, binds);
50+
}
51+
52+
@Override
53+
public void logDebug(String msg, Object... binds) {
54+
if (getLog() != null) getLog().debug(msg, binds);
55+
}
56+
57+
@Override
58+
public void logTrace(String msg, Object... binds) {
59+
if (getLog() != null) getLog().trace(msg, binds);
60+
}
3661
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.exadmin.cyberferret.async;
2+
3+
import org.slf4j.Logger;
4+
5+
public interface Loggable {
6+
Logger getLog();
7+
void logError(String msg, Object ... binds);
8+
void logWarn(String msg, Object ... binds);
9+
void logInfo(String msg, Object ... binds);
10+
void logDebug(String msg, Object ... binds);
11+
void logTrace(String msg, Object ... binds);
12+
}

src/main/java/com/github/exadmin/cyberferret/async/RunnableCheckOnlineDictionary.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
import java.io.InputStream;
1515

1616
public class RunnableCheckOnlineDictionary extends ARunnable {
17-
private static final Logger log = LoggerFactory.getLogger(RunnableCheckOnlineDictionary.class);
17+
private volatile static Logger log = null;
1818

1919
@Override
2020
protected void _run() throws Exception {
21-
log.info("Checking if new online dictionary exists");
21+
logInfo("Checking if new online dictionary exists");
2222

2323
File savePath = new File(FxConstants.DICTIONARY_FILE_PATH_ENCRYPTED);
2424

2525
if (savePath.exists()) {
2626
boolean wasDeleted = savePath.delete();
27-
if (wasDeleted) log.info("Previous version of downloaded copy was cleaned by path {}", savePath);
27+
if (wasDeleted) logInfo("Previous version of downloaded copy was cleaned by path {}", savePath);
2828
}
2929

30-
log.info("Downloading latest online dictionary");
30+
logInfo("Downloading latest online dictionary");
3131
try (CloseableHttpClient client = HttpClients.createDefault()) {
3232
HttpGet request = new HttpGet(FxConstants.CYBER_FERRET_ONLINE_DICTIONARY_URL);
3333
try (CloseableHttpResponse response = client.execute(request);
@@ -38,15 +38,18 @@ protected void _run() throws Exception {
3838
while ((byteRead = inputStream.read()) != -1) {
3939
writer.write(byteRead);
4040
}
41-
log.info("File was downloaded successfully and saved in {}", savePath.getAbsoluteFile());
41+
logInfo("File was downloaded successfully and saved in {}", savePath.getAbsoluteFile());
4242
}
4343
} catch (IOException ex) {
44-
log.error("Error while downloading online dictionary file", ex);
44+
logError("Error while downloading online dictionary file", ex);
4545
}
4646
}
4747

4848
@Override
49-
protected Logger getLog() {
50-
return null;
49+
public Logger getLog() {
50+
if (log == null) {
51+
log = LoggerFactory.getLogger(RunnableCheckOnlineDictionary.class);
52+
}
53+
return log;
5154
}
5255
}

src/main/java/com/github/exadmin/cyberferret/async/RunnableScanner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void setFxCallback(FxCallback fxCallback) {
6666
}
6767

6868
@Override
69-
protected Logger getLog() {
69+
public Logger getLog() {
7070
return log;
7171
}
7272

src/main/java/com/github/exadmin/cyberferret/async/RunnableSigsLoader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public boolean isReady() {
5252
}
5353

5454
@Override
55-
protected Logger getLog() {
55+
public Logger getLog() {
5656
return log;
5757
}
5858

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.exadmin.cyberferret.cli;
2+
3+
import com.github.exadmin.cyberferret.async.RunnableCheckOnlineDictionary;
4+
import com.github.exadmin.cyberferret.utils.ConsoleUtils;
5+
6+
public class RunnableCheckOnlineDictionaryProxy extends RunnableCheckOnlineDictionary {
7+
@Override
8+
public void logError(String msg, Object... binds) {
9+
ConsoleUtils.error(msg, binds);
10+
}
11+
12+
@Override
13+
public void logWarn(String msg, Object... binds) {
14+
throw new IllegalStateException("Not implemented yet");
15+
}
16+
17+
@Override
18+
public void logInfo(String msg, Object... binds) {
19+
ConsoleUtils.info(msg, binds);
20+
}
21+
22+
@Override
23+
public void logDebug(String msg, Object... binds) {
24+
ConsoleUtils.debug(msg, binds);
25+
}
26+
27+
@Override
28+
public void logTrace(String msg, Object... binds) {
29+
throw new IllegalStateException("Not implemented yet");
30+
}
31+
}

src/main/java/com/github/exadmin/cyberferret/utils/ConsoleUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
public class ConsoleUtils {
44

5+
public static void info(String msg, Object ... binds) {
6+
String result = "[INFO ] " + format(msg, binds);
7+
System.out.println(result);
8+
}
9+
510
public static void debug(String msg, Object ... binds) {
611
String result = "[DEBUG] " + format(msg, binds);
712
System.out.println(result);

0 commit comments

Comments
 (0)