Advertisement
Guest User

Untitled

a guest
Jan 10th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.77 KB | None | 0 0
  1. Index: aCis_gameserver/java/net/sf/l2j/commons/logging/CLogger.java
  2. ===================================================================
  3. --- aCis_gameserver/java/net/sf/l2j/commons/logging/CLogger.java    (nonexistent)
  4. +++ aCis_gameserver/java/net/sf/l2j/commons/logging/CLogger.java    (revision 886)
  5. @@ -0,0 +1,231 @@
  6. +package net.sf.l2j.commons.logging;
  7. +
  8. +import java.util.logging.Level;
  9. +import java.util.logging.LogRecord;
  10. +import java.util.logging.Logger;
  11. +
  12. +import net.sf.l2j.commons.lang.StringReplacer;
  13. +
  14. +/**
  15. + * Wraps the regular {@link Logger} to handle slf4j features, notably {} replacement.<br>
  16. + * <br>
  17. + * The current values should be used :
  18. + * <ul>
  19. + * <li>debug (Level.FINE): debug purposes (end replacement for Config.DEBUG).</li>
  20. + * <li>info (Level.INFO) : send regular informations to the console.</li>
  21. + * <li>warn (Level.WARNING): report failed integrity checks.</li>
  22. + * <li>error (Level.SEVERE): report an issue involving data loss / leading to unexpected server behavior.</li>
  23. + * </ul>
  24. + */
  25. +public final class CLogger
  26. +{
  27. +   private final Logger _logger;
  28. +  
  29. +   public CLogger(String name)
  30. +   {
  31. +       _logger = Logger.getLogger(name);
  32. +   }
  33. +  
  34. +   private void log0(Level level, StackTraceElement caller, Object message, Throwable exception)
  35. +   {
  36. +       if (!_logger.isLoggable(level))
  37. +           return;
  38. +      
  39. +       if (caller == null)
  40. +           caller = new Throwable().getStackTrace()[2];
  41. +      
  42. +       _logger.logp(level, caller.getClassName(), caller.getMethodName(), String.valueOf(message), exception);
  43. +   }
  44. +  
  45. +   private void log0(Level level, StackTraceElement caller, Object message, Throwable exception, Object... args)
  46. +   {
  47. +       if (!_logger.isLoggable(level))
  48. +           return;
  49. +      
  50. +       if (caller == null)
  51. +           caller = new Throwable().getStackTrace()[2];
  52. +      
  53. +       _logger.logp(level, caller.getClassName(), caller.getMethodName(), format(String.valueOf(message), args), exception);
  54. +   }
  55. +  
  56. +   public void log(LogRecord record)
  57. +   {
  58. +       _logger.log(record);
  59. +   }
  60. +  
  61. +   /**
  62. +    * Logs a message with Level.FINE.
  63. +    * @param message : The object to log.
  64. +    */
  65. +   public void debug(Object message)
  66. +   {
  67. +       log0(Level.FINE, null, message, null);
  68. +   }
  69. +  
  70. +   /**
  71. +    * Logs a message with Level.FINE.
  72. +    * @param message : The object to log.
  73. +    * @param args : The passed arguments, used to format the message.
  74. +    */
  75. +   public void debug(Object message, Object... args)
  76. +   {
  77. +       log0(Level.FINE, null, message, null, args);
  78. +   }
  79. +  
  80. +   /**
  81. +    * Logs a message with Level.FINE.
  82. +    * @param message : The object to log.
  83. +    * @param exception : Log the caught exception.
  84. +    */
  85. +   public void debug(Object message, Throwable exception)
  86. +   {
  87. +       log0(Level.FINE, null, message, exception);
  88. +   }
  89. +  
  90. +   /**
  91. +    * Logs a message with Level.FINE.
  92. +    * @param message : The object to log.
  93. +    * @param exception : Log the caught exception.
  94. +    * @param args : The passed arguments, used to format the message.
  95. +    */
  96. +   public void debug(Object message, Throwable exception, Object... args)
  97. +   {
  98. +       log0(Level.FINE, null, message, exception, args);
  99. +   }
  100. +  
  101. +   /**
  102. +    * Logs a message with Level.INFO.
  103. +    * @param message : The object to log.
  104. +    */
  105. +   public void info(Object message)
  106. +   {
  107. +       log0(Level.INFO, null, message, null);
  108. +   }
  109. +  
  110. +   /**
  111. +    * Logs a message with Level.INFO.
  112. +    * @param message : The object to log.
  113. +    * @param args : The passed arguments, used to format the message.
  114. +    */
  115. +   public void info(Object message, Object... args)
  116. +   {
  117. +       log0(Level.INFO, null, message, null, args);
  118. +   }
  119. +  
  120. +   /**
  121. +    * Logs a message with Level.INFO.
  122. +    * @param message : The object to log.
  123. +    * @param exception : Log the caught exception.
  124. +    */
  125. +   public void info(Object message, Throwable exception)
  126. +   {
  127. +       log0(Level.INFO, null, message, exception);
  128. +   }
  129. +  
  130. +   /**
  131. +    * Logs a message with Level.INFO.
  132. +    * @param message : The object to log.
  133. +    * @param exception : Log the caught exception.
  134. +    * @param args : The passed arguments, used to format the message.
  135. +    */
  136. +   public void info(Object message, Throwable exception, Object... args)
  137. +   {
  138. +       log0(Level.INFO, null, message, exception, args);
  139. +   }
  140. +  
  141. +   /**
  142. +    * Logs a message with Level.WARNING.
  143. +    * @param message : The object to log.
  144. +    */
  145. +   public void warn(Object message)
  146. +   {
  147. +       log0(Level.WARNING, null, message, null);
  148. +   }
  149. +  
  150. +   /**
  151. +    * Logs a message with Level.WARNING.
  152. +    * @param message : The object to log.
  153. +    * @param args : The passed arguments, used to format the message.
  154. +    */
  155. +   public void warn(Object message, Object... args)
  156. +   {
  157. +       log0(Level.WARNING, null, message, null, args);
  158. +   }
  159. +  
  160. +   /**
  161. +    * Logs a message with Level.WARNING.
  162. +    * @param message : The object to log.
  163. +    * @param exception : Log the caught exception.
  164. +    */
  165. +   public void warn(Object message, Throwable exception)
  166. +   {
  167. +       log0(Level.WARNING, null, message, exception);
  168. +   }
  169. +  
  170. +   /**
  171. +    * Logs a message with Level.WARNING.
  172. +    * @param message : The object to log.
  173. +    * @param exception : Log the caught exception.
  174. +    * @param args : The passed arguments, used to format the message.
  175. +    */
  176. +   public void warn(Object message, Throwable exception, Object... args)
  177. +   {
  178. +       log0(Level.WARNING, null, message, exception, args);
  179. +   }
  180. +  
  181. +   /**
  182. +    * Logs a message with Level.SEVERE.
  183. +    * @param message : The object to log.
  184. +    */
  185. +   public void error(Object message)
  186. +   {
  187. +       log0(Level.SEVERE, null, message, null);
  188. +   }
  189. +  
  190. +   /**
  191. +    * Logs a message with Level.SEVERE.
  192. +    * @param message : The object to log.
  193. +    * @param args : The passed arguments, used to format the message.
  194. +    */
  195. +   public void error(Object message, Object... args)
  196. +   {
  197. +       log0(Level.SEVERE, null, message, null, args);
  198. +   }
  199. +  
  200. +   /**
  201. +    * Logs a message with Level.SEVERE.
  202. +    * @param message : The object to log.
  203. +    * @param exception : Log the caught exception.
  204. +    */
  205. +   public void error(Object message, Throwable exception)
  206. +   {
  207. +       log0(Level.SEVERE, null, message, exception);
  208. +   }
  209. +  
  210. +   /**
  211. +    * Logs a message with Level.SEVERE.
  212. +    * @param message : The object to log.
  213. +    * @param exception : Log the caught exception.
  214. +    * @param args : The passed arguments, used to format the message.
  215. +    */
  216. +   public void error(Object message, Throwable exception, Object... args)
  217. +   {
  218. +       log0(Level.SEVERE, null, message, exception, args);
  219. +   }
  220. +  
  221. +   /**
  222. +    * Format the message, allowing to use {} as parameter. Avoid to generate String concatenation.
  223. +    * @param message : the Object (String) message to format.
  224. +    * @param args : the arguments to pass.
  225. +    * @return a formatted String.
  226. +    */
  227. +   private static final String format(String message, Object... args)
  228. +   {
  229. +       if (args == null || args.length == 0)
  230. +           return message;
  231. +      
  232. +       final StringReplacer sr = new StringReplacer(message);
  233. +       sr.replaceAll(args);
  234. +       return sr.toString();
  235. +   }
  236. +}
  237. \ No newline at end of file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement