PageRenderTime 41ms CodeModel.GetById 25ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/scalate-util/src/main/scala/org/fusesource/scalate/util/Logging.scala

http://github.com/scalate/scalate
Scala | 204 lines | 147 code | 31 blank | 26 comment | 22 complexity | 38d0adb6eaa6b330462fcbceed740bbb MD5 | raw file
  1/**
  2 * Copyright (C) 2009-2011 the original author or authors.
  3 * See the notice.md file distributed with this work for additional
  4 * information regarding copyright ownership.
  5 *
  6 * Licensed under the Apache License, Version 2.0 (the "License");
  7 * you may not use this file except in compliance with the License.
  8 * You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18package org.fusesource.scalate.util
 19
 20import _root_.java.lang.{ Throwable, String }
 21import org.slf4j.{ MDC, LoggerFactory }
 22
 23import java.util.concurrent.atomic.AtomicLong
 24
 25/**
 26 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
 27 */
 28object Log {
 29  def apply(name: String): Log = new Log {
 30    override lazy val log = LoggerFactory.getLogger(name)
 31  }
 32  def apply(clazz: Class[_]): Log = apply(clazz.getName.replace("$", "#").stripSuffix("#"))
 33  def apply(clazz: Class[_], suffix: String): Log = apply(clazz.getName.replace("$", "#").stripSuffix("#") + "." + suffix)
 34
 35  val exception_id_generator = new AtomicLong(System.currentTimeMillis)
 36  def next_exception_id = exception_id_generator.incrementAndGet.toHexString
 37}
 38
 39/**
 40 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
 41 */
 42trait Log {
 43  import Log._
 44
 45  lazy val log = LoggerFactory.getLogger(getClass.getName.replace("$", "#").stripSuffix("#"))
 46
 47  private def with_throwable(e: Throwable)(func: => Unit) = {
 48    if (e != null) {
 49      val stack_ref = if (log.isDebugEnabled) {
 50        val id = next_exception_id
 51        MDC.put("stackref", id.toString)
 52        Some(id)
 53      } else {
 54        None
 55      }
 56      func
 57      stack_ref.foreach { id =>
 58        log.debug("stack trace: " + id, e)
 59        MDC.remove("stackref")
 60      }
 61    } else {
 62      func
 63    }
 64  }
 65
 66  private def format(message: String, args: Seq[Any]) = {
 67    if (args.isEmpty) {
 68      message
 69    } else {
 70      message.format(args.map(_.asInstanceOf[AnyRef]): _*)
 71    }
 72  }
 73
 74  def error(m: => String, args: Any*): Unit = {
 75    if (log.isErrorEnabled) {
 76      log.error(format(m, args.toSeq))
 77    }
 78  }
 79
 80  def error(e: Throwable, m: => String, args: Any*): Unit = {
 81    if (log.isErrorEnabled) {
 82      with_throwable(e) {
 83        log.error(format(m, args.toSeq))
 84      }
 85    }
 86  }
 87
 88  def error(e: Throwable): Unit = {
 89    if (log.isErrorEnabled) {
 90      with_throwable(e) {
 91        log.error(e.getMessage)
 92      }
 93    }
 94  }
 95
 96  def warn(m: => String, args: Any*): Unit = {
 97    if (log.isWarnEnabled) {
 98      log.warn(format(m, args.toSeq))
 99    }
100  }
101
102  def warn(e: Throwable, m: => String, args: Any*): Unit = {
103    if (log.isWarnEnabled) {
104      with_throwable(e) {
105        log.warn(format(m, args.toSeq))
106      }
107    }
108  }
109
110  def warn(e: Throwable): Unit = {
111    if (log.isWarnEnabled) {
112      with_throwable(e) {
113        log.warn(e.getMessage)
114      }
115    }
116  }
117
118  def info(m: => String, args: Any*): Unit = {
119    if (log.isInfoEnabled) {
120      log.info(format(m, args.toSeq))
121    }
122  }
123
124  def info(e: Throwable, m: => String, args: Any*): Unit = {
125    if (log.isInfoEnabled) {
126      with_throwable(e) {
127        log.info(format(m, args.toSeq))
128      }
129    }
130  }
131
132  def info(e: Throwable): Unit = {
133    with_throwable(e) {
134      if (log.isInfoEnabled) {
135        log.info(e.getMessage)
136      }
137    }
138  }
139
140  def debug(m: => String, args: Any*): Unit = {
141    if (log.isDebugEnabled) {
142      log.debug(format(m, args.toSeq))
143    }
144  }
145
146  def debug(e: Throwable, m: => String, args: Any*): Unit = {
147    if (log.isDebugEnabled) {
148      log.debug(format(m, args.toSeq), e)
149    }
150  }
151
152  def debug(e: Throwable): Unit = {
153    if (log.isDebugEnabled) {
154      log.debug(e.getMessage, e)
155    }
156  }
157
158  def trace(m: => String, args: Any*): Unit = {
159    if (log.isTraceEnabled) {
160      log.trace(format(m, args.toSeq))
161    }
162  }
163
164  def trace(e: Throwable, m: => String, args: Any*): Unit = {
165    if (log.isTraceEnabled) {
166      log.trace(format(m, args.toSeq), e)
167    }
168  }
169
170  def trace(e: Throwable): Unit = {
171    if (log.isTraceEnabled) {
172      log.trace(e.getMessage, e)
173    }
174  }
175
176}
177
178/**
179 * A Logging trait you can mix into an implementation class without affecting its public API
180 */
181trait Logging {
182
183  protected val log = Log(getClass)
184
185  protected def error(message: => String): Unit = log.error(message)
186  protected def error(message: => String, e: Throwable): Unit = log.error(e, message)
187  protected def error(e: Throwable): Unit = log.error(e)
188
189  protected def warn(message: => String): Unit = log.warn(message)
190  protected def warn(message: => String, e: Throwable): Unit = log.warn(e, message)
191  protected def warn(e: Throwable): Unit = log.warn(e)
192
193  protected def info(message: => String): Unit = log.info(message)
194  protected def info(message: => String, e: Throwable): Unit = log.info(e, message)
195  protected def info(e: Throwable): Unit = log.info(e)
196
197  protected def debug(message: => String): Unit = log.debug(message)
198  protected def debug(message: => String, e: Throwable): Unit = log.debug(e, message)
199  protected def debug(e: Throwable): Unit = log.debug(e)
200
201  protected def trace(message: => String): Unit = log.trace(message)
202  protected def trace(message: => String, e: Throwable): Unit = log.trace(e, message)
203  protected def trace(e: Throwable): Unit = log.trace(e)
204}