Skip to content

Commit

Permalink
remove log4j dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed Nov 2, 2024
1 parent 76afe9a commit 7a2de21
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 38 deletions.
2 changes: 0 additions & 2 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.7.1")
implementation("com.github.ajalt.clikt:clikt:4.2.2")
testImplementation("org.jetbrains.kotlin:kotlin-test")
implementation("org.apache.logging.log4j:log4j-core:2.23.1")
implementation("org.apache.logging.log4j:log4j-slf4j-impl:2.23.1")
testCompileOnly(project(":runtime"))
}

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/kotlin/org/pastalab/fray/core/RunContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class RunContext(val config: Configuration) {
var mainExiting = false
var nanoTime = System.nanoTime()
val terminatingThread = mutableSetOf<Int>()
val logger = config.loggerContext.getLogger(RunContext::class.java)
val hashCodeMapper = ReferencedContextManager<Int>({ config.randomnessProvider.nextInt() })
var forkJoinPool: ForkJoinPool? = null
private val lockManager = LockManager()
Expand Down Expand Up @@ -82,12 +81,13 @@ class RunContext(val config: Configuration) {
} else {
e.printStackTrace(PrintWriter(sw))
}
logger.error(sw.toString())
config.frayLogger.error(sw.toString())
if (config.exploreMode || config.noExitWhenBugFound) {
return
}
if (!config.isReplay) {
logger.error("Error found, the recording is saved to ${config.report}/recording_0/")
config.frayLogger.error(
"Error found, the recording is saved to ${config.report}/recording_0/")
println("Error found, you may find the error report in ${config.report}")
config.saveToReportFolder(0)
}
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/kotlin/org/pastalab/fray/core/TestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.pastalab.fray.core

import java.util.*
import kotlin.time.TimeSource
import org.apache.logging.log4j.Logger
import org.pastalab.fray.core.command.Configuration
import org.pastalab.fray.core.randomness.ControlledRandom
import org.pastalab.fray.core.scheduler.RandomScheduler
Expand All @@ -13,8 +12,6 @@ class TestRunner(val config: Configuration) {
val context = RunContext(config)
var currentDivision = 1

val logger: Logger = config.loggerContext.getLogger(TestRunner::class.java)

init {
context.bootstrap()
}
Expand Down Expand Up @@ -44,7 +41,7 @@ class TestRunner(val config: Configuration) {
val timeSource = TimeSource.Monotonic
val start = timeSource.markNow()
var i = 0
logger.info("Fray started.")
config.frayLogger.info("Fray started.")
var bugsFound = 0
if (config.dummyRun) {
// We want to do a dummy-run first to make sure all variables are initialized
Expand Down Expand Up @@ -95,10 +92,10 @@ class TestRunner(val config: Configuration) {
if (config.isReplay) {
break
}
logger.info(
config.frayLogger.info(
"Error found at iter: $i, Elapsed time: ${(timeSource.markNow() - start).inWholeMilliseconds}ms",
)
logger.info("The recording is saved to ${config.report}/recording_$i/")
config.frayLogger.info("The recording is saved to ${config.report}/recording_$i/")
if (!config.exploreMode) {
config.saveToReportFolder(i)
break
Expand All @@ -107,7 +104,7 @@ class TestRunner(val config: Configuration) {
i++
}
context.shutDown()
logger.info(
config.frayLogger.info(
"Run finished. Total iter: $i, Elapsed time: ${(timeSource.markNow() - start).inWholeMilliseconds}ms",
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import kotlinx.serialization.json.JsonNamingStrategy
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass
import org.apache.logging.log4j.core.LoggerContext
import org.apache.logging.log4j.core.config.Configurator
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory
import org.pastalab.fray.core.logger.FrayLogger
import org.pastalab.fray.core.observers.ScheduleObserver
import org.pastalab.fray.core.observers.ScheduleRecorder
import org.pastalab.fray.core.observers.ScheduleRecording
Expand Down Expand Up @@ -242,29 +240,7 @@ data class Configuration(
scheduleObservers.forEach { it.saveToReportFolder("$report/recording_$index") }
}

val loggerContext by lazy {
val builder = ConfigurationBuilderFactory.newConfigurationBuilder()
builder.setConfigurationName("Fray")
builder.setLoggerContext(LoggerContext("Fray"))
val standard = builder.newLayout("PatternLayout")
standard.addAttribute("pattern", "%d [%t] %-5level: %msg%n%throwable")
val appender =
if (isReplay) {
builder.newAppender("log", "Console").addAttribute("target", "SYSTEM_OUT").add(standard)
} else {
builder
.newAppender("log", "File")
.addAttribute("fileName", "${report}/fray.log")
.addAttribute("append", false)
}
appender.add(standard)
builder.add(appender)
val logger = builder.newLogger("org.pastalab.fray", "INFO")
logger.add(builder.newAppenderRef("log"))
logger.addAttribute("additivity", false)
builder.add(logger)
Configurator.initialize(builder.build())
}
val frayLogger = FrayLogger("$report/fray.log")

init {
if (!isReplay) {
Expand Down
25 changes: 25 additions & 0 deletions core/src/main/kotlin/org/pastalab/fray/core/logger/FrayLogger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.pastalab.fray.core.logger

import java.io.File
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class FrayLogger(location: String) {
val logFile = File(location)

@Synchronized
fun log(level: String, message: String) {
val currentDateTime =
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
val logMessage = "$currentDateTime [$level]: $message\n"
logFile.appendText(logMessage)
}

fun info(message: String) {
log("INFO", message)
}

fun error(message: String) {
log("ERROR", message)
}
}

0 comments on commit 7a2de21

Please sign in to comment.