-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from TheProgramSrc/feat/log_filter
Added LogFilter
- Loading branch information
Showing
4 changed files
with
70 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
## v0.2.0 - Snapshot | ||
* Added Log Filter | ||
|
||
## v0.1.0 - Snapshot | ||
Hello, World! |
This file contains 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
8 changes: 8 additions & 0 deletions
8
src/main/kotlin/xyz/theprogramsrc/loggingmodule/filter/FilterResult.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package xyz.theprogramsrc.loggingmodule.filter | ||
|
||
@Suppress("unused") | ||
enum class FilterResult { | ||
DENY, | ||
NEUTRAL, | ||
NONE, | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/xyz/theprogramsrc/loggingmodule/filter/LogFilter.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package xyz.theprogramsrc.loggingmodule.filter | ||
|
||
import org.apache.logging.log4j.Level | ||
import org.apache.logging.log4j.LogManager | ||
import org.apache.logging.log4j.Marker | ||
import org.apache.logging.log4j.core.Filter | ||
import org.apache.logging.log4j.core.LogEvent | ||
import org.apache.logging.log4j.core.Logger | ||
import org.apache.logging.log4j.core.filter.AbstractFilter | ||
import org.apache.logging.log4j.message.Message | ||
|
||
/** | ||
* Representation of a LogFilter | ||
* @param result The result to use if the filter matches | ||
* @param filteredStrings The strings that a message must contain to be filtered | ||
*/ | ||
open class LogFilter(val result: FilterResult = FilterResult.NEUTRAL, val filteredStrings: Array<String>): AbstractFilter() { | ||
|
||
/** | ||
* Extra requirements for a string filter to be matched | ||
*/ | ||
open val extraRequirements = emptyArray<String>() | ||
|
||
private fun process(message: String?): Filter.Result { | ||
if(message != null){ | ||
if(filteredStrings.any { message.lowercase().contains(it.lowercase()) } && extraRequirements.any { message.lowercase().contains(it.lowercase()) }){ | ||
return Filter.Result.valueOf(result.name) | ||
} | ||
} | ||
|
||
return Filter.Result.NEUTRAL | ||
} | ||
|
||
/** | ||
* Registers this log filter to work with the root logger. | ||
*/ | ||
fun register() { | ||
val logger = LogManager.getRootLogger() as Logger | ||
logger.addFilter(this) | ||
} | ||
|
||
override fun filter(event: LogEvent?): Filter.Result = process(event?.message?.formattedMessage) | ||
|
||
override fun filter(logger: Logger?, level: Level?, marker: Marker?, msg: Message, t: Throwable?): Filter.Result = process(msg.formattedMessage) | ||
|
||
override fun filter(logger: Logger?, level: Level?, marker: Marker?, msg: Any, t: Throwable?): Filter.Result = process(msg.toString()) | ||
|
||
override fun filter(logger: Logger?, level: Level?, marker: Marker?, msg: String?, vararg params: Any?): Filter.Result = process(msg) | ||
|
||
} |