/src/main/java/se/crafted/chrisb/ecoCreature/commons/LoggerUtil.java

https://github.com/mung3r/ecoCreature · Java · 100 lines · 65 code · 16 blank · 19 comment · 2 complexity · 0d5d3b7b4475dd19f14a236228a81423 MD5 · raw file

  1. /*
  2. * This file is part of ecoCreature.
  3. *
  4. * Copyright (c) 2011-2015, R. Ramos <http://github.com/mung3r/>
  5. * ecoCreature is licensed under the GNU Lesser General Public License.
  6. *
  7. * ecoCreature is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * ecoCreature is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package se.crafted.chrisb.ecoCreature.commons;
  21. import java.util.logging.Logger;
  22. public final class LoggerUtil
  23. {
  24. private static final String LOG_NAME = "ecoCreature";
  25. private static final LoggerUtil instance = new LoggerUtil();
  26. private final Logger logger;
  27. private String name;
  28. private boolean debug;
  29. public static LoggerUtil getInstance()
  30. {
  31. return instance;
  32. }
  33. private LoggerUtil()
  34. {
  35. logger = Logger.getLogger("Minecraft");
  36. debug = false;
  37. name = LOG_NAME;
  38. }
  39. public boolean isDebug()
  40. {
  41. return debug;
  42. }
  43. public void setDebug(boolean debug)
  44. {
  45. this.debug = debug;
  46. }
  47. public String getName()
  48. {
  49. return name;
  50. }
  51. public void setName(String name)
  52. {
  53. this.name = name;
  54. }
  55. public void info(String msg)
  56. {
  57. logger.info(format(msg));
  58. }
  59. public void warning(String msg)
  60. {
  61. logger.warning(format(msg));
  62. }
  63. public void severe(String msg)
  64. {
  65. logger.severe(format(msg));
  66. }
  67. public void debugTrue(String msg, boolean condition) {
  68. if (condition) {
  69. debug(msg);
  70. }
  71. }
  72. public void debug(String msg)
  73. {
  74. if (debug) {
  75. @SuppressWarnings("restriction")
  76. Class<?> aClass = sun.reflect.Reflection.getCallerClass(2);
  77. logger.info(format(aClass.getSimpleName() + ": " + msg));
  78. }
  79. }
  80. public String format(String msg)
  81. {
  82. return String.format("[%s] %s", name, msg);
  83. }
  84. }