PageRenderTime 61ms CodeModel.GetById 53ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/scala/com/codahale/logula/Log.scala

http://github.com/codahale/logula
Scala | 174 lines | 69 code | 30 blank | 75 comment | 4 complexity | f48ff7c16e648c47663858db4a750a60 MD5 | raw file
  1package com.codahale.logula
  2
  3import org.apache.log4j.{Level, Logger}
  4import reflect.NameTransformer
  5
  6/**
  7 * Log's companion object.
  8 */
  9object Log {
 10  /**
 11   * Returns a log for a given class.
 12   */
 13  def forClass[A](implicit mf: Manifest[A]) = forName(mf.erasure.getCanonicalName)
 14
 15  /**
 16   * Returns a log for a given class.
 17   */
 18  def forClass(klass: Class[_]) = forName(klass.getCanonicalName)
 19
 20  /**
 21   * Returns a log with the given name.
 22   */
 23  def forName(name: String) = new Log(Logger.getLogger(clean(name)))
 24
 25  private def clean(s: String) = NameTransformer.decode(if (s.endsWith("$")) {
 26    s.substring(0, s.length - 1)
 27  } else {
 28    s
 29  }.replaceAll("\\$", "."))
 30
 31  protected val CallerFQCN = classOf[Log].getCanonicalName
 32}
 33
 34/**
 35 * <b>In general, use the Logging trait rather than using Log directly.</b>
 36 *
 37 * A wrapper for org.apache.log4j which allows for a smoother interaction with
 38 * Scala classes. All messages are treated as format strings:
 39 * <pre>
 40 *    log.info("We have this many threads: %d", thread.size)
 41 * </pre>
 42 * Each log level has two methods: one for logging regular messages, the other
 43 * for logging messages with thrown exceptions.
 44 *
 45 * The log levels here are those of org.apache.log4j.
 46 *
 47 * @author coda
 48 */
 49class Log(private val logger: Logger) {
 50  import Log._
 51  
 52  /**
 53   * Logs a message with optional parameters at level TRACE.
 54   */
 55  def trace(message: String, params: Any*) {
 56    log(Level.TRACE, None, message, params)
 57  }
 58
 59  /**
 60   * Logs a thrown exception and a message with optional parameters at level
 61   * TRACE.
 62   */
 63  def trace(thrown: Throwable, message: String, params: Any*) {
 64    log(Level.TRACE, Some(thrown), message, params)
 65  }
 66
 67  /**
 68   * Logs a message with optional parameters at level DEBUG.
 69   */
 70  def debug(message: String, params: Any*) {
 71    log(Level.DEBUG, None, message, params)
 72  }
 73
 74  /**
 75   * Logs a thrown exception and a message with optional parameters at level
 76   * DEBUG.
 77   */
 78  def debug(thrown: Throwable, message: String, params: Any*) {
 79    log(Level.DEBUG, Some(thrown), message, params)
 80  }
 81
 82  /**
 83   * Logs a message with optional parameters at level INFO.
 84   */
 85  def info(message: String, params: Any*) {
 86    log(Level.INFO, None, message, params)
 87  }
 88
 89  /**
 90   * Logs a thrown exception and a message with optional parameters at level
 91   * INFO.
 92   */
 93  def info(thrown: Throwable, message: String, params: Any*) {
 94    log(Level.INFO, Some(thrown), message, params)
 95  }
 96
 97  /**
 98   * Logs a message with optional parameters at level WARN.
 99   */
100  def warn(message: String, params: Any*) {
101    log(Level.WARN, None, message, params)
102  }
103
104  /**
105   * Logs a thrown exception and a message with optional parameters at level
106   * WARN.
107   */
108  def warn(thrown: Throwable, message: String, params: Any*) {
109    log(Level.WARN, Some(thrown), message, params)
110  }
111
112  /**
113   * Logs a message with optional parameters at level ERROR.
114   */
115  def error(message: String, params: Any*) {
116    log(Level.ERROR, None, message, params)
117  }
118
119  /**
120   * Logs a thrown exception and a message with optional parameters at level
121   * ERROR.
122   */
123  def error(thrown: Throwable, message: String, params: Any*) {
124    log(Level.ERROR, Some(thrown), message, params)
125  }
126
127  /**
128   * Logs a message with optional parameters at level FATAL.
129   */
130
131  def fatal(message: String, params: Any*) {
132    log(Level.FATAL, None, message, params)
133  }
134
135  /**
136   * Logs a thrown exception and a message with optional parameters at level
137   * FATAL.
138   */
139
140  def fatal(thrown: Throwable, message: String, params: Any*) {
141    log(Level.FATAL, Some(thrown), message, params)
142  }
143
144  /**
145   * Returns the log's current level.
146   */
147  def level = logger.getLevel
148
149  /**
150   * Sets the log's current level.
151   */
152  def level_=(level: Level) {
153    logger.setLevel(level)
154  }
155
156  def isTraceEnabled = logger.isEnabledFor(Level.TRACE)
157
158  def isDebugEnabled = logger.isEnabledFor(Level.DEBUG)
159
160  def isInfoEnabled = logger.isEnabledFor(Level.INFO)
161
162  def isWarnEnabled = logger.isEnabledFor(Level.WARN)
163
164  def isErrorEnabled = logger.isEnabledFor(Level.ERROR)
165
166  def isFatalEnabled = logger.isEnabledFor(Level.FATAL)
167
168  private def log(level: Level, thrown: Option[Throwable], message: String, values: Seq[Any]) {
169    if (logger.isEnabledFor(level)) {
170      val statement = if (values.isEmpty) message else message.format(values:_*)
171      logger.log(CallerFQCN, level, statement, thrown.orNull)
172    }
173  }
174}