Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text-logger-slf4j: introduce wrapper for formatted logging #757

Merged
merged 9 commits into from Jun 2, 2022
3 changes: 3 additions & 0 deletions .checkstyle/suppressions.xml
Expand Up @@ -8,6 +8,9 @@
<suppress files="src[\\/](test|jmh)[\\/]java[\\/].*" checks="(FilteringWriteTag|JavadocPackage|MissingJavadoc.*)"/>
<suppress files="api[\\/]src[\\/]main[\\/]java[\\/]net[\\/]kyori[\\/]adventure[\\/]internal[\\/].*" checks="(FilteringWriteTag|JavadocPackage|MissingJavadoc.*)"/>
<suppress files="minimessage[\\/]src[\\/]main[\\/]java[\\/]net[\\/]kyori[\\/]adventure[\\/]text[\\/]minimessage[\\/]parser[\\/].*" checks="(FilteringWriteTag|JavadocPackage|MissingJavadoc.*)"/>

<!-- no package JD on multirelease variants -->
<suppress files="src[\\/]main[\\/]java\d+[\\/].*" checks="JavadocPackage"/>

<suppress files=".*[\\/]nbt[\\/](List|Compound)BinaryTag.java" checks="MethodName"/>
</suppressions>
1 change: 1 addition & 0 deletions bom/build.gradle.kts
Expand Up @@ -18,6 +18,7 @@ dependencies {
"nbt",
"serializer-configurate3",
"serializer-configurate4",
"text-logger-slf4j",
zml2008 marked this conversation as resolved.
Show resolved Hide resolved
"text-minimessage",
"text-serializer-gson",
"text-serializer-gson-legacy-impl",
Expand Down
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Expand Up @@ -10,6 +10,8 @@ indra = "2.1.1"
jmh = "1.35"
jmhPlugin = "0.6.6"
junit = "5.8.2"
mockito = "4.5.1"
slf4j = "1.7.36"
truth = "1.1.3"

[libraries]
Expand All @@ -28,6 +30,10 @@ kotlin-testJunit5 = { module = "org.jetbrains.kotlin:kotlin-test-junit5" }
configurate-v3 = "org.spongepowered:configurate-core:3.7.3"
configurate-v4 = "org.spongepowered:configurate-core:4.1.2"

# text-logger-slf4j
slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j" }
slf4jtest = "com.github.valfirst:slf4j-test:2.6.1" # Specific versions are needed for different SLF4J versions

# text-serializer-gson
gson = "com.google.code.gson:gson:2.8.0"

Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Expand Up @@ -34,6 +34,7 @@ sequenceOf(
"nbt",
"serializer-configurate3",
"serializer-configurate4",
"text-logger-slf4j",
"text-minimessage",
"text-serializer-gson",
"text-serializer-gson-legacy-impl",
Expand Down
28 changes: 28 additions & 0 deletions text-logger-slf4j/build.gradle.kts
@@ -0,0 +1,28 @@
plugins {
id("adventure.common-conventions")
}

dependencies {
api(projects.adventureApi)
api(libs.slf4j)
testImplementation(libs.slf4jtest)
}

sourceSets.main {
multirelease {
alternateVersions(9)
}
}

applyJarMetadata("net.kyori.adventure.text.logger.slf4j")

eclipse {
// Make sure slf4j doesn't end up on the module path until we are actually a module
classpath.file.whenMerged {
(this as org.gradle.plugins.ide.eclipse.model.Classpath).entries.forEach { entry ->
if (entry is org.gradle.plugins.ide.eclipse.model.Library) {
entry.entryAttributes["module"] = false
}
}
}
}
@@ -0,0 +1,43 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2022 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.text.logger.slf4j;

// Java 8 version, see Java 9 version as well
final class CallerClassFinder {
private CallerClassFinder() {
}

static String callingClassName() {
return callingClassName(2); // this, plus the calling method
}

static String callingClassName(final int elementsToSkip) { // elementsToSkip not counting this method
final StackTraceElement[] elements = Thread.currentThread().getStackTrace(); // includes call to getStackTrace()
if (elements.length <= elementsToSkip) {
throw new IllegalArgumentException("Not enough stack elements to skip " + elementsToSkip + " elements");
} else {
return elements[elementsToSkip + 2].getClassName();
}
}
}