5
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()
            }
        }
}

**这是我拥有的功能代码,它获取日志并将其写入文件。

在过去的几天里,我一直无法找到一种方法,如何使用 KotlinLogger 做同样的事情。

我正在尝试将所有日志写入文件。

我是一个编码初学者,所以我希望这个问题写得足够好。如果不是,我会完成它。

我一直在谷歌搜索如何做到这一点,但我一无所获,所以我希望这是可能的。

正在工作的记录器是“java.util.logging.Logger”,我想使用的是“mu.KLogging”(或者可能是“org.slf4j.LoggerFactory”)

更新:我发现这个:https ://github.com/tony19/logback-android/wiki 目前正在实施:

 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'

简单的登录到 log cat 现在可以工作了。仍在努力将日志写入文件。**

4

1 回答 1

12

所以,我设法登录到 logcat 和一个文件。因为我从来没有发现这个问题在其他任何地方都得到了解决,所以我在这里发帖以防有人需要它。

我在我的 gradle 文件中编译了这些:

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'

我在日志记录类中使用了一个伴随对象:

import mu.KLogging

class LogToFile(context: Context) {

    companion object: KLogging()

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

并制作了一个 logback.xml 配置文件:

<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>

所以它目前写入一个文件和一个logcat,一切正常。

于 2018-01-30T12:03:59.910 回答