Skip to content

Commit ab034c0

Browse files
committed
control mapping lookup logging: logger/stdout
1 parent e0868ff commit ab034c0

File tree

6 files changed

+94
-30
lines changed

6 files changed

+94
-30
lines changed

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/LoggingOptions.kt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,26 @@
55

66
package io.openapiprocessor.core.converter
77

8+
import io.openapiprocessor.core.converter.mapping.steps.Target
9+
import io.openapiprocessor.core.converter.mapping.steps.LoggingOptions
10+
11+
812
class LoggingOptions(
913
/**
1014
* log mapping lookups
1115
*/
12-
var mapping: Boolean = false
13-
)
16+
override var mapping: Boolean = false,
17+
18+
/**
19+
* log mapping lookups with logging library (slf4j) or to stdout
20+
*
21+
* this is a workaround for gradle.
22+
* Unfortunately gradle can only globally enable/disable log levels, i.e. enabling info will enable info logging
23+
* for everything which would hide the mapping lookup logging in a lot of noise. To avoid that we just log to
24+
* stdout.
25+
*/
26+
override var mappingTarget: Target = Target.LOGGER
27+
): LoggingOptions {
28+
val logger = mappingTarget == Target.LOGGER
29+
val stdout = mappingTarget == Target.STDOUT
30+
}

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/MappingFinder.kt

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class MappingFinder(val options: ApiOptions) {
2525
try {
2626
return getResultTypeMapping(query, step)
2727
} finally {
28-
step.write()
28+
step.log()
2929
}
3030
}
3131

@@ -48,7 +48,7 @@ class MappingFinder(val options: ApiOptions) {
4848
try {
4949
return findResultStyleMapping(query, step)
5050
} finally {
51-
step.write()
51+
step.log()
5252
}
5353
}
5454

@@ -72,7 +72,7 @@ class MappingFinder(val options: ApiOptions) {
7272
try {
7373
return getSingleTypeMapping(query, step)
7474
} finally {
75-
step.write()
75+
step.log()
7676
}
7777
}
7878

@@ -96,7 +96,7 @@ class MappingFinder(val options: ApiOptions) {
9696
try {
9797
return getMultiTypeMapping(query, step)
9898
} finally {
99-
step.write()
99+
step.log()
100100
}
101101
}
102102

@@ -131,7 +131,7 @@ class MappingFinder(val options: ApiOptions) {
131131
try {
132132
return findAnyTypeMapping(query, step)
133133
} finally {
134-
step.write()
134+
step.log()
135135
}
136136
}
137137

@@ -185,7 +185,7 @@ class MappingFinder(val options: ApiOptions) {
185185
try {
186186
return findTypeMapping(query, step)
187187
} finally {
188-
step.write()
188+
step.log()
189189
}
190190
}
191191

@@ -218,7 +218,7 @@ class MappingFinder(val options: ApiOptions) {
218218
try {
219219
return findAnnotationTypeMappings(query, step)
220220
} finally {
221-
step.write()
221+
step.log()
222222
}
223223
}
224224

@@ -236,7 +236,7 @@ class MappingFinder(val options: ApiOptions) {
236236
try {
237237
return findParameterTypeMapping(query, step)
238238
} finally {
239-
step.write()
239+
step.log()
240240
}
241241
}
242242

@@ -259,7 +259,7 @@ class MappingFinder(val options: ApiOptions) {
259259
try {
260260
return findAnnotationParameterTypeMappings(query, step)
261261
} finally {
262-
step.write()
262+
step.log()
263263
}
264264
}
265265

@@ -287,7 +287,7 @@ class MappingFinder(val options: ApiOptions) {
287287
try {
288288
return findParameterNameTypeMapping(query, step)
289289
} finally {
290-
step.write()
290+
step.log()
291291
}
292292
}
293293

@@ -310,7 +310,7 @@ class MappingFinder(val options: ApiOptions) {
310310
try {
311311
return findAnnotationParameterNameTypeMapping(query, step)
312312
} finally {
313-
step.write()
313+
step.log()
314314
}
315315
}
316316

@@ -328,7 +328,7 @@ class MappingFinder(val options: ApiOptions) {
328328
try {
329329
return findAddParameterTypeMappings(query, step)
330330
} finally {
331-
step.write()
331+
step.log()
332332
}
333333
}
334334

@@ -346,7 +346,7 @@ class MappingFinder(val options: ApiOptions) {
346346
try {
347347
return findContentTypeMapping(query, step)
348348
} finally {
349-
step.write()
349+
step.log()
350350
}
351351
}
352352

@@ -369,7 +369,7 @@ class MappingFinder(val options: ApiOptions) {
369369
try {
370370
return findNullTypeMapping(query, step)
371371
} finally {
372-
step.write()
372+
step.log()
373373
}
374374
}
375375

@@ -386,7 +386,7 @@ class MappingFinder(val options: ApiOptions) {
386386
try {
387387
return findExtensionAnnotations(extension, values, step)
388388
} finally {
389-
step.write()
389+
step.log()
390390
}
391391
}
392392

@@ -401,7 +401,7 @@ class MappingFinder(val options: ApiOptions) {
401401
try {
402402
return isEndpointExcluded(query, step)
403403
} finally {
404-
step.write()
404+
step.log()
405405
}
406406
}
407407

@@ -416,10 +416,4 @@ class MappingFinder(val options: ApiOptions) {
416416
private fun rootStep(message: String, extension: String): MappingStep {
417417
return RootStepX(message, extension)
418418
}
419-
420-
private fun MappingStep.write() {
421-
if (options.loggingOptions.mapping) {
422-
log()
423-
}
424-
}
425419
}

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/OptionsConverter.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package io.openapiprocessor.core.converter
77

8+
import io.openapiprocessor.core.converter.mapping.steps.MappingStepBase
9+
import io.openapiprocessor.core.converter.mapping.steps.Target
810
import io.openapiprocessor.core.converter.options.TargetDirLayout
911
import io.openapiprocessor.core.processor.MappingConverter
1012
import io.openapiprocessor.core.processor.MappingReader
@@ -103,8 +105,12 @@ class OptionsConverter(private val checkObsoleteProcessorOptions: Boolean = fals
103105
log.warn("is 'options.package-name' set in mapping? found default: '{}'.", options.packageName)
104106
}
105107

106-
if (mapping.debug.mapping) {
107-
options.loggingOptions.mapping = true
108+
with(mapping) {
109+
options.loggingOptions.mapping = debug.mapping
110+
if (debug.mappingTarget == "stdout") {
111+
options.loggingOptions.mappingTarget = Target.STDOUT
112+
}
113+
MappingStepBase.options.set(options.loggingOptions)
108114
}
109115
}
110116
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright 2024 https://github.com/openapi-processor/openapi-processor-base
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.converter.mapping.steps
7+
8+
enum class Target {
9+
LOGGER, STDOUT
10+
}
11+
12+
interface LoggingOptions {
13+
var mapping: Boolean
14+
var mappingTarget: Target
15+
}

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/mapping/steps/MappingStepBase.kt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,38 @@ package io.openapiprocessor.core.converter.mapping.steps
77

88
import org.slf4j.LoggerFactory
99

10-
abstract class MappingStepBase: MappingStep {
10+
abstract class MappingStepBase : MappingStep {
1111
private val log = LoggerFactory.getLogger(this::class.java)
1212

1313
fun log(message: String, vararg args: Any?) {
14-
// would like to log this as debug or info, but gradle can only globally enable/disable log levels.
15-
log.warn(message, *args)
14+
if (!enabled())
15+
return
16+
17+
if (useLogger()) {
18+
log.debug(message, *args)
19+
} else {
20+
var resolved = message
21+
args.forEach {
22+
resolved = resolved.replaceFirst("{}", it.toString())
23+
}
24+
println(resolved)
25+
}
26+
}
27+
28+
private fun enabled(): Boolean {
29+
return options.get().mapping
30+
}
31+
32+
private fun useLogger(): Boolean {
33+
return options.get().mappingTarget == Target.LOGGER
34+
}
35+
36+
companion object {
37+
val options: ThreadLocal<LoggingOptions> = ThreadLocal.withInitial {
38+
object : LoggingOptions {
39+
override var mapping = false
40+
override var mappingTarget = Target.LOGGER
41+
}
42+
}
1643
}
1744
}

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/Debug.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,10 @@ data class Debug(
99
/**
1010
* enable/disable tracing of mapping lookups (optional).
1111
*/
12-
val mapping: Boolean = false
12+
val mapping: Boolean = false,
13+
14+
/**
15+
* logging target
16+
*/
17+
val mappingTarget: String = "logger"
1318
)

0 commit comments

Comments
 (0)