Skip to content

Commit

Permalink
Merge pull request #2 from TheProgramSrc/feat/log_filter
Browse files Browse the repository at this point in the history
Added LogFilter
  • Loading branch information
Im-Fran authored Dec 21, 2021
2 parents 1b7253b + c4fa999 commit 0ed9fc1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
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!
15 changes: 9 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
id 'org.jetbrains.dokka' version '1.6.0'
}

def projectVersion = (System.getenv("VERSION") ?: '0.1.0-SNAPSHOT').replaceFirst("v", "").replace('/', '')
def projectVersion = (System.getenv("VERSION") ?: '0.2.0-SNAPSHOT').replaceFirst("v", "").replace('/', '')

group 'xyz.theprogramsrc'
version projectVersion
Expand All @@ -16,11 +16,15 @@ description 'Just a logger for the SimpleCore API'
repositories {
mavenLocal()
mavenCentral()

maven { url 'https://repo.theprogramsrc.xyz/repository/maven-public/' }
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
maven { url 'https://oss.sonatype.org/content/repositories/releases/' }
maven { url 'https://oss.sonatype.org/content/groups/public/' }
maven { url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
maven { url 'https://repo.codemc.org/repository/maven-public' }
maven { url 'https://repo.codemc.org/repository/nms' }
maven { url 'https://jitpack.io' }
}

dependencies {
Expand All @@ -29,6 +33,9 @@ dependencies {
compileOnly 'net.md-5:bungeecord-api:1.17-R0.1-SNAPSHOT'
compileOnly 'xyz.theprogramsrc:simplecoreapi:0.1.8-SNAPSHOT'

compileOnly 'org.apache.logging.log4j:log4j-api:2.17.0'
compileOnly 'org.apache.logging.log4j:log4j-core:2.17.0'

testImplementation 'org.junit.jupiter:junit-jupiter:5.8.2'
}

Expand Down Expand Up @@ -67,10 +74,6 @@ tasks.withType(Jar) {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

test {
useJUnitPlatform()
}

configurations {
testImplementation {
extendsFrom(compileOnly)
Expand Down
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,
}
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)

}

0 comments on commit 0ed9fc1

Please sign in to comment.