-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Logger and use it to report progress loading ingestion package (#19)
Prepend server response with INFO/WARNING/ERROR. Provide Java methods to parse level/message from a log line.
- Loading branch information
Showing
16 changed files
with
204 additions
and
98 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
connectionUtils/src/main/java/com/ge/research/semtk/utility/Logger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.ge.research.semtk.utility; | ||
|
||
import java.io.PrintWriter; | ||
|
||
public class Logger { | ||
|
||
public enum Levels {INFO, WARNING, ERROR}; | ||
private PrintWriter printWriter; | ||
|
||
/** | ||
* Constructor | ||
*/ | ||
public Logger(PrintWriter printWriter) { | ||
this.printWriter = printWriter; | ||
} | ||
|
||
/** | ||
* Log an info message | ||
*/ | ||
public void info(String s) { | ||
log(s, Levels.INFO); | ||
} | ||
|
||
/** | ||
* Log a warning message | ||
*/ | ||
public void warning(String s) { | ||
log(s, Levels.WARNING); | ||
} | ||
|
||
/** | ||
* Log an error message | ||
*/ | ||
public void error(String s) { | ||
log(s, Levels.ERROR); | ||
} | ||
|
||
/** | ||
* Get the logging level from a logged line | ||
* @param line (e.g. "INFO: performed an action") | ||
* @return the level (e.g. Levels.INFO) | ||
*/ | ||
public static Levels getLevel(String line) throws Exception { | ||
String levelStr = line.substring(0, line.indexOf(":")); | ||
if(levelStr.equals(Levels.INFO.name())) { return Levels.INFO; } | ||
if(levelStr.equals(Levels.WARNING.name())) { return Levels.WARNING; } | ||
if(levelStr.equals(Levels.ERROR.name())) { return Levels.ERROR; } | ||
throw new Exception("Unrecognized level: " + levelStr); | ||
} | ||
|
||
/** | ||
* Get the message from a logged line | ||
* @param line (e.g. "INFO: performed an action") | ||
* @return the message, e.g. "performed an action" | ||
*/ | ||
public static String getMessage(String line) { | ||
return line.substring(line.indexOf(": ") + 2); | ||
} | ||
|
||
// log the message | ||
private void log(String s, Levels level) { | ||
printWriter.println(level.name() + ": " + s); | ||
printWriter.flush(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.