日志正在控制台中写入但未写入日志文件
Logs are writing in console but not writing to log file
最近我从 log4j1.x 迁移到 log4j2.x。我用 log4j2.properties 文件替换了 log4j.properties。我正在控制台中打印日志,但日志未打印在 sac.log 文件中。
Pom 文件:
- log4j-jcl:2.17.1
- log4j-核心:2.17.1
- log4j-api:2.17.1
- 杰克逊核心:2.13.1
- 杰克逊注释:2.13.1
- 杰克逊数据绑定:2.13.1
Log4j.properties:
log4j.rootLogger=INFO, stdout, RollingLog
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%C{0}.%M:(%L) - %m%n
log4j.appender.RollingLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingLog.Threshold=TRACE
log4j.appender.RollingLog.File=SD/sac.log
log4j.appender.RollingLog.Append=true
log4j.appender.RollingLog.DatePattern=.yyyy-MM-dd
log4j.appender.RollingLog.layout=net.logstash.log4j.JSONEventLayoutV1
log4j.appender.RollingLog.rolling.rollingPolicy = org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.RollingLog.rollingPolicy.FileNamePattern = SD/sac/sac-{yyyyMMdd}.log.gz
Log4j2.properties:
status = error
dest = err
filter.threshold.type = ThresholdFilter
filter.threshold.level = trace
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern =%C{0}.%M:(%L) - %m%n
#File Appender for JSON log file.
appender.rolling.type = RollingFile
appender.rolling.name = RALLog
appender.rolling.fileName = SD/sac.log
appender.rolling.filePattern = SD/sac/sac-{yyyyMMdd}.log.gz
appender.rolling.layout.type = JsonLayout
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
logger.rolling.name = SD
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RALLog
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT
A.java
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class A {
private static final Log logger = LogFactory.getLog(A.class);
您的 logger.rolling
记录器配置的 name
是“SD”,因此只有记录器“SD”和名称以“SD”开头的记录器。将记录到您的文件。
您 A
class 中使用的记录器名称是 class 的完全限定名称。
最近我从 log4j1.x 迁移到 log4j2.x。我用 log4j2.properties 文件替换了 log4j.properties。我正在控制台中打印日志,但日志未打印在 sac.log 文件中。
Pom 文件:
- log4j-jcl:2.17.1
- log4j-核心:2.17.1
- log4j-api:2.17.1
- 杰克逊核心:2.13.1
- 杰克逊注释:2.13.1
- 杰克逊数据绑定:2.13.1
Log4j.properties:
log4j.rootLogger=INFO, stdout, RollingLog
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%C{0}.%M:(%L) - %m%n
log4j.appender.RollingLog=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingLog.Threshold=TRACE
log4j.appender.RollingLog.File=SD/sac.log
log4j.appender.RollingLog.Append=true
log4j.appender.RollingLog.DatePattern=.yyyy-MM-dd
log4j.appender.RollingLog.layout=net.logstash.log4j.JSONEventLayoutV1
log4j.appender.RollingLog.rolling.rollingPolicy = org.apache.log4j.rolling.TimeBasedRollingPolicy
log4j.appender.RollingLog.rollingPolicy.FileNamePattern = SD/sac/sac-{yyyyMMdd}.log.gz
Log4j2.properties:
status = error
dest = err
filter.threshold.type = ThresholdFilter
filter.threshold.level = trace
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern =%C{0}.%M:(%L) - %m%n
#File Appender for JSON log file.
appender.rolling.type = RollingFile
appender.rolling.name = RALLog
appender.rolling.fileName = SD/sac.log
appender.rolling.filePattern = SD/sac/sac-{yyyyMMdd}.log.gz
appender.rolling.layout.type = JsonLayout
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
logger.rolling.name = SD
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RALLog
rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT
A.java
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class A {
private static final Log logger = LogFactory.getLog(A.class);
您的 logger.rolling
记录器配置的 name
是“SD”,因此只有记录器“SD”和名称以“SD”开头的记录器。将记录到您的文件。
您 A
class 中使用的记录器名称是 class 的完全限定名称。