daily pastebin goal
37%
SHARE
TWEET

Untitled

a guest Jun 14th, 2018 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.lang.StringBuilder;
  2. import hudson.model.TaskListener;
  3. import java.lang.reflect.Field;
  4.  
  5. class Logger {
  6.  
  7.     public enum Level {
  8.         WTF(55), CRITICAL(50), ERROR(40), WARNING(30), INFO(20), DEBUG(10), NOSET(0);
  9.  
  10.         private final int level;
  11.  
  12.         private Level(int logginLevel) {
  13.             this.level = logginLevel;
  14.         }
  15.  
  16.         public boolean validLevel(Level checkLevel) {
  17.             return 0 < this.level && this.level<= checkLevel.level ;
  18.         }
  19.     }
  20.  
  21.     private static TaskListener listener;
  22.     private static Level logLevel;
  23.  
  24.     public static void setListener(TaskListener listener) {
  25.         Logger.listener = listener;
  26.     }
  27.  
  28.     public static void wtf(String message) {
  29.         if (Logger.logLevel.validLevel(Level.WTF))
  30.             Logger.println("[¡¿*?!] WTF  :: %s", message);
  31.     }
  32.  
  33.     public static void critical(String message) {
  34.         if (Logger.logLevel.validLevel(Level.CRITICAL))
  35.             Logger.println("[¡!] CRITICAL :: %s ", message);
  36.     }
  37.  
  38.     public static void error(String message) {
  39.         if (Logger.logLevel.validLevel(Level.ERROR))
  40.             Logger.println(message);
  41.     }
  42.  
  43.     public static void warn(String message) {
  44.         if (Logger.logLevel.validLevel(Level.WARNING))
  45.             Logger.println("[¿?] WARNING  :: %s", message);
  46.     }
  47.  
  48.     public static void warning(String message) {
  49.         Logger.warn(message);
  50.     }
  51.  
  52.     public static void info(String message) {
  53.         if (Logger.logLevel.validLevel(Level.INFO))
  54.             Logger.println("[**] INFO  :: %s", message);
  55.     }
  56.  
  57.     public static void debug(String message) {
  58.         if (Logger.logLevel.validLevel(Level.DEBUG))
  59.             Logger.println("[+-] DEBUG  :: %s", message);
  60.     }
  61.  
  62.  
  63.     public static void debug(String message, Object... args) {
  64.            Logger.debug(String.format(message, args));
  65.     }
  66.  
  67.     public static void debug(String message, Object obj){
  68.         Logger.debug(message + "\r\n" + Logger.objectToString(obj));
  69.     }
  70.  
  71.     public static void debug(Object obj){
  72.         Logger.debug(Logger.objectToString(obj));
  73.     }
  74.  
  75.  
  76.     private static String objectToString(Object obj)
  77.     {
  78.         StringBuilder result = new StringBuilder("Printing object\n");
  79.         // result.append(obj.getClass().getCanonicalName() + "\n");
  80.  
  81.         try {
  82.             String newLine = System.getProperty("line.separator");
  83.  
  84.             result.append(obj.getClass().getName());
  85.             result.append(" Object {");
  86.             result.append(newLine);
  87.  
  88.             // determine fields declared in obj class only (no fields of superclass)
  89.             Field[] fields = obj.getClass().getDeclaredFields();
  90.  
  91.             // print field names paired with their values
  92.             for (Field field : fields) {
  93.                 result.append("  ");
  94.                 try {
  95.                     result.append(field.getName());
  96.                     result.append(": ");
  97.                     field.setAccessible(true);
  98.                     // requires access to private field:
  99.                     result.append(field.get(obj));
  100.                 } catch (IllegalAccessException ex) {
  101.                     System.out.println(ex);
  102.                 }
  103.                 result.append(newLine);
  104.             }
  105.             result.append("}");
  106.         } catch (Exception ex) {
  107.             result.append("Unable to get attributes: " + ex.getMessage());
  108.         }
  109.         return result.toString();    
  110.     }
  111.  
  112.  
  113.     public static void println(String message, Object... args) {
  114.         /**
  115.         * Here you can substitute with our stdout redirection using String.format
  116.         * And need to handle if (stdout stream is null) we defined as TaskListener, because is used as Jenkins Plugins logger
  117.         */
  118.         if (Logger.listener != null)
  119.             Logger.listener.getLogger().println(String.format(message, args));
  120.     }
  121.  
  122.     public static void enableDebug() {
  123.         Logger.setLevel(Level.DEBUG);
  124.     }
  125.  
  126.     public static void disableDebug() {
  127.         Logger.setLevel(Level.INFO);
  128.     }
  129.  
  130.     public static void setLevel(Level logLevel) {
  131.         Logger.logLevel = logLevel;
  132.     }
  133. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top