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