Created
August 3, 2015 13:15
-
-
Save kushti/4b1f10030882413ed5a4 to your computer and use it in GitHub Desktop.
Threshold + Logger filter for Logback in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package smartcontract.utils | |
import ch.qos.logback.classic.Level | |
import ch.qos.logback.classic.spi.ILoggingEvent | |
import ch.qos.logback.core.filter.Filter | |
import ch.qos.logback.core.spi.FilterReply | |
/** | |
* Code rewritten from Java source | |
* found on http://stackoverflow.com/questions/10734025/logback-two-appenders-multiple-loggers-different-levels | |
*/ | |
class ThresholdLoggerFilter extends Filter[ILoggingEvent] { | |
private var level: Level = _ | |
private var logger: String = _ | |
override def decide(event: ILoggingEvent): FilterReply = { | |
if (!isStarted) { | |
FilterReply.NEUTRAL | |
} else if (!event.getLoggerName.startsWith(logger)) { | |
FilterReply.DENY | |
} else if (event.getLevel.isGreaterOrEqual(level)) { | |
FilterReply.NEUTRAL | |
} else { | |
FilterReply.DENY | |
} | |
} | |
def setLevel(level: Level) { | |
this.level = level | |
} | |
def setLogger(logger: String) { | |
this.logger = logger | |
} | |
override def start() { | |
if (this.level != null && this.logger != null) { | |
super.start() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I understand this implementation correctly, then this filter only allows
Loggers
that begin withlogger
as all other loggers result inDENY
. This would make using this filter for multiple loggers impossible:Here both filters mutually exclude each other.