Logging.scala 2.06 KB
Newer Older
1
/**
2
3
4
5
6
7
8
9
10
11
12
13
14
  * Biopet is built on top of GATK Queue for building bioinformatic
  * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
  * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
  * should also be able to execute Biopet tools and pipelines.
  *
  * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
  *
  * Contact us at: sasc@lumc.nl
  *
  * A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
  * license; For commercial users or users who do not want to follow the AGPL
  * license, please contact us to obtain a separate license.
  */
Peter van 't Hof's avatar
Peter van 't Hof committed
15
package nl.lumc.sasc.biopet.utils
Peter van 't Hof's avatar
Peter van 't Hof committed
16
17
18

import org.apache.log4j.Logger

Peter van 't Hof's avatar
Peter van 't Hof committed
19
20
import scala.collection.mutable.ListBuffer

Peter van 't Hof's avatar
Peter van 't Hof committed
21
/**
22
23
  * Trait to implement logger function on local class/object
  */
Peter van 't Hof's avatar
Peter van 't Hof committed
24
trait Logging {
25

Peter van 't Hof's avatar
Peter van 't Hof committed
26
  /**
27
28
29
    *
    * @return Global biopet logger
    */
pjvan_thof's avatar
pjvan_thof committed
30
  def logger: Logger = Logging.logger
31
32
}

Peter van 't Hof's avatar
Peter van 't Hof committed
33
/**
34
35
  * Logger object, has a global logger
  */
36
object Logging {
pjvan_thof's avatar
pjvan_thof committed
37
  val logger: Logger = Logger.getRootLogger
Peter van 't Hof's avatar
Peter van 't Hof committed
38

Peter van 't Hof's avatar
Peter van 't Hof committed
39
  private[biopet] val errors: ListBuffer[Exception] = ListBuffer()
Peter van 't Hof's avatar
Peter van 't Hof committed
40

Peter van 't Hof's avatar
Peter van 't Hof committed
41
42
43
44
45
46
47
48
49
50
51
  def addError(error: String, debug: Option[String] = None, cause: Option[Exception] = None): Unit = {
    val msg = error + debug.map("; " + _).getOrElse("")

    cause match {
      case Some(e) =>
        logger.error(msg, e)
        errors.append(new Exception(msg, e))
      case _ =>
        logger.error(msg)
        errors.append(new Exception(msg))
    }
Peter van 't Hof's avatar
Peter van 't Hof committed
52
53
  }

54
  def checkErrors(debug: Boolean = false): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
55
56
57
    if (errors.nonEmpty) {
      logger.error("*************************")
      logger.error("Biopet found some errors:")
58
      if (debug || logger.isDebugEnabled) {
Peter van 't Hof's avatar
Peter van 't Hof committed
59
60
        for (e <- errors) {
          logger.error(e.getMessage)
61
          logger.error(e.getStackTrace.mkString("Stack trace:\n", "\n", "\n"))
Peter van 't Hof's avatar
Peter van 't Hof committed
62
63
64
65
        }
      } else {
        errors.map(_.getMessage).sorted.distinct.foreach(logger.error(_))
      }
66
      errors.clear()
Peter van 't Hof's avatar
Peter van 't Hof committed
67
68
69
      throw new IllegalStateException("Biopet found errors")
    }
  }
70
}