Giter Site home page Giter Site logo

apache / logging-log4j-kotlin Goto Github PK

View Code? Open in Web Editor NEW
45.0 19.0 11.0 1.33 MB

A Kotlin-friendly interface to log against the Log4j API

Home Page: https://logging.apache.org/log4j/kotlin

License: Apache License 2.0

Kotlin 82.08% FreeMarker 17.01% Java 0.91%
logging apache api concurrency coroutines jvm kotlin library log4j log4j2 logger

logging-log4j-kotlin's Issues

Coroutine context is not cleared properly

The coroutine context is not cleared properly. This test fails:

  @Test
  fun `Context is restored after a context block is complete`() = runBlocking {
    assertTrue(ContextMap.empty)
    assertTrue(ContextStack.empty)
    withContext(CoroutineThreadContext(ThreadContextData(mapOf("myKey" to "myValue"), listOf("test")))) {
      assertEquals("myValue", ContextMap["myKey"])
      assertEquals("test", ContextStack.peek())
    }
    assertTrue(ContextMap.empty)
    assertTrue(ContextStack.empty)
  }

Improve logging context integration

Using log4j2 context seems a bit limited for non-thread use cases. The assumption that context will always be set at the Thread or coroutine level is IMO false, and sometimes we just want to set context for a particular block of code, or for a particular logger.

We should have a way to do something like this outside of a coroutine / suspending context:

withContextMap(...) {
  // log something
}

Possible implementations (only map shown here, but similar for stack)?

fun <T> withContextMap(
  map: Map<String, String>,
  block: () -> T,
): T {
  val oldMap = ContextMap.view
  return try {
    ContextMap += map
    block()
  } finally {
    ContextMap.clear()
    ContextMap += oldMap
  }
}

Could also be related to #36. We may want to do something like:

val log = parentLog.withContext(...)

...

log.debug { ... }

Add an extension property for storing a cached logger

Analogous to the extension function T.logger() which constructs a logger using the class of T, an extension property T.logger should be added for an alternative to using the Logging mixin. The general change is fairly simple:

inline val <reified T> T.logger: KotlinLogger
  get() = cachedLoggerOf(T::class.java)

Support LogBuilder API in a kotlin-y way

The Kotlin API does not have an equivalent to the https://logging.apache.org/log4j/2.x/manual/logbuilder.html.

A Kotlin-y approach may use a DSL rather than a builder. For example, a LoggingScope passed as a lambda receiver could result in an API that could look something like:

logger.atError {
  // `this` here is an instance of `LoggingScope`
  // everything optional except log
  throwable = ...
  marker = ...
  location = ...
  log = ...
}

The standard builder API could also be supported as an alternative for users who prefer it.

See also relevant background discussion about designing a fluent logging API from https://github.com/google/flogger and https://google.github.io/flogger/benefits.

Support Kotlin multi-platform

It would be nice if the API supported Kotlin multi-platform, and so could be used on other platforms that Kotlin targets, such as native, JavaScript, and WASM.

Log4j2 is obviously a JVM-only solution but this library is mostly just an API. We could make it easier for users to plugin in different backends for different platforms, and provide some reasonable choices out of the box for each platform.

Open KotlinLogger

Open KotlinLogger because I want to inherit it, rewrite the error method, and count the number of error calls. Of course, if there are other ways to meet my needs, I would be more than happy to ask for your advice

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.