Search code examples
javalogginglog4jlog4j2properties-file

Logs are writing in console but not writing to log file


Recently I migrated from log4j1.x to log4j2.x. I had replaced log4j.properties with log4j2.properties file. I am getting logs printed in my console but logs are not printing in sac.log file.

Pom File:

  • log4j-jcl:2.17.1
  • log4j-core:2.17.1
  • log4j-api:2.17.1
  • jackson-core:2.13.1
  • jackson-annotations:2.13.1
  • jackson-databind: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);

Solution

  • The name of your logger.rolling logger configuration is "SD", therefore only the logger "SD" and the loggers, whose name starts with "SD." will log to your file.

    The name of the logger used in your A class is the fully qualified name of that class.