FormattedSnuviLogger.java 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package me.hammerle.snuviscript;
  2. import java.util.Date;
  3. import me.hammerle.code.ISnuviLogger;
  4. import me.hammerle.code.Script;
  5. public class FormattedSnuviLogger implements ISnuviLogger {
  6. private final Date date = new Date();
  7. public FormattedSnuviLogger() {
  8. }
  9. @Override
  10. public void printException(Exception thrown, String function, Script script, int line) {
  11. System.err.println(createMessage("ERROR", codeLocation(script.getName(), function, line), thrown));
  12. }
  13. @Override
  14. public void printException(Exception thrown, String function, String scriptName, int line) {
  15. System.err.println(createMessage("ERROR", codeLocation(scriptName, function, line), thrown));
  16. }
  17. private static String codeLocation(String scriptName, String function, int line) {
  18. return scriptName + ":" + function + ":" + line;
  19. }
  20. @Override
  21. public void printWarning(String message) {
  22. System.err.println(createMessage("WARNING", message));
  23. }
  24. @Override
  25. public void printInfo(String message) {
  26. System.out.println(createMessage("INFO", message));
  27. }
  28. private String createMessage(String level, String message, Exception thrown) {
  29. return String.format("%1$s%n\t%2$s", createMessage(level, message), thrown);
  30. }
  31. private String createMessage(String level, String message) {
  32. date.setTime(System.currentTimeMillis());
  33. return String.format("[%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS] [SnuviScript|%2$s]: %3$s",
  34. date, level, message);
  35. }
  36. }