我按照这篇文章设置了logback。
注意:我使用的是
,而不是logback
logback-android
项目依赖:
implementation 'org.slf4j:slf4j-api:2.0.7'
implementation 'ch.qos.logback:logback-classic:1.4.7'
implementation 'ch.qos.logback:logback-core:1.4.7'
xml配置文件
app\src\main\resources\logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Logcat"
class="ch.qos.logback.classic.android.LogcatAppender">
<encoder>
<Pattern>%date %level %msg%n</Pattern>
</encoder>
</appender>
<appender name="File" class="ch.qos.logback.core.FileAppender">
<file>./log/test.log</file>
<encoder>
<pattern>%date %level %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="Logcat" />
<appender-ref ref="File"/>
</root>
</configuration>
然后像这样使用
logger
:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val logger = LoggerFactory.getLogger("qwdqwd")
logger.debug("hello")
setContent {
AndroidcomposelatestTheme {
// A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
ArticleScreen2()
}
}
}
}
}
出现错误:
java.lang.NoSuchMethodError: No virtual method getModule()Ljava/lang/Module; in class Ljava/lang/Class; or its super classes (declaration of 'java.lang.Class' appears in /apex/com.android.runtime/javalib/core-oj.jar)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at ch.qos.logback.core.util.EnvUtil.logbackVersionByModule(EnvUtil.java:56)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at ch.qos.logback.core.util.EnvUtil.logbackVersion(EnvUtil.java:36)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:81)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:77)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at ch.qos.logback.classic.spi.LogbackServiceProvider.initializeLoggerContext(LogbackServiceProvider.java:50)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at ch.qos.logback.classic.spi.LogbackServiceProvider.initialize(LogbackServiceProvider.java:41)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at org.slf4j.LoggerFactory.bind(LoggerFactory.java:183)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:170)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at org.slf4j.LoggerFactory.getProvider(LoggerFactory.java:455)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:441)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:390)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at com.sage.MainActivity.onCreate(MainActivity.kt:57)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at android.app.Activity.performCreate(Activity.java:8214)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at android.app.Activity.performCreate(Activity.java:8202)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1320)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4033)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4247)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
2023-05-22 09:47:22.672 4644-4644 System.err com.sage W at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
2023-05-22 09:47:22.673 4644-4644 System.err com.sage W at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
2023-05-22 09:47:22.673 4644-4644 System.err com.sage W at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2613)
2023-05-22 09:47:22.673 4644-4644 System.err com.sage W at android.os.Handler.dispatchMessage(Handler.java:110)
2023-05-22 09:47:22.673 4644-4644 System.err com.sage W at android.os.Looper.loop(Looper.java:219)
2023-05-22 09:47:22.673 4644-4644 System.err com.sage W at android.app.ActivityThread.main(ActivityThread.java:8668)
2023-05-22 09:47:22.673 4644-4644 System.err com.sage W at java.lang.reflect.Method.invoke(Native Method)
2023-05-22 09:47:22.673 4644-4644 System.err com.sage W at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
2023-05-22 09:47:22.673 4644-4644 System.err com.sage W at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1109)
我错过了什么?
对于那些想要在 Android 中使用 Logback classic 并具有解决方案的人,因为
Logback-android
已经过时了
第一步
添加打包选项,Logback classic 和 Logback core 都包含 INDEX.LIST 需要选择其一才能构建成功。
android {
packagingOptions {
pickFirst 'META-INF/INDEX.LIST'
}
}
第二步
克隆
ContextInitializer
然后在 autoConfig()
替换下面的块代码
var versionStr = EnvUtil.logbackVersion()
if (versionStr == null) {
versionStr = CoreConstants.NA
}
由
var versionStr = CoreConstants.NA // or any string you want
第三步
克隆
LogbackServiceProvider
,然后替换为导入步骤 2 中克隆的 ContextInitializer
,而不是 Logback's ContextInitializer
最后一步
配置为让 SLF4j 选择您克隆的
LogbackServiceProvider
,而不是默认的,方法是创建一个 resources/META-INF/services/org.slf4j.spi.SLF4JServiceProvider 文件,其中包含指向您克隆的 LogbackServiceProvider
的值链接 - 详细信息参见图片