diff options
author | Izabela Zawadzka <izabela.zawadzka@nokia.com> | 2018-12-19 07:25:33 +0100 |
---|---|---|
committer | Izabela Zawadzka <izabela.zawadzka@nokia.com> | 2018-12-21 10:55:03 +0100 |
commit | 0bf244ef85d3ccddd33b7b8a6c939869d17e06b0 (patch) | |
tree | 339fb2da8124fbe6fcd3a1f72590eab7e6f1b226 /sources/hv-collector-utils/src/main | |
parent | 4128aa2c9368ed20fab92e8c0df83f14d6233b86 (diff) |
Enable setting log level from command line
Change-Id: I8397e0134d254cd5b6be79ed2b847ce265fc775c
Signed-off-by: Izabela Zawadzka <izabela.zawadzka@nokia.com>
Issue-ID: DCAEGEN2-1045
Diffstat (limited to 'sources/hv-collector-utils/src/main')
3 files changed, 132 insertions, 58 deletions
diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt index 9439bff5..e869901d 100644 --- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt +++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/commandline/CommandLineOption.kt @@ -23,118 +23,144 @@ import org.apache.commons.cli.Option enum class CommandLineOption(val option: Option, val required: Boolean = false) { - HEALTH_CHECK_API_PORT(Option.builder("H") + HEALTH_CHECK_API_PORT( + Option.builder("H") .longOpt("health-check-api-port") .hasArg() .desc("Health check rest api listen port") .build() ), - LISTEN_PORT(Option.builder("p") + LISTEN_PORT( + Option.builder("p") .longOpt("listen-port") .hasArg() .desc("Listen port") .build(), - required = true + required = true ), - CONSUL_CONFIG_URL(Option.builder("c") + CONSUL_CONFIG_URL( + Option.builder("c") .longOpt("config-url") .hasArg() .desc("URL of ves configuration on consul") .build(), - required = true + required = true ), - CONSUL_FIRST_REQUEST_DELAY(Option.builder("d") + CONSUL_FIRST_REQUEST_DELAY( + Option.builder("d") .longOpt("first-request-delay") .hasArg() .desc("Delay of first request to consul in seconds") .build() ), - CONSUL_REQUEST_INTERVAL(Option.builder("I") + CONSUL_REQUEST_INTERVAL( + Option.builder("I") .longOpt("request-interval") .hasArg() .desc("Interval of consul configuration requests in seconds") .build() ), - VES_HV_PORT(Option.builder("v") + VES_HV_PORT( + Option.builder("v") .longOpt("ves-port") .hasArg() .desc("VesHvCollector port") .build(), - required = true + required = true ), - VES_HV_HOST(Option.builder("h") + VES_HV_HOST( + Option.builder("h") .longOpt("ves-host") .hasArg() .desc("VesHvCollector host") .build(), - required = true + required = true ), - KAFKA_SERVERS(Option.builder("s") + KAFKA_SERVERS( + Option.builder("s") .longOpt("kafka-bootstrap-servers") .hasArg() .desc("Comma-separated Kafka bootstrap servers in <host>:<port> format") .build(), - required = true + required = true ), - KAFKA_TOPICS(Option.builder("f") + KAFKA_TOPICS( + Option.builder("f") .longOpt("kafka-topics") .hasArg() .desc("Comma-separated Kafka topics") .build(), - required = true + required = true ), - SSL_DISABLE(Option.builder("l") + SSL_DISABLE( + Option.builder("l") .longOpt("ssl-disable") .desc("Disable SSL encryption") .build() ), - KEY_STORE_FILE(Option.builder("k") + KEY_STORE_FILE( + Option.builder("k") .longOpt("key-store") .hasArg() .desc("Key store in PKCS12 format") .build() ), - KEY_STORE_PASSWORD(Option.builder("kp") + KEY_STORE_PASSWORD( + Option.builder("kp") .longOpt("key-store-password") .hasArg() .desc("Key store password") .build() ), - TRUST_STORE_FILE(Option.builder("t") + TRUST_STORE_FILE( + Option.builder("t") .longOpt("trust-store") .hasArg() .desc("File with trusted certificate bundle in PKCS12 format") .build() ), - TRUST_STORE_PASSWORD(Option.builder("tp") + TRUST_STORE_PASSWORD( + Option.builder("tp") .longOpt("trust-store-password") .hasArg() .desc("Trust store password") .build() ), - IDLE_TIMEOUT_SEC(Option.builder("i") + IDLE_TIMEOUT_SEC( + Option.builder("i") .longOpt("idle-timeout-sec") .hasArg() - .desc("""Idle timeout for remote hosts. After given time without any data exchange the - |connection might be closed.""".trimMargin()) + .desc( + """Idle timeout for remote hosts. After given time without any data exchange the + |connection might be closed.""".trimMargin() + ) .build() ), - MAXIMUM_PAYLOAD_SIZE_BYTES(Option.builder("m") + MAXIMUM_PAYLOAD_SIZE_BYTES( + Option.builder("m") .longOpt("max-payload-size") .hasArg() .desc("Maximum supported payload size in bytes") .build() ), - DUMMY_MODE(Option.builder("u") + LOG_LEVEL( + Option.builder("ll") + .longOpt("log-level") + .hasArg() + .desc("Log level") + .build() + ), + DUMMY_MODE( + Option.builder("u") .longOpt("dummy") .desc("If present will start in dummy mode (dummy external services)") .build() ); fun environmentVariableName(prefix: String = DEFAULT_ENV_PREFIX): String = - option.longOpt.toUpperCase().replace('-', '_').let { mainPart -> - "${prefix}_${mainPart}" - } + option.longOpt.toUpperCase().replace('-', '_').let { mainPart -> + "${prefix}_${mainPart}" + } companion object { private const val DEFAULT_ENV_PREFIX = "VESHV" diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LogLevel.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LogLevel.kt new file mode 100644 index 00000000..ec782bf7 --- /dev/null +++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/LogLevel.kt @@ -0,0 +1,41 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018 NOKIA + * ================================================================================ + * 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. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.utils.logging + +import arrow.core.Try +import ch.qos.logback.classic.Level + +enum class LogLevel(private val logbackLevel: Level) { + ERROR(Level.ERROR), + WARN(Level.WARN), + INFO(Level.INFO), + DEBUG(Level.DEBUG), + TRACE(Level.TRACE); + + operator fun invoke() = logbackLevel + + companion object { + fun optionFromString(level: String) = Try { + valueOf(level.toUpperCase()) + }.toOption() + } + + +} diff --git a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt index 938ba793..82ce50a9 100644 --- a/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt +++ b/sources/hv-collector-utils/src/main/kotlin/org/onap/dcae/collectors/veshv/utils/logging/Logger.kt @@ -19,6 +19,7 @@ */ package org.onap.dcae.collectors.veshv.utils.logging +import ch.qos.logback.classic.LoggerContext import kotlin.reflect.KClass import org.slf4j.LoggerFactory import org.slf4j.MDC @@ -41,86 +42,92 @@ class Logger(logger: org.slf4j.Logger) { fun withError(block: AtLevelLogger.() -> Unit) = errorLogger.block() fun withError(mdc: MappedDiagnosticContext, block: AtLevelLogger.() -> Unit) = - errorLogger.withMdc(mdc, block) + errorLogger.withMdc(mdc, block) fun error(message: () -> String) = errorLogger.run { log(message()) } fun error(mdc: MappedDiagnosticContext, message: () -> String) = - errorLogger.withMdc(mdc) { log(message()) } + errorLogger.withMdc(mdc) { log(message()) } fun error(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String) = - errorLogger.withMdc(mdc) { log(marker, message()) } + errorLogger.withMdc(mdc) { log(marker, message()) } // WARN fun withWarn(block: AtLevelLogger.() -> Unit) = warnLogger.block() fun withWarn(mdc: MappedDiagnosticContext, block: AtLevelLogger.() -> Unit) = - warnLogger.withMdc(mdc, block) + warnLogger.withMdc(mdc, block) fun warn(message: () -> String) = warnLogger.run { log(message()) } fun warn(mdc: MappedDiagnosticContext, message: () -> String) = - warnLogger.withMdc(mdc) { log(message()) } + warnLogger.withMdc(mdc) { log(message()) } fun warn(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String) = - warnLogger.withMdc(mdc) { log(marker, message()) } + warnLogger.withMdc(mdc) { log(marker, message()) } // INFO fun withInfo(block: AtLevelLogger.() -> Unit) = infoLogger.block() fun withInfo(mdc: MappedDiagnosticContext, block: AtLevelLogger.() -> Unit) = - infoLogger.withMdc(mdc, block) + infoLogger.withMdc(mdc, block) fun info(message: () -> String) = infoLogger.run { log(message()) } fun info(mdc: MappedDiagnosticContext, message: () -> String) = - infoLogger.withMdc(mdc) { log(message()) } + infoLogger.withMdc(mdc) { log(message()) } fun info(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String) = - infoLogger.withMdc(mdc) { log(marker, message()) } + infoLogger.withMdc(mdc) { log(marker, message()) } // DEBUG fun withDebug(block: AtLevelLogger.() -> Unit) = debugLogger.block() fun withDebug(mdc: MappedDiagnosticContext, block: AtLevelLogger.() -> Unit) = - debugLogger.withMdc(mdc, block) + debugLogger.withMdc(mdc, block) fun debug(message: () -> String) = debugLogger.run { log(message()) } fun debug(mdc: MappedDiagnosticContext, message: () -> String) = - debugLogger.withMdc(mdc) { log(message()) } + debugLogger.withMdc(mdc) { log(message()) } fun debug(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String) = - debugLogger.withMdc(mdc) { log(marker, message()) } + debugLogger.withMdc(mdc) { log(marker, message()) } // TRACE fun withTrace(block: AtLevelLogger.() -> Unit) = traceLogger.block() fun withTrace(mdc: MappedDiagnosticContext, block: AtLevelLogger.() -> Unit) = - traceLogger.withMdc(mdc, block) + traceLogger.withMdc(mdc, block) fun trace(message: () -> String) = traceLogger.run { log(message()) } fun trace(mdc: MappedDiagnosticContext, message: () -> String) = - traceLogger.withMdc(mdc) { log(message()) } + traceLogger.withMdc(mdc) { log(message()) } fun trace(mdc: MappedDiagnosticContext, marker: Marker, message: () -> String) = - traceLogger.withMdc(mdc) { log(marker, message()) } + traceLogger.withMdc(mdc) { log(marker, message()) } + companion object { + fun setLogLevel(packageName: String, level: LogLevel) { + val loggerContext = LoggerFactory.getILoggerFactory() as LoggerContext + loggerContext.getLogger(packageName).level = level() + } + } } abstract class AtLevelLogger { @@ -183,9 +190,9 @@ class ErrorLevelLogger(private val logger: org.slf4j.Logger) : AtLevelLogger() { } override fun log(marker: Marker, message: String) = - withAdditionalMdc(marker.mdc) { - logger.error(marker.slf4jMarker, message) - } + withAdditionalMdc(marker.mdc) { + logger.error(marker.slf4jMarker, message) + } } @Suppress("SuboptimalLoggerUsage") @@ -199,9 +206,9 @@ class WarnLevelLogger(private val logger: org.slf4j.Logger) : AtLevelLogger() { } override fun log(marker: Marker, message: String) = - withAdditionalMdc(marker.mdc) { - logger.warn(marker.slf4jMarker, message) - } + withAdditionalMdc(marker.mdc) { + logger.warn(marker.slf4jMarker, message) + } } @Suppress("SuboptimalLoggerUsage") @@ -215,9 +222,9 @@ class InfoLevelLogger(private val logger: org.slf4j.Logger) : AtLevelLogger() { } override fun log(marker: Marker, message: String) = - withAdditionalMdc(marker.mdc) { - logger.info(marker.slf4jMarker, message) - } + withAdditionalMdc(marker.mdc) { + logger.info(marker.slf4jMarker, message) + } } @Suppress("SuboptimalLoggerUsage") @@ -231,9 +238,9 @@ class DebugLevelLogger(private val logger: org.slf4j.Logger) : AtLevelLogger() { } override fun log(marker: Marker, message: String) = - withAdditionalMdc(marker.mdc) { - logger.debug(marker.slf4jMarker, message) - } + withAdditionalMdc(marker.mdc) { + logger.debug(marker.slf4jMarker, message) + } } @Suppress("SuboptimalLoggerUsage") @@ -247,7 +254,7 @@ class TraceLevelLogger(private val logger: org.slf4j.Logger) : AtLevelLogger() { } override fun log(marker: Marker, message: String) = - withAdditionalMdc(marker.mdc) { - logger.trace(marker.slf4jMarker, message) - } + withAdditionalMdc(marker.mdc) { + logger.trace(marker.slf4jMarker, message) + } } |