Skip to content

Commit

Permalink
log4j2.xml: insert js7.serverId into file header
Browse files Browse the repository at this point in the history
  • Loading branch information
Zschimmer committed Oct 22, 2024
1 parent c8c0484 commit 535f44d
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scala.jdk.CollectionConverters.*
*/
object AgentClientMain extends SimpleServiceProgram[AgentClientMain.Conf]:

override protected def productName = "JS7 AgentClient"
override protected val productName = "AgentClient"

protected def program(conf: Conf): IO[ExitCode] =
program(conf, println)(using runtime)
Expand Down
2 changes: 1 addition & 1 deletion js7-agent/src/main/scala/js7/agent/main/AgentMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import js7.common.system.startup.ServiceApp
object AgentMain extends ServiceApp:
// No Logger here!

override protected def productName = "JS7 Agent"
override protected val productName = "Agent"

def run(args: List[String]): IO[ExitCode] =
runService(args, AgentConfiguration.fromCommandLine(_), useLockFile = true):
Expand Down
2 changes: 1 addition & 1 deletion js7-base/jvm/src/main/resources/js7/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ java ... -Dlog4j2.configurationFile=classpath:js7/log4j2.xml,$config/log4j2.xml
%highlight{${debugLine}}{ERROR=red bold,WARN=red,INFO=default,DEBUG=green,TRACE=bright_black}%n
</property>
<property name="fileHeader">
%d{yyyy-MM-dd HH:mm:ss.SSSXX} Begin %X{js7.name} · %X{js7.prettyVersion} · %X{js7.system}%n
%d{yyyy-MM-dd HH:mm:ss.SSSXX} Begin JS7 %X{js7.serverId} · %X{js7.prettyVersion} · %X{js7.system}%n
</property>
<property name="fileFooter">
%d{yyyy-MM-dd HH:mm:ss.SSSXX} End of logging%n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ extends ThreadContextMap, ReadOnlyThreadContextMap, CopyOnWrite:
val last = lastLog4jStringMap
val correlId = CorrelId.local()
val v = keyToValueVersion
if last != null
&& last.correlId.eq(correlId)
&& v == lastKeyToValueVersion
then
if last != null && last.correlId.eq(correlId) && v == lastKeyToValueVersion then
last
else
lastKeyToValueVersion = v
Expand Down Expand Up @@ -104,11 +101,11 @@ object Log4jThreadContextMap:
case _ => false

def initialize(name: String): Unit =
keyToValue.put("js7.name", name)
keyToValue.put("js7.serverId", name) // May be overwritten later by a more specific value
System.setProperty("log4j2.threadContextMap", myClassName)
debug(s"log4j2.threadContextMap=$myClassName")

private[log] def set(key: String, value: String) =
private[log] def set(key: String, value: String): Unit =
keyToValue.put(key, value)
keyToValueVersion += 1

Expand All @@ -130,4 +127,4 @@ object Log4jThreadContextMap:
Logger[this.type].trace(statistics)

private def debug(string: => String): Unit =
if isDebug then println(myClassName + " - " + string)
if isDebug then println(s"$myClassName - $string")
4 changes: 0 additions & 4 deletions js7-base/jvm/src/main/scala/js7/base/log/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ object Logger extends AdHocLogger:
ScalaLogger[this.type].info(StartUp.startUpLine(name))
Tests.log()

def initializeOnly(name: String): Unit =
ifNotInitialized:
Log4j.initialize(name)

/** Don't initialize but mark as initialized.
* Use this when logging has been initialized by some outer software (like JOC). */
def dontInitialize(): Unit =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package js7.base.test
import js7.base.log.Logger

trait TestLogging:
Logger.initialize("JS7 Tests")
Logger.initialize("Test")
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.jetbrains.annotations.TestOnly
object ClusterWatchMain extends ServiceApp:
// No Logger here!

override protected def productName = "JS7 ClusterWatch"
override protected val productName = "ClusterWatch"

def run(args: List[String]): IO[ExitCode] =
runService(args, ClusterWatchConf.fromCommandLine):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object ServiceMain:
: IO[ExitCode] =
IO
.defer:
printlnWithClock(s"$name ${BuildInfo.longVersion}")
printlnWithClock(s"JS7 $name ${BuildInfo.longVersion}")
StartUp.initializeMain()

JavaMainLockfileSupport
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import js7.controller.configuration.ControllerConfiguration
object ControllerMain extends ServiceApp:

// No Logger here!
override protected def productName = "JS7 Controller"
override protected val productName = "Controller"

def run(args: List[String]): IO[ExitCode] =
given IORuntime = runtime
Expand Down
2 changes: 1 addition & 1 deletion project/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<!-- append=true is required to let forked tests append. -->
<patternLayout
pattern="%highlight{%d{yyyy-MM-dd HH:mm:ss.nnnnnn} %-5level{INFO=info, DEBUG=debug, TRACE=trace} %notEmpty{%X{js7.correlId} }[%threadName] %logger - %message}{ERROR=red bold,WARN=red,INFO=default,DEBUG=blue,TRACE=bright_black}%n"
header="%d{yyyy-MM-dd HH:mm:ss.nnnnnn} Begin %X{js7.name} · %X{js7.prettyVersion} · %X{js7.system}%n"
header="%d{yyyy-MM-dd HH:mm:ss.nnnnnn} Begin JS7 %X{js7.serverId} · %X{js7.prettyVersion} · %X{js7.system}%n"
footer="%d{yyyy-MM-dd HH:mm:ss.nnnnnn} End of logging%n"
charset="UTF-8"/>
<policies>
Expand Down

0 comments on commit 535f44d

Please sign in to comment.