Skip to content

Commit

Permalink
refactor: logging in separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspots committed Oct 3, 2024
1 parent 7a8f258 commit be48126
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 176 deletions.
2 changes: 1 addition & 1 deletion orchestrator/src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package technology.idlab
import java.io.File
import kotlin.system.exitProcess
import kotlinx.coroutines.runBlocking
import technology.idlab.log.Log
import technology.idlab.orchestrator.impl.SimpleOrchestrator
import technology.idlab.parser.impl.JenaParser
import technology.idlab.resolver.impl.GenericResolver
import technology.idlab.util.Log
import technology.idlab.util.ManagedProcess

/**
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/src/main/kotlin/broker/impl/SimpleBroker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package technology.idlab.broker.impl
import technology.idlab.broker.Broker
import technology.idlab.broker.BrokerClient
import technology.idlab.broker.BrokerException
import technology.idlab.util.Log
import technology.idlab.log.Log

typealias Clients<T> = Pair<Int, MutableSet<BrokerClient<T>>>

Expand Down
75 changes: 75 additions & 0 deletions orchestrator/src/main/kotlin/log/Log.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package technology.idlab.log

import technology.idlab.log.pretty.PrettyLog

abstract class Log(
// The current log level. Messages with a level lower than this will not be printed.
private val level: LogLevel
) {
/**
* Print a message to the output using a given level.
*
* @param message The message to print.
* @param level The level of the message.
*/
abstract fun log(message: String, level: LogLevel)

/**
* Print a message if and only if the debug flag is set. Note that the message will not be
* evaluated lazily.
*
* @param message The message to print.
*/
fun debug(message: String) {
log(message, LogLevel.DEBUG)
}

/**
* Print a message if and only if the debug flag is set. Note that the message will be evaluated
* lazily only if the debug flag is set.
*
* @param message A function determining the message to print.
*/
fun debug(message: () -> String) {
if (level == LogLevel.DEBUG) {
val computedMessage = message()
log(computedMessage, LogLevel.DEBUG)
}
}

/**
* Print a general info message. Will get outputted in all modes.
*
* @param message The message to print.
*/
fun info(message: String) {
log(message, LogLevel.INFO)
}

/**
* Print a command message, which will be colored yellow in the console. The location, i.e. the
* file and line number, will be determined by the caller.
*
* @param message The message to print.
*/
fun command(message: String) {
log(message, LogLevel.CMD)
}

/**
* Print a severe message, which will be colored red in the console.
*
* @param message The message to print.
*/
fun severe(message: String) {
log(message, LogLevel.SEVERE)
}

companion object {
/**
* A globally available instance of the logger. Note that at its creation, the logger will
* output the header to the console, which is why we only allow a single instance.
*/
val shared = PrettyLog(LogLevel.INFO)
}
}
8 changes: 8 additions & 0 deletions orchestrator/src/main/kotlin/log/LogLevel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package technology.idlab.log

enum class LogLevel {
CMD,
DEBUG,
INFO,
SEVERE,
}
76 changes: 76 additions & 0 deletions orchestrator/src/main/kotlin/log/pretty/PrettyLog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package technology.idlab.log.pretty

import java.time.format.DateTimeFormatter
import java.util.*
import technology.idlab.log.Log
import technology.idlab.log.LogLevel

private const val TIME_PADDING = 15
private const val LEVEL_PADDING = 10
private const val FILE_PADDING = 39
private const val MESSAGE_PADDING = 87

/**
* A simple logging class that prints messages to the console. The class is a singleton, and all
* messages are outputted to the console. The class has three levels of logging: DEBUG, INFO, and
* FATAL. DEBUG is for debugging information, INFO is for general information, and FATAL is for
* errors that will cause the program to exit. At the moment of writing, all levels are outputted to
* the console regardless of the debug flag.
*/
class PrettyLog(level: LogLevel) : Log(level) {
init {
val builder = StringBuilder()
builder.append("\u001B[1m")
builder.append("TIME".padEnd(TIME_PADDING, ' '))
builder.append("LEVEL".padEnd(LEVEL_PADDING, ' '))
builder.append("FILE".padEnd(FILE_PADDING, ' '))
builder.append("MESSAGE".padEnd(MESSAGE_PADDING, ' '))
builder.append("\n")
builder.append("\u001B[0m")
builder.append("----".padEnd(TIME_PADDING, ' '))
builder.append("-----".padEnd(LEVEL_PADDING, ' '))
builder.append("----".padEnd(FILE_PADDING, ' '))
builder.append("-------\n")

synchronized(System.out) { print(builder) }
}

override fun log(message: String, level: LogLevel) {
// Get the current time.
val instant = Date().toInstant()
val tz = instant.atZone(TimeZone.getDefault().toZoneId())
val iso = DateTimeFormatter.ISO_LOCAL_TIME

val time = tz.format(iso)
val levelCode = level.name

val stack = Throwable().stackTrace[4]
val file = "${stack.fileName ?: "Unknown"}:${stack.lineNumber}"

// Build the message.
val builder = StringBuilder()

// If the message is of level debug, set color to gray.
if (level == LogLevel.DEBUG) {
builder.append("\u001B[37m")
}

// If the message is of level command, set color to muted yellow.
if (level == LogLevel.CMD) {
builder.append("\u001B[38;5;180m")
}

// The actual message.
builder.append(time.padEnd(TIME_PADDING, ' '))
builder.append(levelCode.padEnd(LEVEL_PADDING, ' '))
builder.append(file.padEnd(FILE_PADDING, ' '))
builder.append(message)
builder.append("\n")

// Reset coloring.
builder.append("\u001B[0m")

// Print to the console, thread safe.
synchronized(System.out) { print(builder) }
}
}
2 changes: 1 addition & 1 deletion orchestrator/src/main/kotlin/parser/impl/JenaParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import technology.idlab.intermediate.parameter.LiteralParameter
import technology.idlab.intermediate.parameter.NestedParameter
import technology.idlab.intermediate.parameter.Parameter
import technology.idlab.intermediate.runner.RunnerType
import technology.idlab.log.Log
import technology.idlab.parser.Parser
import technology.idlab.parser.ParserException
import technology.idlab.util.Log

class JenaParser(
/** A file pointer to the pipeline configuration entrypoint. */
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/src/main/kotlin/resolver/impl/GitResolver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package technology.idlab.resolver.impl

import java.io.File
import technology.idlab.intermediate.IRDependency
import technology.idlab.log.Log
import technology.idlab.resolver.Resolver
import technology.idlab.util.Log

/** Resolve a Git repository by cloning it locally and reading its configuration file. */
class GitResolver : Resolver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import runner.impl.grpc.Config
import technology.idlab.ConnectionException
import technology.idlab.UnrecognizedRequestException
import technology.idlab.intermediate.IRStage
import technology.idlab.log.Log
import technology.idlab.runner.Runner
import technology.idlab.util.Log
import technology.idlab.util.retries

/**
Expand Down
2 changes: 1 addition & 1 deletion orchestrator/src/main/kotlin/runner/impl/jvm/JVMRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import technology.idlab.intermediate.IRStage
import technology.idlab.intermediate.argument.LiteralArgument
import technology.idlab.intermediate.argument.NestedArgument
import technology.idlab.intermediate.parameter.LiteralParameterType
import technology.idlab.log.Log
import technology.idlab.runner.Runner
import technology.idlab.util.Log

private const val JVM_RUNNER_URI = "https://www.rdf-connect/#JVMRunner"

Expand Down
2 changes: 1 addition & 1 deletion orchestrator/src/main/kotlin/runner/impl/jvm/Processor.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package technology.idlab.runner.impl.jvm

import technology.idlab.util.Log
import technology.idlab.log.Log

abstract class Processor(
/**
Expand Down
167 changes: 0 additions & 167 deletions orchestrator/src/main/kotlin/util/Log.kt

This file was deleted.

Loading

0 comments on commit be48126

Please sign in to comment.