fun main()

in log4j-api-kotlin-sample/src/main/kotlin/org/apache/logging/log4j/kotlin/sample/LoggingAppExtensionProperty.kt [25:62]


  fun main(args: Array<String>) {
    val s1 = "foo"
    val s2 = "bar"

    ContextMap["key"] = "value"

    logger.info { "Hello, world: $s1 $s2" }

    ContextMap -= "key"

    logger.trace("Regular trace")

    logger.runInTrace {
      logger.info("Inside trace extension!")
    }

    logger.runInTrace(logger.traceEntry({ "param1" }, { "param2" })) {
      logger.info("Inside trace extension with params suppliers!")
    }

    fun getKey(): Int = logger.runInTrace {
      Random().nextInt(10)
    }

    fun getKeyError(): Int = logger.runInTrace {
      throw Exception("Oops!")
    }

    ContextMap += "foo" to "bar"
    ContextMap += "a" to "b"
    logger.info { "Key was ${getKey()}" }
    try {
      logger.info { "Key was ${getKeyError()}" }
    } catch(e: Exception) {
      ContextMap.clear()
      logger.info { "Key threw ${e.message}" }
    }
  }