1

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);
007_programmer
  • 35
  • 1
  • 10

1 Answers1

1

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.

Piotr P. Karwasz
  • 12,857
  • 3
  • 20
  • 43
  • Logs file are generating now. Thanks!! – 007_programmer Apr 14 '22 at 07:45
  • One more doubt, I want to print hostname and timestamp in my log file with "appender.rolling.layout.type = JsonLayout" not with PatternLayout. Could you please help me with the pattern I should here "appender.console.layout.pattern:". I do use "%d{yyyy-MM-dd HH:mm:ss.SSS} source_host:${hostName}" for date and timestamp but timestamp is coming in epoch time and it is not printing any hostname. I don't want epoch time, I want to print normal time format in the form of "yyyy-MM-dd HH:mm:ss.SSS". Could you please help me with the format. – 007_programmer Apr 14 '22 at 07:50
  • `JsonLayout` does not have a `pattern` property. If you want to modify the format it uses switch to the more recent [JSON Layout Template](https://logging.apache.org/log4j/2.x/manual/json-template-layout.html): it is the JSON equivalent of `PatternLayout`. – Piotr P. Karwasz Apr 14 '22 at 08:20
  • Thanks for the suggestion. I used Json layout template in my project but I am getting the logs with few parameters missing. I have tried to mention those missing parameters in pattern itself but I guess that was not a right approach to get those parameters in my logs. Any suggestion on that? – 007_programmer Apr 19 '22 at 09:19
  • You should post another question about this. BTW: if this answer solves your original problem, you should consider marking it as accepted (the checkmark sign beside the question). – Piotr P. Karwasz Apr 19 '22 at 09:59
  • Sure I will create a separate question. I mark it accepted. Sorry I didn't aware about that. – 007_programmer Apr 19 '22 at 11:30