Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
Merge branch 'release/2.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dotStart committed Aug 1, 2019
2 parents ac7fa1e + 5f64b0b commit 09aeedb
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 57 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@

<groupId>tv.dotstart.beacon</groupId>
<artifactId>parent</artifactId>
<version>2.1.5</version>
<version>2.2.0</version>
<packaging>pom</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<cling.version>2.1.2</cling.version>
<jackson.version>2.9.9</jackson.version>
<kotlin.version>1.3.31</kotlin.version>
<log4j.version>2.5</log4j.version>
<kotlin.version>1.3.41</kotlin.version>
<log4j.version>2.12.0</log4j.version>
<openjfx.version>11</openjfx.version>
<protobuf.version>3.8.0</protobuf.version>
<protobuf.version>3.9.0</protobuf.version>
</properties>

<licenses>
Expand Down Expand Up @@ -84,7 +84,7 @@
<dependency>
<groupId>tv.dotstart.beacon</groupId>
<artifactId>repository-model</artifactId>
<version>2.1.5</version>
<version>2.2.0</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion repository-compiler/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>tv.dotstart.beacon</groupId>
<version>2.1.5</version>
<version>2.2.0</version>
</parent>

<artifactId>repository-compiler</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion repository-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>tv.dotstart.beacon</groupId>
<version>2.1.5</version>
<version>2.2.0</version>
</parent>

<artifactId>repository-model</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>tv.dotstart.beacon</groupId>
<version>2.1.5</version>
<version>2.2.0</version>
</parent>

<artifactId>ui</artifactId>
Expand Down
56 changes: 8 additions & 48 deletions ui/src/main/kotlin/tv/dotstart/beacon/BeaconCli.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,8 @@ import javafx.application.Application
import javafx.application.Platform
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.LogManager
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 tv.dotstart.beacon.util.Banner
import tv.dotstart.beacon.util.Localization
import tv.dotstart.beacon.util.OperatingSystem
import tv.dotstart.beacon.util.detailedErrorDialog
import tv.dotstart.beacon.util.*
import java.net.URI
import java.nio.charset.StandardCharsets
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.time.Duration
Expand Down Expand Up @@ -116,7 +105,7 @@ object BeaconCli : CliktCommand(name = "Beacon") {
}

override fun run() {
registerFileAppender()
configureLogStorage(this.logDirectory)

val logger = LogManager.getLogger(Beacon::class.java)

Expand Down Expand Up @@ -157,19 +146,14 @@ object BeaconCli : CliktCommand(name = "Beacon") {
"System Repositories (${systemRepositories.size}): ${systemRepositories.joinToString()}")

if (this.verbose || this.debug) {
Configurator.setRootLevel(
if (this.verbose) {
Level.ALL
} else {
Level.DEBUG
}
)

if (this.verbose) {
logger.warn("Enabled VERBOSE logging - This may cause significant log output")
val level = if (this.verbose) {
Level.ALL
} else {
logger.warn("Enabled DEBUG logging")
Level.DEBUG
}

rootLevel = level
logger.info("Adjusted the application log level to $level")
}

if (this.disableCache) {
Expand All @@ -181,30 +165,6 @@ object BeaconCli : CliktCommand(name = "Beacon") {
// we do not pass any of our arguments to JavaFX since there's nothing special to handle here
Application.launch(Beacon::class.java)
}

private fun registerFileAppender() {
val ctx = LoggerContext.getContext(false)
val root = ctx.rootLogger
val cfg = ctx.configuration
val layout = PatternLayout.createLayout("[%d{HH:mm:ss}] [%25.25t] [%level]: %msg%n", null, cfg,
null, StandardCharsets.UTF_8, true, false, null, null)
Files.createDirectories(this.logDirectory)

val policy = OnStartupTriggeringPolicy.createPolicy()
val strategy = DefaultRolloverStrategy.createStrategy("10", "1", "min", null, null, true, cfg)

val fileAppender = RollingFileAppender.createAppender(
this.logDirectory.resolve("latest.log").toAbsolutePath().toString(),
this.logDirectory.resolve("beacon.log").toAbsolutePath().toString() + ".%i",
"true", "File", null, null, "true",
policy, strategy, layout, null, null, null, null, cfg
)

fileAppender.start()

cfg.addAppender(fileAppender)
root.addAppender(fileAppender)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ object PortMapper {
Model.Protocol.UDP -> PortMapping.Protocol.UDP
else -> throw IllegalArgumentException("Unsupported protocol: ${port.protocol}")
},
"Beacon Service ({$service.id})"
"Beacon Service (${service.id})"
)
mapping.leaseDurationSeconds = UnsignedIntegerFourBytes(LEASE_DURATION)
return mapping
Expand Down
72 changes: 72 additions & 0 deletions ui/src/main/kotlin/tv/dotstart/beacon/util/LoggerExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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.
*
Expand All @@ -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>()

0 comments on commit 09aeedb

Please sign in to comment.