You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandLineLogger.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* Copyright 2004 The Apache Software Foundation.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /* $Id$ */
  16. package org.apache.fop.util;
  17. import org.apache.commons.logging.Log;
  18. import org.apache.commons.logging.LogFactory;
  19. /**
  20. * This is a commons-logging logger for command line use.
  21. */
  22. public class CommandLineLogger implements Log {
  23. /** "Trace" level logging. */
  24. public static final int LOG_LEVEL_TRACE = 1;
  25. /** "Debug" level logging. */
  26. public static final int LOG_LEVEL_DEBUG = 2;
  27. /** "Info" level logging. */
  28. public static final int LOG_LEVEL_INFO = 3;
  29. /** "Warn" level logging. */
  30. public static final int LOG_LEVEL_WARN = 4;
  31. /** "Error" level logging. */
  32. public static final int LOG_LEVEL_ERROR = 5;
  33. /** "Fatal" level logging. */
  34. public static final int LOG_LEVEL_FATAL = 6;
  35. private int logLevel;
  36. private String logName;
  37. /**
  38. * Construct the logger with a default log level taken from the LogFactory
  39. * attribute "level".
  40. * @param logName the logger name.
  41. */
  42. public CommandLineLogger(String logName) {
  43. this.logName = logName;
  44. setLogLevel((String) LogFactory.getFactory().getAttribute("level"));
  45. }
  46. /**
  47. * Set a log level for the logger.
  48. * @param level the log level
  49. */
  50. public void setLogLevel(String level) {
  51. if ("fatal".equals(level)) {
  52. logLevel = LOG_LEVEL_FATAL;
  53. } else if ("error".equals(level)) {
  54. logLevel = LOG_LEVEL_ERROR;
  55. } else if ("warn".equals(level)) {
  56. logLevel = LOG_LEVEL_WARN;
  57. } else if ("info".equals(level)) {
  58. logLevel = LOG_LEVEL_INFO;
  59. } else if ("debug".equals(level)) {
  60. logLevel = LOG_LEVEL_DEBUG;
  61. } else if ("trace".equals(level)) {
  62. logLevel = LOG_LEVEL_TRACE;
  63. } else {
  64. logLevel = LOG_LEVEL_INFO;
  65. }
  66. }
  67. /**
  68. * @see org.apache.commons.logging.Log#isTraceEnabled()
  69. */
  70. public final boolean isTraceEnabled() {
  71. return logLevel <= LOG_LEVEL_TRACE;
  72. }
  73. /**
  74. * @see org.apache.commons.logging.Log#isDebugEnabled()
  75. */
  76. public final boolean isDebugEnabled() {
  77. return logLevel <= LOG_LEVEL_DEBUG;
  78. }
  79. /**
  80. * @see org.apache.commons.logging.Log#isInfoEnabled()
  81. */
  82. public final boolean isInfoEnabled() {
  83. return logLevel <= LOG_LEVEL_INFO;
  84. }
  85. /**
  86. * @see org.apache.commons.logging.Log#isWarnEnabled()
  87. */
  88. public final boolean isWarnEnabled() {
  89. return logLevel <= LOG_LEVEL_WARN;
  90. }
  91. /**
  92. * @see org.apache.commons.logging.Log#isErrorEnabled()
  93. */
  94. public final boolean isErrorEnabled() {
  95. return logLevel <= LOG_LEVEL_ERROR;
  96. }
  97. /**
  98. * @see org.apache.commons.logging.Log#isFatalEnabled()
  99. */
  100. public final boolean isFatalEnabled() {
  101. return logLevel <= LOG_LEVEL_FATAL;
  102. }
  103. /**
  104. * @see org.apache.commons.logging.Log#trace(java.lang.Object)
  105. */
  106. public final void trace(Object message) {
  107. if (isTraceEnabled()) {
  108. log(LOG_LEVEL_TRACE, message, null);
  109. }
  110. }
  111. /**
  112. * @see org.apache.commons.logging.Log#trace(java.lang.Object, java.lang.Throwable)
  113. */
  114. public final void trace(Object message, Throwable t) {
  115. if (isTraceEnabled()) {
  116. log(LOG_LEVEL_TRACE, message, t);
  117. }
  118. }
  119. /**
  120. * @see org.apache.commons.logging.Log#debug(java.lang.Object)
  121. */
  122. public final void debug(Object message) {
  123. if (isDebugEnabled()) {
  124. log(LOG_LEVEL_DEBUG, message, null);
  125. }
  126. }
  127. /**
  128. * @see org.apache.commons.logging.Log#debug(java.lang.Object, java.lang.Throwable)
  129. */
  130. public final void debug(Object message, Throwable t) {
  131. if (isDebugEnabled()) {
  132. log(LOG_LEVEL_DEBUG, message, t);
  133. }
  134. }
  135. /**
  136. * @see org.apache.commons.logging.Log#info(java.lang.Object)
  137. */
  138. public final void info(Object message) {
  139. if (isInfoEnabled()) {
  140. log(LOG_LEVEL_INFO, message, null);
  141. }
  142. }
  143. /**
  144. * @see org.apache.commons.logging.Log#info(java.lang.Object, java.lang.Throwable)
  145. */
  146. public final void info(Object message, Throwable t) {
  147. if (isInfoEnabled()) {
  148. log(LOG_LEVEL_INFO, message, t);
  149. }
  150. }
  151. /**
  152. * @see org.apache.commons.logging.Log#warn(java.lang.Object)
  153. */
  154. public final void warn(Object message) {
  155. if (isWarnEnabled()) {
  156. log(LOG_LEVEL_WARN, message, null);
  157. }
  158. }
  159. /**
  160. * @see org.apache.commons.logging.Log#warn(java.lang.Object, java.lang.Throwable)
  161. */
  162. public final void warn(Object message, Throwable t) {
  163. if (isWarnEnabled()) {
  164. log(LOG_LEVEL_WARN, message, t);
  165. }
  166. }
  167. /**
  168. * @see org.apache.commons.logging.Log#error(java.lang.Object)
  169. */
  170. public final void error(Object message) {
  171. if (isErrorEnabled()) {
  172. log(LOG_LEVEL_ERROR, message, null);
  173. }
  174. }
  175. /**
  176. * @see org.apache.commons.logging.Log#error(java.lang.Object, java.lang.Throwable)
  177. */
  178. public final void error(Object message, Throwable t) {
  179. if (isErrorEnabled()) {
  180. log(LOG_LEVEL_ERROR, message, t);
  181. }
  182. }
  183. /**
  184. * @see org.apache.commons.logging.Log#fatal(java.lang.Object)
  185. */
  186. public final void fatal(Object message) {
  187. if (isFatalEnabled()) {
  188. log(LOG_LEVEL_FATAL, message, null);
  189. }
  190. }
  191. /**
  192. * @see org.apache.commons.logging.Log#fatal(java.lang.Object, java.lang.Throwable)
  193. */
  194. public final void fatal(Object message, Throwable t) {
  195. if (isFatalEnabled()) {
  196. log(LOG_LEVEL_FATAL, message, t);
  197. }
  198. }
  199. /**
  200. * Do the actual logging.
  201. * This method assembles the message and prints it to
  202. * and then calls <code>write()</code> to cause it to be written.</p>
  203. *
  204. * @param type One of the LOG_LEVEL_XXX constants defining the log level
  205. * @param message The message itself (typically a String)
  206. * @param t The exception whose stack trace should be logged
  207. */
  208. protected void log(int type, Object message, Throwable t) {
  209. StringBuffer buf = new StringBuffer();
  210. // Append the message
  211. buf.append(String.valueOf(message));
  212. if (t != null) {
  213. buf.append("\n");
  214. // Append a stack trace or just the stack trace message.
  215. if (!isDebugEnabled()) {
  216. buf.append(t.toString());
  217. buf.append("\n");
  218. } else {
  219. java.io.StringWriter sw = new java.io.StringWriter(1024);
  220. java.io.PrintWriter pw = new java.io.PrintWriter(sw);
  221. t.printStackTrace(pw);
  222. pw.close();
  223. buf.append(sw.toString());
  224. }
  225. }
  226. // Print to the appropriate destination
  227. if (type >= LOG_LEVEL_WARN) {
  228. System.err.println(buf);
  229. } else {
  230. System.out.println(buf);
  231. }
  232. }
  233. }