scala配置log4j+slf4j
环境信息
jdk 17
scala 2.11.0
导入依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
<version>2.0.9</version>
</dependency>
添加配置文件
resource
目录下新建 log4j.properties
### 设置log级别为DEBUG, 并将log分别分发给stdout, d, e
log4j.rootLogger = DEBUG, stdout, D, E
### stdout相关配置 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold = DEBUG
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] [%d{yyyy-MM-dd HH:mm:ss,SSS}] [%C{1}.%M:%L] [%t] - %m%n
### D相关配置, 输出DEBUG及以上级别的日志到logs/log.log文件 ###
log4j.appender.D = org.apache.log4j.FileAppender
log4j.appender.D.File = logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = [%-5p] [%d{yyyy-MM-dd HH:mm:ss,SSS}] [%C{1}.%M:%L] [%t] - %m%n
### E相关配置, 输出ERROR级别的日志到logs/error.log文件 ###
log4j.appender.E = org.apache.log4j.FileAppender
log4j.appender.E.File = logs/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = [%-5p] [%d{yyyy-MM-dd HH:mm:ss,SSS}] [%C{1}.%M:%L] [%t] - %m%n
代码测试
import org.slf4j._
object Main{
private val logger: Logger = LoggerFactory.getLogger(getClass)
def main(args: Array[String]): Unit = {
logger.debug("11111111")
logger.info("22222222")
logger.warn("33333333")
logger.error("4444444")
}
}
依赖冲突
我在导入spark依赖后,出现了依赖冲突,导致log4j配置文件被覆盖,无法生效,
解决方案是找到依赖冲突的那个依赖,手动删除掉依赖自带的log4j
错误信息
SLF4J: Class path contains multiple SLF4J providers.
SLF4J: Found provider [org.apache.logging.slf4j.SLF4JServiceProvider@1ae369b7]
SLF4J: Found provider [org.slf4j.reload4j.Reload4jServiceProvider@6fffcba5]
SLF4J: See https://www.slf4j.org/codes.html#multiple_bindings for an explanation.
删除依赖
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>3.5.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
标签:log,scala,log4j,slf4j,apache,org,appender
From: https://www.cnblogs.com/INnoVationv2/p/17756651.html