/src/org/simiancage/bukkit/messagechangerlite/Log.java

https://github.com/dredhorse/MessageChangerLite · Java · 297 lines · 94 code · 38 blank · 165 comment · 12 complexity · f529e1e90d3160dffbd3e8eaa8424c73 MD5 · raw file

  1. package org.simiancage.bukkit.messagechangerlite; /**
  2. *
  3. * PluginName: ${plugin}
  4. * Class: Log
  5. * User: DonRedhorse
  6. * Date: 05.12.11
  7. * Time: 20:49
  8. *
  9. */
  10. import org.bukkit.plugin.Plugin;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13. /**
  14. * The Log Class allows you to log in an easy way to the craftbukkit console.
  15. * It supports different log levels:<p>
  16. * {@link #info(String) info} <p>
  17. * {@link #warning(String) warning} <p>
  18. * {@link #severe(String) severe} <p>
  19. * {@link #debug(String, Object) debug} <p>
  20. * {@link #error(String) error} <p>
  21. * {@link #log(java.util.logging.Level, String, Throwable) log}<p>
  22. * and checks with the {@link Config} if debug and error logging is
  23. * enabled.<p>
  24. * It also supports a plugin {@link #enableMsg()} and {@link #disableMsg()}.<p>
  25. * Initialization of the Log is being done by {@link #getInstance(String, String)} getInstance} .<p>
  26. * Based on the work of xZise.
  27. *
  28. * @author Don Redhorse
  29. * @author xZise
  30. */
  31. @SuppressWarnings({"WeakerAccess", "UnusedDeclaration"})
  32. public class Log {
  33. /**
  34. * Reference to the logger
  35. */
  36. private final Logger logger;
  37. /**
  38. * contains the plugin name
  39. */
  40. private final String pluginName;
  41. /**
  42. * contains the plugin pluginVersion
  43. */
  44. private final String pluginVersion;
  45. /**
  46. * Instance of the Log
  47. */
  48. private static Log instance = null;
  49. /**
  50. * Instance of the ConfigurationCalls
  51. */
  52. private final Config config = Config.getInstance();
  53. /**
  54. * Method to get the instance of the Log.
  55. * Log will be initialized when necessary.
  56. *
  57. * @param loggerName should be "Minecraft"
  58. * @param pluginName the pluginname as string
  59. *
  60. * @return instance of the Log
  61. */
  62. public static Log getInstance(String loggerName, String pluginName) {
  63. if (instance == null) {
  64. instance = new Log(loggerName, pluginName);
  65. }
  66. return instance;
  67. }
  68. /**
  69. * Method to get the instance of the Log.
  70. * Log will be initialized when necessary.
  71. *
  72. * @param pluginName the pluginname as string
  73. *
  74. * @return instance of the Log
  75. */
  76. public static Log getInstance(String pluginName) {
  77. if (instance == null) {
  78. instance = new Log(pluginName);
  79. }
  80. return instance;
  81. }
  82. /**
  83. * Method to get the instance of the Log.
  84. * Log will be initialized when necessary.
  85. * Use this to initialize the Log.
  86. *
  87. * @param pluginName the pluginname as Plugin
  88. *
  89. * @return instance of the Log
  90. */
  91. public static Log getInstance(Plugin pluginName) {
  92. if (instance == null) {
  93. instance = new Log(pluginName);
  94. }
  95. return instance;
  96. }
  97. /**
  98. * Method to get the instance of the Log.
  99. * Output to console via System.out.print if this method is called
  100. * when instance is NULL
  101. *
  102. * @return instance of the Log, NOTE: This can be NULL
  103. */
  104. public static Log getLogger() {
  105. if (instance == null) {
  106. System.out.print("Log is not ready yet!");
  107. }
  108. return instance;
  109. }
  110. /**
  111. * Constructor to initialize the Log via LoggerName and PluginName.
  112. * will hand over to {@link #Log(java.util.logging.Logger, String, String)}
  113. *
  114. * @param loggerName should be "Minecraft"
  115. * @param pluginName the name of the plugin
  116. */
  117. private Log(String loggerName, String pluginName) {
  118. this(Logger.getLogger(loggerName), pluginName, "");
  119. }
  120. /**
  121. * Constructor to initialize the Log via the PluginName.
  122. * will hand over to {@link #Log(String, String)}
  123. *
  124. * @param pluginName the name of the plugin
  125. */
  126. private Log(String pluginName) {
  127. this("Minecraft", pluginName);
  128. }
  129. /**
  130. * Constructor which finally initializes the Log.
  131. *
  132. * @param logger Logger object of Minecraft
  133. * @param pluginName the name of the plugin
  134. * @param version the version of the plugin
  135. */
  136. private Log(Logger logger, String pluginName, String version) {
  137. this.logger = logger;
  138. this.pluginName = pluginName;
  139. this.pluginVersion = version;
  140. }
  141. /**
  142. * Constructor to initialize the Log via a Plugin Object.
  143. * will hand over to {@link #Log(java.util.logging.Logger, String, String)}
  144. *
  145. * @param plugin the plugin object
  146. */
  147. private Log(Plugin plugin) {
  148. this(plugin.getServer().getLogger(), plugin.getDescription().getName(), plugin.getDescription().getVersion());
  149. }
  150. // Nothing to change from here on....
  151. // ***************************************************************************************************************************
  152. /**
  153. * will output with INFO level to console if debugging is enabled and also prints out the object contents.
  154. *
  155. * @param msg message to output
  156. * @param object object to output, will use .toString()
  157. */
  158. public void debug(String msg, Object object) {
  159. if (config.isDebugLogEnabled()) {
  160. this.logger.info(this.formatMessage(msg + "= [" + object.toString() + "]"));
  161. }
  162. }
  163. /**
  164. * will output with INFO level to console if debugging is enabled.
  165. *
  166. * @param msg message to output
  167. */
  168. public void debug(String msg) {
  169. if (config.isDebugLogEnabled()) {
  170. this.logger.info(msg);
  171. }
  172. }
  173. /**
  174. * formats the message by adding the [PluginName] in front.
  175. *
  176. * @param message to format, e.g. this is a test
  177. *
  178. * @return formated message, e.g. [PluginName] this is a test
  179. */
  180. private String formatMessage(String message) {
  181. return "[" + pluginName + "] " + message;
  182. }
  183. /**
  184. * will output with INFO level to console if error logging is enabled
  185. *
  186. * @param msg message to output
  187. */
  188. public void info(String msg) {
  189. if (config.isErrorLogEnabled()) {
  190. this.logger.info(this.formatMessage(msg));
  191. }
  192. }
  193. /**
  194. * will output with WARN level to console
  195. *
  196. * @param msg message to output
  197. */
  198. public void warning(String msg) {
  199. this.logger.warning(this.formatMessage(msg));
  200. }
  201. /**
  202. * will output with SEVERE level to console
  203. *
  204. * @param msg message to output
  205. */
  206. public void severe(String msg) {
  207. this.logger.severe(this.formatMessage(msg));
  208. }
  209. /**
  210. * will output with SEVERE level to console and also prints the exception
  211. *
  212. * @param msg message to output
  213. * @param exception exception to output
  214. */
  215. public void severe(String msg, Throwable exception) {
  216. this.log(Level.SEVERE, msg, exception);
  217. }
  218. /**
  219. * will output with specified level to console
  220. *
  221. * @param level LoggerLevel = INFO, WARNING, SEVERE
  222. * @param msg message to output
  223. * @param exception exception to output
  224. */
  225. public void log(Level level, String msg, Throwable exception) {
  226. this.logger.log(level, this.formatMessage(msg), exception);
  227. }
  228. /**
  229. * will output with WARNING level to console and also prints exception
  230. *
  231. * @param msg message to output
  232. * @param exception exception to output
  233. */
  234. public void warning(String msg, Throwable exception) {
  235. this.log(Level.WARNING, msg, exception);
  236. }
  237. /**
  238. * will output [PluginName] v "VersionNumber" enabled to console
  239. */
  240. public void enableMsg() {
  241. this.info("v" + this.pluginVersion + " enabled");
  242. }
  243. /**
  244. * will output [PluginName] v "VersionNumber" disabled to console
  245. */
  246. public void disableMsg() {
  247. this.info("v" + this.pluginVersion + " disabled");
  248. }
  249. /**
  250. * will output with INFO level to console if error logging is enabled
  251. *
  252. * @param msg message to output
  253. */
  254. public void error(String msg) {
  255. if (config.isErrorLogEnabled()) {
  256. this.logger.info(msg);
  257. }
  258. }
  259. }