Utilities

Logger

desc A simple logger with extensibility for using other 3rd party loggers
jar kiit.common.jar
package kiit.common.log
artifact dev.kiit:kiit-common
sources src/lib/kotlin/slatekit-common/src/main/kotlin/slatekit/common/log
example src/lib/kotlin/slate-examples/src/main/kotlin/slatekit/examples/Example_Logger.kt
dependency kiit-results


Gradle

Use the following settings in gradle for installing this component.

    
    buildscript {
        // Kotlin version currently being used
        ext.kotlin_version = '1.8.22'

        // Reference version 
        ext.kiit_version = '3.1.0'

        // ...
    }

    repositories {
        // Currently stored in Git Hub Packages.
        maven {
            url "https://maven.pkg.github.com/slatekit/kiit"
            credentials {
                username = System.getenv('GIT_PACKAGES_INSTALL_ACTOR')
                password = System.getenv('GIT_PACKAGES_INSTALL_TOKEN')
            }
        }

        // ...
    }

    dependencies {
        // Other dependencies ...
        implementation "dev.kiit:kiit-common:$kiit_version"
    }
    



Import

  
// required 
import kiit.common.log.*


// optional 
import kiit.results.Try
import kiit.results.Success
  


Setup

  // NOTE: The logger is very simple and designed to be extended for customization
  // especially to use a more robust logging system such as :
  // 1. log4net
  // 2. loggly
  // 3. new relic
  override val logger:Logger? = LoggerConsole()


  // Setup a custom logger
  class MyCustomLogger : Logger(LogLevel.Warn)  {


    override val logger:Logger? = LoggerConsole()


    override fun performLog(entry: LogEntry)
    {
      println("custom logger : " + entry.level + " : " + entry.msg)
    }
  }


  


Usage

    // Sample exception
    val ex = IllegalArgumentException("Example exception")

    // CASE 1: Different ways to log with the static logger
    // 1. message only
    // 2. message + exception
    // 3. message + exception + tag
    val logger = LoggerConsole(LogLevel.Debug)
    logger.debug("debug with message only")
    logger.info("info with message and exception", ex)
    logger.warn("debug with message, exception, and tag", ex)
    logger.fatal("fatal message")

    // CASE: 2 Standard info, warn, error levels available
    // Same overloads ( msg, ex, tag ) are available.
    logger.debug("debug")
    logger.info("info")
    logger.warn("warn")
    logger.error("error")
    logger.fatal("fatal")


    // CASE 3: Log explicitly using log method.
    logger.log(LogLevel.Error, "error", ex)


    // CASE 4: You can extend a class with the LogSupportIn trait
    // to add logging methods to any class. The trait expects to have
    // a _log member variable available.
    debug("debug from trait")
    info ("info from trait")
    warn ("warn from trait")
    error("error from trait")
    fatal("fatal from trait")


    // CASE 5: Custom logger ( see setup above )
    // YOu just have to implement the log method.
    val log = MyCustomLogger()
    log.debug("debug from trait")
    log.info ("info from trait")
    log.warn ("warn from trait")
    log.error("error from trait")
    log.fatal("fatal from trait")