我使用org.slf4j.LoggerFactory
记录我的Kotlin项目。
在我的gradle.build
中dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile(
"org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
//,'commons-io:commons-io:2.6'
, "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+"
, "ch.qos.logback:logback-classic:1.2.3"
// DB
, "com.zaxxer:HikariCP:3.1.0"
, 'org.postgresql:postgresql:42.2.2'
, "com.squareup.okhttp3:okhttp:3.12.1"
, "org.apache.commons:commons-compress:1.18"
//,"com.github.ccob:bittrex4j:1.0.9"
// https://mvnrepository.com/artifact/com.google.code.gson/gson
, 'com.google.code.gson:gson:2.8.5'
)
testCompile group: 'junit', name: 'junit', version: '4.11'
}
在我的项目中,我有logback.xml
和下一个内容:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<shutdownHook/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<Pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</Pattern>
</encoder>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover. Make sure the path matches the one in the file element or else
the rollover logs are placed in the working directory. -->
<fileNamePattern>debug_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<charset>UTF-8</charset>
<pattern>%d %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<logger name="com.github.ccob.bittrex4j" level="ERROR" />
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>
如您所见,我设置为登录到console
和 file
。
在 MyApp.kt
import org.slf4j.LoggerFactory
fun main(args: Array<String>) {
logger.info("STARTED")
我通过影子插件创建可执行jar。
因此,我将my-app-1.0-SNAPSHOT-all.jar
复制到d:/temp/
文件夹中。我也将文件logback.xml
复制到文件夹d:/temp/
。
然后我以以下身份启动应用。
java -jar my-app-1.0-SNAPSHOT-all.jar
我将成功和成功日志记录到控制台。
很好。
但是为什么不记录到文件?