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

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

http://github.com/codahale/logula
Scala | 39 lines | 18 code | 5 blank | 16 comment | 0 complexity | a095af7a22742dd303f8d74d932b8eee MD5 | raw file
 1package com.codahale.logula
 2
 3import org.apache.log4j.{Logger, Level}
 4
 5/**
 6 * A JMX MBean which allows logger levels to be set at runtime.
 7 *
 8 * @author coda
 9 */
10trait LoggingMXBean {
11  /**
12   * Returns the logger's effective level as a string.
13   */
14  def getLoggerLevel(name: String): String
15
16  /**
17   * Sets the specified logger's level to the specific level.
18   *
19   * (If the level name is unknown, INFO is used as a default.)
20   */
21  def setLoggerLevel(name: String, level: String): String
22}
23
24/**
25 * The LoggingMXBean implementation.
26 */
27object LoggingMXBean extends LoggingMXBean {
28  def setLoggerLevel(name: String, levelName: String) = {
29    val level = Level.toLevel(levelName, Level.INFO)
30    val logger = Logger.getLogger(name)
31    logger.setLevel(level)
32    "set to " + logger.getLevel
33  }
34
35  def getLoggerLevel(name: String) = {
36    val logger = Logger.getLogger(name)
37    logger.getEffectiveLevel.toString
38  }
39}