added logging

This commit is contained in:
aj 2019-05-02 10:04:35 +01:00
parent f4f2fbf4e6
commit d80ba0c3ce
13 changed files with 296 additions and 133 deletions

View File

@ -1,5 +1,11 @@
package sarsoo.fmframework.fm;
import sarsoo.fmframework.log.Log;
import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.console.ConsoleHandler;
import sarsoo.fmframework.log.entry.ErrorEntry;
import sarsoo.fmframework.log.entry.InfoEntry;
import sarsoo.fmframework.log.entry.LogEntry;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Album.AlbumBuilder;
import sarsoo.fmframework.music.Artist;
@ -8,7 +14,6 @@ import sarsoo.fmframework.music.FMObj;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.music.Track.TrackBuilder;
import sarsoo.fmframework.music.Wiki;
import sarsoo.fmframework.util.ConsoleHandler;
import java.util.HashMap;
@ -49,8 +54,9 @@ public class FmNetwork {
* @return Album
*/
public Album getAlbum(String name, String artist) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getAlbum: " + name + " " + artist);
Log log = Logger.getLog();
log.log(new LogEntry("getAlbum").addArg(name).addArg(artist));
HashMap<String, String> parameters = new HashMap<String, String>();
@ -77,43 +83,32 @@ public class FmNetwork {
try {
builder.setMbid(albumJson.getString("mbid"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
log.log(new InfoEntry("getAlbum").addArg("no mbid for").addArg(nameIn).addArg(e.getMessage()));
}
try {
builder.setUrl(albumJson.getString("url"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No Url for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + nameIn + " , " + e.getMessage());
log.log(new InfoEntry("getAlbum").addArg("no url for").addArg(nameIn).addArg(e.getMessage()));
}
try {
builder.setListeners(albumJson.getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
log.log(new InfoEntry("getAlbum").addArg("no listeners for").addArg(nameIn).addArg(e.getMessage()));
}
try {
builder.setPlayCount(albumJson.getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
log.log(new InfoEntry("getAlbum").addArg("no play count for").addArg(nameIn).addArg(e.getMessage()));
}
try {
builder.setUserPlayCount(albumJson.getInt("userplaycount"));
} catch (JSONException e) {
log.log(new InfoEntry("getAlbum").addArg("no user play count for").addArg(nameIn)
.addArg(e.getMessage()));
}
try {
@ -125,19 +120,13 @@ public class FmNetwork {
builder.setWiki(wiki);
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
log.log(new InfoEntry("getAlbum").addArg("no wiki for").addArg(nameIn).addArg(e.getMessage()));
}
return builder.build();
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: Album Name Not Found, " + e.getMessage());
else
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
log.log(new InfoEntry("getAlbum").addArg("album name not found").addArg(e.getMessage()));
}
return null;
@ -151,8 +140,9 @@ public class FmNetwork {
* @return Artist
*/
public Artist getArtist(String name) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getArtist: " + name);
Log log = Logger.getLog();
log.log(new LogEntry("getArtist").addArg(name));
HashMap<String, String> parameters = new HashMap<String, String>();
@ -176,44 +166,34 @@ public class FmNetwork {
try {
builder.setMbid(artistJson.getString("mbid"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No MBID for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No MBID for " + artistName + " , " + e.getMessage());
log.log(new InfoEntry("getArtist").addArg("no mbid for").addArg(artistName).addArg(e.getMessage()));
}
try {
builder.setUrl(artistJson.getString("url"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No Url for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + artistName + " , " + e.getMessage());
log.log(new InfoEntry("getArtist").addArg("no url for").addArg(artistName).addArg(e.getMessage()));
}
try {
builder.setListeners(artistJson.getJSONObject("stats").getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No listeners for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + artistName + " , " + e.getMessage());
log.log(new InfoEntry("getArtist").addArg("no listeners for").addArg(artistName)
.addArg(e.getMessage()));
}
try {
builder.setPlayCount(artistJson.getJSONObject("stats").getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole()
.write("ERROR: No play count for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + artistName + " , " + e.getMessage());
log.log(new InfoEntry("getArtist").addArg("no play count for").addArg(artistName)
.addArg(e.getMessage()));
}
try {
builder.setUserPlayCount(artistJson.getJSONObject("stats").getInt("userplaycount"));
} catch (JSONException e) {
log.log(new InfoEntry("getArtist").addArg("no user play count for").addArg(artistName)
.addArg(e.getMessage()));
}
try {
@ -225,19 +205,13 @@ public class FmNetwork {
builder.setWiki(wiki);
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No wiki for " + artistName + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + artistName + " , " + e.getMessage());
log.log(new InfoEntry("getArtist").addArg("no wiki for").addArg(artistName).addArg(e.getMessage()));
}
return builder.build();
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: Arist Name Not Found, " + e.getMessage());
else
System.err.println("ERROR: Arist Name Not Found, " + e.getMessage());
log.log(new InfoEntry("getArtist").addArg("artist name not found").addArg(e.getMessage()));
}
return null;
@ -252,8 +226,9 @@ public class FmNetwork {
* @return Track
*/
public Track getTrack(String name, String artist) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getTrack: " + name + " " + artist);
Log log = Logger.getLog();
log.log(new LogEntry("getTrack").addArg(name).addArg(artist));
HashMap<String, String> parameters = new HashMap<String, String>();
@ -280,43 +255,32 @@ public class FmNetwork {
try {
builder.setMbid(trackJson.getString("mbid"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No MBID for " + nameIn + " , " + e.getMessage());
log.log(new InfoEntry("getTrack").addArg("no mbid for").addArg(nameIn).addArg(e.getMessage()));
}
try {
builder.setUrl(trackJson.getString("url"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No Url for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No Url for " + nameIn + " , " + e.getMessage());
log.log(new InfoEntry("getTrack").addArg("no url for").addArg(nameIn).addArg(e.getMessage()));
}
try {
builder.setListeners(trackJson.getInt("listeners"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No listeners for " + nameIn + " , " + e.getMessage());
log.log(new InfoEntry("getTrack").addArg("no listeners for").addArg(nameIn).addArg(e.getMessage()));
}
try {
builder.setPlayCount(trackJson.getInt("playcount"));
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No play count for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No play count for " + nameIn + " , " + e.getMessage());
log.log(new InfoEntry("getTrack").addArg("no play count for").addArg(nameIn).addArg(e.getMessage()));
}
try {
builder.setUserPlayCount(trackJson.getInt("userplaycount"));
} catch (JSONException e) {
log.log(new InfoEntry("getTrack").addArg("no user play count for").addArg(nameIn)
.addArg(e.getMessage()));
}
try {
@ -328,19 +292,13 @@ public class FmNetwork {
builder.setWiki(wiki);
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
else
System.err.println("ERROR: No wiki for " + nameIn + " , " + e.getMessage());
log.log(new InfoEntry("getTrack").addArg("no wiki for").addArg(nameIn).addArg(e.getMessage()));
}
return builder.build();
} catch (JSONException e) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR: Album Name Not Found, " + e.getMessage());
else
System.err.println("ERROR: Album Name Not Found, " + e.getMessage());
log.log(new InfoEntry("getTrack").addArg("track name not found").addArg(e.getMessage()));
}
return null;
@ -353,8 +311,8 @@ public class FmNetwork {
* @return Refreshed Album
*/
public Album refresh(Album album) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>refreshAlbum: " + album.getName() + " " + album.getArtist().getName());
Logger.getLog().log(new LogEntry("refreshAlbum").addArg(album.getName()).addArg(album.getArtist().getName()));
return getAlbum(album.getName(), album.getArtist().getName());
}
@ -366,8 +324,8 @@ public class FmNetwork {
* @return Refreshed Artist
*/
public Artist refresh(Artist artist) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>refreshArtist: " + artist.getName());
Logger.getLog().log(new LogEntry("refreshArtist").addArg(artist.getName()));
return getArtist(artist.getName());
}
@ -379,8 +337,8 @@ public class FmNetwork {
* @return Refreshed Track
*/
public Track refresh(Track track) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>refreshTrack: " + track.getName() + " " + track.getArtist().getName());
Logger.getLog().log(new LogEntry("refreshTrack").addArg(track.getName()).addArg(track.getArtist().getName()));
Track refreshedTrack = getTrack(track.getName(), track.getArtist().getName());
@ -440,11 +398,7 @@ public class FmNetwork {
return new JSONObject(response.getBody().toString());
} else {
System.out.println(response.getBody());
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write("ERROR : HTTP Request Error " + response.getStatus());
else
System.err.println("ERROR : HTTP Request Error " + response.getStatus());
Logger.getLog().logError(new ErrorEntry("HTTP Get").setErrorCode(response.getStatus()));
return null;
}
} catch (UnirestException e) {
@ -453,15 +407,4 @@ public class FmNetwork {
return null;
}
protected class RequestParam {
int intparam;
String strparam;
public RequestParam(int param) {
}
}
}

View File

@ -8,10 +8,14 @@ import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONObject;
import sarsoo.fmframework.log.Log;
import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.console.ConsoleHandler;
import sarsoo.fmframework.log.entry.InfoEntry;
import sarsoo.fmframework.log.entry.LogEntry;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Tag;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.util.ConsoleHandler;
import sarsoo.fmframework.util.FMObjList;
public class FmUserNetwork extends FmNetwork {
@ -63,8 +67,8 @@ public class FmUserNetwork extends FmNetwork {
* @return User
*/
public User getUser() {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getUser");
Logger.getLog().log(new LogEntry("getUser"));
HashMap<String, String> parameters = new HashMap<String, String>();
@ -84,8 +88,8 @@ public class FmUserNetwork extends FmNetwork {
* @return User real name
*/
public String getUserRealName() {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getUserRealname");
Logger.getLog().log(new LogEntry("getUserRealName"));
return getUser().getRealName();
}
@ -96,8 +100,8 @@ public class FmUserNetwork extends FmNetwork {
* @return Total scrobble count
*/
public int getUserScrobbleCount() {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getUserScrobbleCount");
Logger.getLog().log(new LogEntry("getUserScrobbleCount"));
return getUser().getScrobbleCount();
}
@ -108,8 +112,8 @@ public class FmUserNetwork extends FmNetwork {
* @return Last track
*/
public Track getLastTrack() {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getLastTrack");
Logger.getLog().log(new LogEntry("getLastTrack"));
HashMap<String, String> parameters = new HashMap<String, String>();
@ -135,8 +139,8 @@ public class FmUserNetwork extends FmNetwork {
* @return Scrobble count today
*/
public int getScrobblesToday() {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getScrobblesToday");
Logger.getLog().log(new LogEntry("getScrobblesToday"));
LocalDate local = LocalDate.now();
@ -166,8 +170,9 @@ public class FmUserNetwork extends FmNetwork {
* @return Scrobble count
*/
public int getScrobbleCountByDate(int day, int month, int year) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getScrobblesByDate " + day + "." + month + "." + year);
Logger.getLog().log(new LogEntry("getScrobblesByDate").addArg(Integer.toString(day))
.addArg(Integer.toString(month)).addArg(Integer.toString(year)));
LocalDate startDate = LocalDate.of(year, month, day);
@ -197,8 +202,8 @@ public class FmUserNetwork extends FmNetwork {
* @return Scrobble count
*/
public int getScrobbleCountByDeltaDay(int day) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getScrobblesByDeltaDay " + day);
Logger.getLog().log(new LogEntry("getScrobblesByDeltaDay").addArg(Integer.toString(day)));
LocalDate local = LocalDate.now();
@ -228,8 +233,8 @@ public class FmUserNetwork extends FmNetwork {
* @return List of tags
*/
public ArrayList<Tag> getTags() {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getTags");
Logger.getLog().log(new LogEntry("getTags"));
HashMap<String, String> parameters = new HashMap<String, String>();
@ -265,8 +270,8 @@ public class FmUserNetwork extends FmNetwork {
* @return FMObjList of artists
*/
public FMObjList getTag(String tagName) {
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">>getTag: " + tagName);
Logger.getLog().log(new LogEntry("getTag").addArg(tagName));
HashMap<String, String> parameters = new HashMap<String, String>();
@ -277,8 +282,7 @@ public class FmUserNetwork extends FmNetwork {
JSONObject obj = makeGetRequest("user.getpersonaltags", parameters);
JSONArray tagJsonArray = obj.getJSONObject("taggings")
.getJSONObject("artists").getJSONArray("artist");
JSONArray tagJsonArray = obj.getJSONObject("taggings").getJSONObject("artists").getJSONArray("artist");
JSONObject artistJson;
@ -293,8 +297,7 @@ public class FmUserNetwork extends FmNetwork {
Artist artist = getArtist(artistJson.getString("name"));
if (ConsoleHandler.isVerbose())
ConsoleHandler.getConsole().write(">Tag: " + tagName + ", " + artist.getName());
Logger.getLog().log(new InfoEntry("Tag").addArg(tagName).addArg(artist.getName()));
list.add(artist);

View File

@ -2,7 +2,7 @@ package sarsoo.fmframework.fx;
import javafx.application.Platform;
import javafx.scene.control.TextArea;
import sarsoo.fmframework.util.Console;
import sarsoo.fmframework.log.console.Console;
public class TextAreaConsole implements Console{

View File

@ -28,13 +28,15 @@ import sarsoo.fmframework.fx.tab.FMObjListEditTab;
import sarsoo.fmframework.fx.tab.FMObjListTab;
import sarsoo.fmframework.fx.tab.ScrobbleChartTab;
import sarsoo.fmframework.fx.tab.TrackTab;
import sarsoo.fmframework.log.Log;
import sarsoo.fmframework.log.Logger;
import sarsoo.fmframework.log.console.ConsoleHandler;
import sarsoo.fmframework.fx.FmFramework;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;
import sarsoo.fmframework.music.Tag;
import sarsoo.fmframework.music.Track;
import sarsoo.fmframework.net.Key;
import sarsoo.fmframework.util.ConsoleHandler;
import sarsoo.fmframework.util.FMObjList;
import sarsoo.fmframework.util.Reference;
import sarsoo.fmframework.util.tagpool.TagPool;
@ -54,7 +56,8 @@ public class RootController {
public void initialize() {
// Reference.setUserName("sarsoo");
ConsoleHandler.setVerbose(TextAreaConsole.getInstance());
Logger.setLog(new Log(TextAreaConsole.getInstance(), false));
// ConsoleHandler.setVerbose(TextAreaConsole.getInstance());
refresh();
}

View File

@ -0,0 +1,68 @@
package sarsoo.fmframework.log;
import java.util.ArrayList;
import sarsoo.fmframework.log.console.Console;
import sarsoo.fmframework.log.entry.ErrorEntry;
import sarsoo.fmframework.log.entry.InfoEntry;
import sarsoo.fmframework.log.entry.LogEntry;
public class Log {
private ArrayList<Console> consoles = new ArrayList<Console>();
private Boolean writeToSTDOut;
private ArrayList<LogEntry> logList = new ArrayList<LogEntry>();
private ArrayList<InfoEntry> infoList = new ArrayList<InfoEntry>();
private ArrayList<ErrorEntry> errorList = new ArrayList<ErrorEntry>();
public Log() {
writeToSTDOut = true;
}
public Log(Console console, Boolean toSTD) {
consoles.add(console);
writeToSTDOut = toSTD;
}
public void clearConsoles() {
consoles.clear();
writeToSTDOut = true;
}
public void log(LogEntry entry) {
logList.add(entry);
writeLog(entry.toString());
}
public void logInfo(InfoEntry entry) {
infoList.add(entry);
writeLog(entry.toString());
}
public void logError(ErrorEntry entry) {
errorList.add(entry);
writeLog(entry.toString());
}
protected void writeLog(String logString) {
if (consoles.size() > 0) {
for (Console i : consoles) {
i.write(logString);
}
}
if (writeToSTDOut == true) {
System.out.println(logString);
}
}
}

View File

@ -0,0 +1,15 @@
package sarsoo.fmframework.log;
public class Logger {
private static Log log = new Log();
public static Log getLog() {
return log;
}
public static void setLog(Log logIn) {
log = logIn;
}
}

View File

@ -1,4 +1,4 @@
package sarsoo.fmframework.util;
package sarsoo.fmframework.log.console;
public interface Console {

View File

@ -1,4 +1,4 @@
package sarsoo.fmframework.util;
package sarsoo.fmframework.log.console;
public class ConsoleHandler {

View File

@ -0,0 +1,38 @@
package sarsoo.fmframework.log.entry;
public class ErrorEntry extends LogEntry {
protected int errorCode;
public ErrorEntry(String methodIn) {
super(methodIn);
}
public ErrorEntry setErrorCode(int error) {
errorCode = error;
return this;
}
public String toString() {
String logString = String.format("%s !!%s", timestamp, method);
if (errorCode != 0) {
logString += String.format(" (%i)", errorCode);
}
if (args != null) {
if (args.size() > 0) {
logString += ":";
for (String i : args) {
logString += " " + i;
}
}
}
return logString;
}
}

View File

@ -0,0 +1,27 @@
package sarsoo.fmframework.log.entry;
public class InfoEntry extends LogEntry {
public InfoEntry(String methodIn) {
super(methodIn);
}
public String toString() {
String logString = String.format("%s \t>%s", timestamp, method);
if (args != null) {
if (args.size() > 0) {
logString += ":";
for (String i : args) {
logString += " " + i;
}
}
}
return logString;
}
}

View File

@ -0,0 +1,60 @@
package sarsoo.fmframework.log.entry;
import java.time.LocalDateTime;
import java.util.ArrayList;
public class LogEntry {
protected String method;
protected ArrayList<String> args;
protected LocalDateTime timestamp;
public LogEntry(String methodIn) {
method = methodIn;
timestamp = LocalDateTime.now();
}
public String getMethod() {
return method;
}
public ArrayList<String> getArgs() {
return args;
}
public LogEntry setArgs(ArrayList<String> argsIn) {
args = argsIn;
return this;
}
public LocalDateTime getTimeStamp() {
return timestamp;
}
public LogEntry addArg(String arg) {
if (args == null) {
args = new ArrayList<String>();
}
args.add(arg);
return this;
}
public String toString() {
String logString = String.format("%s >>%s", timestamp, method);
if (args != null) {
if (args.size() > 0) {
logString += ":";
for (String i : args) {
logString += " " + i;
}
}
}
return logString;
}
}

View File

@ -2,6 +2,7 @@ package sarsoo.fmframework.util;
import java.util.ArrayList;
import sarsoo.fmframework.log.console.Console;
import sarsoo.fmframework.music.Album;
import sarsoo.fmframework.music.Artist;

View File

@ -12,4 +12,9 @@ public class FmUserNetworkTest {
public void testNetworkInstantiate() {
assertNotNull(new FmUserNetwork(Key.getKey(), "sarsoo"));
}
@Test
public void testGetLastTrack() {
assertNotNull(new FmUserNetwork(Key.getKey(), "sarsoo").getLastTrack());
}
}