Skip to content

Commit ab14d67

Browse files
committed
Changed console logs to the slf4j api framework
Added an optional logging implementation of logback This closes #2
1 parent 95903bd commit ab14d67

File tree

5 files changed

+68
-15
lines changed

5 files changed

+68
-15
lines changed

pom.xml

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,66 @@
1515

1616
<packaging>jar</packaging>
1717

18+
<dependencies>
19+
<dependency>
20+
<groupId>org.slf4j</groupId>
21+
<artifactId>slf4j-api</artifactId>
22+
<version>1.7.25</version>
23+
</dependency>
24+
25+
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
26+
<dependency>
27+
<groupId>ch.qos.logback</groupId>
28+
<artifactId>logback-classic</artifactId>
29+
<version>1.2.3</version>
30+
<optional>true</optional>
31+
</dependency>
32+
33+
34+
</dependencies>
35+
1836
<build>
1937
<plugins>
2038

2139
<!-- Set a compiler level -->
2240
<plugin>
2341
<groupId>org.apache.maven.plugins</groupId>
2442
<artifactId>maven-compiler-plugin</artifactId>
25-
<version>2.3.2</version>
43+
<version>3.6.1</version>
2644
<configuration>
2745
<source>${jdk.version}</source>
2846
<target>${jdk.version}</target>
2947
</configuration>
3048
</plugin>
3149

32-
<!-- Make this jar executable -->
50+
<!-- Maven Assembly Plugin -->
3351
<plugin>
3452
<groupId>org.apache.maven.plugins</groupId>
35-
<artifactId>maven-jar-plugin</artifactId>
53+
<artifactId>maven-assembly-plugin</artifactId>
54+
<version>3.0.0</version>
3655
<configuration>
56+
<!-- get all project dependencies -->
57+
<descriptorRefs>
58+
<descriptorRef>jar-with-dependencies</descriptorRef>
59+
</descriptorRefs>
60+
<!-- MainClass in mainfest make a executable jar -->
3761
<archive>
3862
<manifest>
39-
<!-- Jar file entry point -->
4063
<mainClass>org.chaiware.app.TextToEmotion</mainClass>
4164
</manifest>
4265
</archive>
66+
4367
</configuration>
68+
<executions>
69+
<execution>
70+
<id>make-assembly</id>
71+
<!-- bind to the packaging phase -->
72+
<phase>package</phase>
73+
<goals>
74+
<goal>single</goal>
75+
</goals>
76+
</execution>
77+
</executions>
4478
</plugin>
4579

4680
</plugins>

src/main/java/org/chaiware/app/TextToEmotion.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,23 @@
44

55
import org.chaiware.emotion.EmotionalState;
66
import org.chaiware.emotion.Empathyscope;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
79

810
/** This class is the class you should use in order to parse your text for emotions, it contains static methods */
911
public class TextToEmotion {
1012

13+
private static Logger logger = LoggerFactory.getLogger(TextToEmotion.class);
14+
1115
/** For internal use, in order to try the app and see that it is working while developing it */
1216
public static void main(String[] args ) throws Exception {
1317
if (args.length < 1) {
14-
System.out.println("Please send an argument with the string you want to sense for emotion");
18+
logger.info("Please send an argument with the string you want to sense for emotion");
1519
System.exit(0);
1620
}
1721
String text = args[0];
1822

19-
System.out.println(textToEmotion(text));
23+
logger.info(textToEmotion(text).toString());
2024
}
2125

2226
/** Use this method in order to analyze any text for emotions */
@@ -26,7 +30,7 @@ public static EmotionalState textToEmotion(String text) throws Exception {
2630
try {
2731
sentenceState = Empathyscope.getInstance().feel(text);
2832
} catch (IOException e) {
29-
e.printStackTrace();
33+
logger.debug("TextToEmotion failure", e);
3034
throw new Exception("TextToEmotion failed to start (probably due to failure of loading its internal files)");
3135
}
3236

src/main/java/org/chaiware/emotion/Empathyscope.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.chaiware.emotion.util.HeuristicsUtility;
99
import org.chaiware.emotion.util.LexicalUtility;
1010
import org.chaiware.emotion.util.ParsingUtility;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
1113

1214
/**
1315
* Defines logic for transferring textual affect information, emotional
@@ -16,11 +18,13 @@
1618
*/
1719
public class Empathyscope {
1820

21+
private static Logger logger = LoggerFactory.getLogger(Empathyscope.class);
1922
private static Empathyscope instance;
2023
private LexicalUtility lexUtil;
2124

2225
private Empathyscope() throws IOException {
2326
lexUtil = LexicalUtility.getInstance();
27+
logger.info("Empathy Scope Instantiated");
2428
}
2529

2630
/**
@@ -53,7 +57,7 @@ public EmotionalState feel(String text) throws IOException {
5357

5458
for (String sentence : sentences) {
5559

56-
System.out.println("- " + sentence);
60+
logger.debug("- " + sentence);
5761

5862
// we imply 6 heuristic rules to adjust emotive weights of the words:
5963
// (1) more exclamation signs in a sentence => more intensive emotive weights

src/main/java/org/chaiware/emotion/util/LexicalUtility.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88

99
import org.chaiware.emotion.AffectWord;
1010
import org.chaiware.util.PropertiesManager;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
1113

1214
/**
1315
* Utility class for some text processing algorithms (Singleton)
1416
*/
1517
public class LexicalUtility {
1618

19+
private static Logger logger = LoggerFactory.getLogger(LexicalUtility.class);
1720
private static LexicalUtility instance;
1821

1922
private String FILENAME_LEXICON = "/data/lex/lexicon.txt";
@@ -36,6 +39,8 @@ private LexicalUtility() throws IOException {
3639
intensityModifiers = ParsingUtility.splitWords(pm.getProperty("intensity.modifiers"), ", ");
3740
parseLexiconFile(affectWords, FILENAME_LEXICON);
3841
parseLexiconFile(emoticons, FILENAME_EMOTICONS);
42+
43+
logger.debug("Lexical Utility Instantiated");
3944
}
4045

4146
/**
@@ -54,15 +59,18 @@ public static LexicalUtility getInstance() throws IOException {
5459

5560
private void parseLexiconFile(List<AffectWord> wordList, String fileName) throws IOException {
5661

57-
try (BufferedReader in = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(fileName), "UTF8"))){
62+
try (BufferedReader in = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream(fileName), "UTF8"))) {
63+
5864
String line = in.readLine();
5965
while (line != null) {
6066
AffectWord record = parseLine(line);
6167
wordList.add(record);
6268
line = in.readLine();
6369
}
70+
71+
logger.debug("Parsed lexicon file: {}", fileName);
6472
}
65-
}
73+
}
6674

6775
/**
6876
* Parses one line of the Lexicon and returns the {@link AffectWord}

src/main/java/org/chaiware/util/PropertiesManager.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.chaiware.util;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
import java.io.File;
4-
import java.io.FileNotFoundException;
57
import java.io.FileOutputStream;
68
import java.io.IOException;
79
import java.util.Properties;
@@ -10,7 +12,8 @@
1012
* A class for handling XML property files.
1113
*/
1214
public class PropertiesManager {
13-
15+
16+
Logger logger = LoggerFactory.getLogger(PropertiesManager.class);
1417
private Properties props;
1518
private File file;
1619

@@ -21,13 +24,13 @@ public class PropertiesManager {
2124
*/
2225

2326
public PropertiesManager(String fileName) {
24-
props = new Properties();
27+
props = new Properties();
2528

2629
try {
2730
props.loadFromXML(this.getClass().getResourceAsStream(fileName));
2831
} catch (IOException e) {
2932
props = new Properties();
30-
e.printStackTrace();
33+
logger.error("Failed loading properties", e);
3134
}
3235
}
3336

@@ -105,7 +108,7 @@ public void save(){
105108
try {
106109
props.storeToXML(new FileOutputStream(file), null);
107110
} catch (Exception e) {
108-
e.printStackTrace();
111+
logger.error("Failed saving properties", e);
109112
}
110113
}
111114
}

0 commit comments

Comments
 (0)