This repository was archived by the owner on Sep 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
89 additions
and
57 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
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
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
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
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
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
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 |
---|---|---|
|
@@ -16,8 +16,18 @@ | |
*/ | ||
package tv.dotstart.beacon.util | ||
|
||
import org.apache.logging.log4j.Level | ||
import org.apache.logging.log4j.LogManager | ||
import org.apache.logging.log4j.Logger | ||
import org.apache.logging.log4j.core.LoggerContext | ||
import org.apache.logging.log4j.core.appender.RollingFileAppender | ||
import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy | ||
import org.apache.logging.log4j.core.appender.rolling.OnStartupTriggeringPolicy | ||
import org.apache.logging.log4j.core.config.Configurator | ||
import org.apache.logging.log4j.core.layout.PatternLayout | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import kotlin.reflect.KClass | ||
|
||
/** | ||
|
@@ -26,6 +36,15 @@ import kotlin.reflect.KClass | |
* @author [Johannes Donath](mailto:[email protected]) | ||
*/ | ||
|
||
/** | ||
* Permits retrieving and altering of the root logger level. | ||
*/ | ||
var rootLevel: Level | ||
get() = LogManager.getRootLogger().level | ||
set(value: Level) { | ||
Configurator.setRootLevel(value) | ||
} | ||
|
||
/** | ||
* Retrieves the logger for a given type. | ||
* | ||
|
@@ -35,3 +54,56 @@ import kotlin.reflect.KClass | |
*/ | ||
inline val <T : Any> KClass<T>.logger: Logger | ||
get() = LogManager.getLogger(this.java) | ||
|
||
/** | ||
* Configures the root logger to write all of its logs to a given storage location. | ||
*/ | ||
fun configureLogStorage(target: Path) { | ||
Files.createDirectories(target) | ||
|
||
val ctx = LoggerContext.getContext(false) | ||
val root = ctx.rootLogger | ||
val cfg = ctx.configuration | ||
|
||
val layout = PatternLayout.newBuilder() | ||
.withPattern("[%d{HH:mm:ss}] [%25.25t] [%level]: %msg%n") | ||
.withCharset(StandardCharsets.UTF_8) | ||
.withAlwaysWriteExceptions(true) | ||
.withConfiguration(cfg) | ||
.build() | ||
|
||
val policy = OnStartupTriggeringPolicy.createPolicy(0) | ||
|
||
val strategy = DefaultRolloverStrategy.newBuilder() | ||
.withMax("10") | ||
.withMin("1") | ||
.withFileIndex("min") | ||
.withConfig(cfg) | ||
.build() | ||
|
||
val fileAppender = (RollingFileAppender.newBuilder<RollingFileAppenderBuilder>() as RollingFileAppender.Builder<RollingFileAppenderBuilder>).apply { | ||
name = "File" | ||
configuration = cfg | ||
setLayout(layout) | ||
|
||
withFileName(target.resolve("latest.log").toAbsolutePath().toString()) | ||
withFilePattern(target.resolve("lantern.log").toAbsolutePath().toString() + ".%i") | ||
withAppend(true) | ||
withImmediateFlush(true) | ||
withPolicy(policy) | ||
withStrategy(strategy) | ||
}.build() | ||
|
||
fileAppender.start() | ||
|
||
cfg.addAppender(fileAppender) | ||
root.addAppender(fileAppender) | ||
} | ||
|
||
/** | ||
* Utility class which works around log4j's current API issues. | ||
* | ||
* See https://stackoverflow.com/questions/49601627/how-to-specify-recursive-generic-parameter-in-kotlin/50565929#50565929 | ||
* for more information on the topic. | ||
*/ | ||
private class RollingFileAppenderBuilder : RollingFileAppender.Builder<RollingFileAppenderBuilder>() |