Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin logger - log to a file

class LogToFile(context: Context) {
        companion object: KLogging()
        val formatter = SimpleFormatter()

        // val logger = LoggerFactory.getLogger("MyLog") **WITH THIS LINE...**

        val logger = Logger.getLogger("MyLog") //this line WORKS
        val dest = context.applicationContext.getExternalFilesDir(null);
        val fh = FileHandler(dest.path.plus(File.pathSeparator).plus("data.txt"))

        init {

            //..THIS LINE DOESN'T WORK (NO addHandler is there some ekvivalent for the LoggerFactory?)//

            logger.addHandler(fh)
            fh.formatter = formatter
        }


        fun write(logString: String) {

            try {

                logger.info(logString)

            } catch (e: SecurityException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
}

**This is a functioning code I have, which takes a log and writes it into a file.

In the past few days, I haven't been able to find a way, how to do the same thing using KotlinLogger.

I am trying to write all the logs into a file.

I am a coding beginner, so I hope the question is written well enough. If it isn't I will complete it.

I have been googling how to do it and I found nothing, so I hope it is possible.

The Logger that's working is 'java.util.logging.Logger' the one I want to use is 'mu.KLogging' (or possibly 'org.slf4j.LoggerFactory')

UPDATE: I found this: https://github.com/tony19/logback-android/wiki currently implementing:

 implementation 'io.github.microutils:kotlin-logging:1.4.9'
    implementation 'org.slf4j:slf4j-api:1.7.25'
    compile 'com.github.tony19:logback-android-core:1.1.1-6'
    compile('com.github.tony19:logback-android-classic:1.1.1-6') {
        // workaround issue #73
        exclude group: 'com.google.android', module: 'android'

simple log into log cat works now. Still working on writing the logs into a file. **

like image 205
Sophie M Avatar asked Jan 29 '18 09:01

Sophie M


Video Answer


1 Answers

So, I managed to log into the logcat and a file. Since I never found this problem solved anywhere else, I am posting here in case someone needs it sometime.

I compiled these in my gradle file:

dependencies {
    implementation 'io.github.microutils:kotlin-logging:1.4.9'


implementation 'com.github.tony19:logback-android-core:1.1.1-6'
implementation('com.github.tony19:logback-android-classic:1.1.1-6') {
    // workaround issue #73
    exclude group: 'com.google.android', module: 'android'
}

implementation 'org.slf4j:slf4j-api:1.7.25'

implementation 'log4j:log4j:1.2.17'

I used a companion object in the logging class:

import mu.KLogging

class LogToFile(context: Context) {

    companion object: KLogging()

fun write(){
logger.info("Hello World")}
}

and made a logback.xml configurations file:

<property name="USER_HOME" value='file_path'/>

<appender name="LOG" class="ch.qos.logback.core.FileAppender">
<!--append to file-->
    <file>
    ${USER_HOME}/myApp.log
</file>
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
         ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>

<root level="debug">
    <appender-ref ref="LOG"/>
    <appender-ref ref="STDOUT" />
</root>

so it currently writes into a file and a logcat and everything works just fine.

like image 120
Sophie M Avatar answered Oct 18 '22 03:10

Sophie M