Advertisement
Guest User

ErrorLogCode

a guest
Mar 12th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Static is optional, depending on how you like to make your Objects. VARIABLES throwable: The exception, severity: How much of a concern this error should be.
  2.     //customReason: Add some additional notes for yourself to help your fix the error. logToConsole: Log the stacktrace to console along with logging to file,
  3.     //logFullyToConsole: logToConsole must be true. Logs the full detailed report to console, along with writing it to the file.
  4.     //notifyAdmin: If the error is such a factor, set this to true, and it will send a message to all online admin a suppressed detailed report.
  5.     //Permission node for admin to see the message is error.notify.
  6.     public /*static*/ void logError(Plugin plugin, Throwable throwable, String severity, String customReason, boolean logToConsole, boolean logFullyToConsole, boolean notifyAdmin){
  7.         String date = new java.text.SimpleDateFormat("MM_dd_yyyy-HH:mm:ss").format(new java.util.Date (System.currentTimeMillis()));
  8.         if(notifyAdmin){
  9.             Iterator<ProxiedPlayer> ppi = BungeeCord.getInstance().getPlayers().iterator();
  10.             while(ppi.hasNext()){
  11.                 ProxiedPlayer pp = ppi.next();
  12.                 if(pp instanceof ProxiedPlayer){
  13.                     if(pp.hasPermission("error.notify")){
  14.                         pp.sendMessage(ChatColor.RED + "--------SEVERE BUNGEE ERROR------");
  15.                         pp.sendMessage(ChatColor.RED + "CAUSE: " + plugin.getDescription().getName());
  16.                         pp.sendMessage(ChatColor.RED + "DEV INFO: " + customReason);
  17.                         for(int i = 0; i < throwable.getStackTrace().length; i++){
  18.                              pp.sendMessage(ChatColor.RED + "" + throwable.getStackTrace()[i]);
  19.                          }
  20.                     }
  21.                 }
  22.             }
  23.         }
  24.         if(logToConsole && !logFullyToConsole){
  25.             throwable.printStackTrace();
  26.         }
  27.         if(logFullyToConsole && logToConsole){
  28.             System.out.println("================================================");
  29.              System.out.println("-----------General Info--------------");
  30.               System.out.println("Error caused by: " + plugin.getDescription().getName());
  31.              String[] pro = {"java.version", "java.vm.version", "java.runtime.version"};
  32.               Properties properties = System.getProperties();
  33.               for (int i = 0; i < pro.length; i++) {
  34.               System.out.println(pro[i] + ": " + properties.getProperty(pro[i]));
  35.               }
  36.              System.out.println("Unix Time of Error: " + System.currentTimeMillis());
  37.              System.out.println("Formatted Time of Error: " + date);
  38.              System.out.println();
  39.              System.out.println("-----------Instance Info-------------");
  40.              System.out.println("BungeeCord Version: " + BungeeCord.getInstance().getVersion());
  41.              System.out.println("Online State: " + BungeeCord.getInstance().config.isOnlineMode());
  42.              System.out.println("Number of players online: " + BungeeCord.getInstance().getPlayers().size());
  43.              System.out.println("Protocol Version: " + BungeeCord.getInstance().getProtocolVersion());
  44.              System.out.println("Plugin List: (" + BungeeCord.getInstance().getPluginManager().getPlugins().size() + ")");
  45.              Iterator<Plugin> pli = BungeeCord.getInstance().getPluginManager().getPlugins().iterator();
  46.              while(pli.hasNext()){
  47.                  Plugin pl = pli.next();
  48.                  System.out.println("- " + pl.getDescription().getName() + " - " + pl.getDescription().getVersion());
  49.              }
  50.              System.out.println();
  51.              System.out.println("-----------Developers Notes-------------");
  52.              System.out.println(customReason);
  53.              System.out.println();
  54.              System.out.println("-----------Stacktrace-------------");
  55.              for(int i = 0; i < throwable.getStackTrace().length; i++){
  56.                  System.out.println(throwable.getStackTrace()[i]);
  57.              }
  58.              System.out.println("-----------Suppressed Stacktrace-------------");
  59.              for(int i = 0; i < throwable.getSuppressed().length; i++){
  60.                  System.out.println(throwable.getSuppressed()[i]);
  61.              }
  62.              System.out.println("-----------Cause (Usually null)-------------");
  63.              System.out.println(throwable.getCause());
  64.              System.out.println();
  65.              System.out.println();
  66.              System.out.println("-----------LocalizedMessage (Usually null)-------------");
  67.              System.out.println(throwable.getLocalizedMessage());
  68.              System.out.println();
  69.              System.out.println("-----------Message (Usually null)------------------");
  70.              System.out.println(throwable.getMessage());
  71.              System.out.println();
  72.              System.out.println("-----------End Error---------------");
  73.              System.out.println();
  74.         }
  75.         System.out.println("[" + severity + "]" + " The plugin: " + plugin.getDescription().getName() + " has thrown an internal exception! This error has been logged in: " + "plugins/errors/" + plugin.getDescription().getName() + "/error_on_" + date + ".log"
  76.                 + " Do not wait for the version this is fixed in! Hurry and report it to " + plugin.getDescription().getAuthor() + " on the SpigotMC.org forums!");
  77.         File errorFile = new File("plugins/errors/" + plugin.getDescription().getName() + "/error_on_" + date + ".log");
  78.             try {
  79.                 File dir = new File("plugins/errors/" + plugin.getDescription().getName() + "/");
  80.                 dir.mkdirs();
  81.                 errorFile.createNewFile();
  82.             } catch (IOException e) {
  83.                 e.printStackTrace();
  84.                 System.out.println("An internal exception was caused by an Error Logging!! Not good man. Not good.");
  85.             }
  86.             try {
  87.                  PrintWriter writer = new PrintWriter(new FileWriter(errorFile,true));
  88.                  writer.println("================================================");
  89.                  writer.println("-----------General Info--------------");
  90.                  writer.println("Error caused by: + plugin.getDescription().getName());
  91.                  String[] pro = {"java.version", "java.vm.version", "java.runtime.version"};
  92.                   Properties properties = System.getProperties();
  93.                   for (int i = 0; i < pro.length; i++) {
  94.                   writer.println(pro[i] + ": " + properties.getProperty(pro[i]));
  95.                   }
  96.                  writer.println("Unix Time of Error: " + System.currentTimeMillis());
  97.                  writer.println("Formatted Time of Error: " + date);
  98.                  writer.println();
  99.                  writer.println("-----------Instance Info-------------");
  100.                  writer.println("BungeeCord Version: " + BungeeCord.getInstance().getVersion());
  101.                  writer.println("Online State: " + BungeeCord.getInstance().config.isOnlineMode());
  102.                  writer.println("Number of players online: " + BungeeCord.getInstance().getPlayers().size());
  103.                  writer.println("Protocol Version: " + BungeeCord.getInstance().getProtocolVersion());
  104.                  writer.println("Plugin List: (" + BungeeCord.getInstance().getPluginManager().getPlugins().size() + ")");
  105.                  Iterator<Plugin> pli = BungeeCord.getInstance().getPluginManager().getPlugins().iterator();
  106.                  while(pli.hasNext()){
  107.                      Plugin pl = pli.next();
  108.                      writer.println("- " + pl.getDescription().getName() + " - " + pl.getDescription().getVersion());
  109.                  }
  110.                  writer.println();
  111.                  writer.println("-----------Developers Notes-------------");
  112.                  writer.println(customReason);
  113.                  writer.println();
  114.                  writer.println("-----------Stacktrace-------------");
  115.                  for(int i = 0; i < throwable.getStackTrace().length; i++){
  116.                      writer.println(throwable.getStackTrace()[i]);
  117.                  }
  118.                  writer.println("-----------Suppressed Stacktrace-------------");
  119.                  for(int i = 0; i < throwable.getSuppressed().length; i++){
  120.                      writer.println(throwable.getSuppressed()[i]);
  121.                  }
  122.                  writer.println("-----------Cause (Usually null)-------------");
  123.                  writer.println(throwable.getCause());
  124.                  writer.println();
  125.                  writer.println();
  126.                  writer.println("-----------LocalizedMessage (Usually null)-------------");
  127.                  writer.println(throwable.getLocalizedMessage());
  128.                  writer.println();
  129.                  writer.println("-----------Message (Usually null)------------------");
  130.                  writer.println(throwable.getMessage());
  131.                  writer.println();
  132.                  writer.println("-----------End Error---------------");
  133.                  writer.println();
  134.                  writer.close();
  135.             } catch(Exception e){
  136.                 e.printStackTrace();
  137.                 System.out.println("An internal exception was caused by an Error Logging!! Not good man. Not good.");
  138.             }
  139.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement