Skip to content
Permalink
Browse files

Use separate loggers per class (with individual names)

Improve logging by using one logger per class so the source of logging
can be more easily identified. Consequently, include the logger name and
thread name into the log pattern, so that the pattern now matches the
"configuration equivalent to the default" mentioned at [1].

As the "log" extension property now is only available in the context of
an object (there is no more top-level "log"), simply hard-code the
logger name for MavenLogger.

[1] https://logging.apache.org/log4j/2.x/manual/configuration.html

Signed-off-by: Sebastian Schuberth <sebastian.schuberth@bosch-si.com>
  • Loading branch information...
sschuberth authored and mnonnenmacher committed Sep 15, 2019
1 parent ffb87e3 commit 9560c1c9313bd4ec2430ce02e6e4850b3fd1fe72
@@ -45,7 +45,7 @@ private fun toPlexusLoggerLevel(level: Level) =
* Implementation of the Plexus [Logger] that forwards all logs to the [org.slf4j.Logger] [log] using the appropriate
* log levels.
*/
class MavenLogger(level: Level) : AbstractLogger(toPlexusLoggerLevel(level), log.delegate.name) {
class MavenLogger(level: Level) : AbstractLogger(toPlexusLoggerLevel(level), "MavenLogger") {
override fun getChildLogger(name: String?) = this

override fun debug(message: String, throwable: Throwable?) = log.debug(message, throwable)
@@ -2,7 +2,7 @@
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level - %msg%n"/>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
@@ -2,7 +2,7 @@
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level - %msg%n"/>
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
@@ -19,4 +19,18 @@

package com.here.ort.utils

val log = org.apache.logging.log4j.kotlin.logger("ORT")
import org.apache.logging.log4j.kotlin.KotlinLogger
import org.apache.logging.log4j.kotlin.loggerOf

import java.util.concurrent.ConcurrentHashMap

/**
* Global map of loggers for classes so only one logger needs to be instantiated per class.
*/
val loggerOfClass = ConcurrentHashMap<Any, KotlinLogger>()

/**
* An extension property for adding a log instance to any (unique) class.
*/
val <reified T : Any> T.log: KotlinLogger
inline get() = loggerOfClass.getOrPut(T::class.java) { loggerOf(T::class.java) }
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2019 Bosch Software Innovations GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/

package com.here.ort.utils

import io.kotlintest.matchers.types.shouldBeSameInstanceAs
import io.kotlintest.specs.StringSpec

class LoggerTest : StringSpec({
"Only one logger is created per class" {
val a = this.log
val b = this.log

a shouldBeSameInstanceAs b
}
})

0 comments on commit 9560c1c

Please sign in to comment.
You can’t perform that action at this time.