I am using log4j2 with slf4j and I want to write my logs to a file. This is my log4j2.xml configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<RandomAccessFileAppender name="Logfile"
fileName="\\domain\path\logs\log.txt">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</RandomAccessFileAppender>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Logfile" />
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Logging to the console works, but I do not get any logs in the file. I get the following error message on the console:
2014-05-28 10:17:01,164 ERROR Error processing element RandomAccessFileAppender: CLASS_NOT_FOUND
2014-05-28 10:17:01,309 ERROR Unable to locate appender Logfile for logger
Why does log4j not find the class? Do I have to use additional dependencies?
Related
This is my configuration log4j2.xml with path to file - src/com/tarasiuk/task_01/log/dataLogger.log:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="FORMAT_MESSAGE">
%d{YYYY-MM-dd HH:mm:ss} [%t] Level:%-7p Class:%c{1}:%-5L - %msg%n
</Property>
<Property name="LOG_FILE">
src/com/tarasiuk/task_01/log/dataLogger.log
</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${FORMAT_MESSAGE}" />
</Console>
<File name="File" fileName="${LOG_FILE}" append="false">
<PatternLayout pattern="${FORMAT_MESSAGE}" />
</File>
</Appenders>
<Loggers>
<Logger name="org.apache.logging.log4j.test2" level="debug" additivity="false">
<AppenderRef ref="File" />
</Logger>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
Structure of my project:
What I do:
change path to log file from src/com/tarasiuk/task_01/log/dataLogger.log to com/tarasiuk/task_01/log/dataLogger.log - no result.
change level in <Logger> from debug to info - no result.
Logs are output to the console - that ok. But why Log4j2 doesn't write logs to a file?
Try with below appender.
May be in your case it is not able to get path from property, so i had provided only name.
So automatically it will create file on same path as your application is.
<Appenders>
<File name="dataLogger" fileName="dataLogger.log" append="false">
<PatternLayout pattern="%level - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%level - %m%n"/>
</Console>
</Appenders>
This will help you.
Below is my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="log" fileName="logs/log.txt">
<PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="Console" level="info"/>
<appender-ref ref="log" level="info"/>
</root>
</loggers>
</configuration>
I would like to print whatever I see on the console into my logs.txt file as well.
What am I doing wrong?
I think You are missing the levelrangefilter tag .
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="log" fileName="logs/log.txt"
filePattern="logs/log.txt.%i" >
<LevelRangeFilter minLevel="FATAL" maxLevel="ALL" onMatch="ACCEPT" onMismatch="DENY"/>
</RollingFile>
</Appenders>
<loggers>
<root level="debug">
<appender-ref ref="Console" level="info"/>
<appender-ref ref="log"/>
</root>
</loggers>
</Configuration>
I created separate simple log class: log.java)
e.g.,
import org.apache.log4j.LogManager;
public class Logs {
private static final org.apache.log4j.Logger logger= LogManager.getLogger(Logs.class);
public static org.apache.log4j.Logger Log() {
return logger;
}
}
and using following log4j2.xml file
<Configuration status="INFO">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="${log-path}/Log${date:yyyy-MM-dd-HH-mm-ss}.log" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile" />
</Root>
</Loggers>
</Configuration>
Question: I want to pass log file name say testbest.log to my log class so that when log is created the log file name would be testbest.log instead of Log${date:yyyy-MM-dd-HH-mm-ss}.log
How can I achieve this?
log4j.xml
<Configuration status="INFO">
<Properties>
<Property name="log-path">logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<File name="MyFile" fileName="${log-path}/${sys:log-file-name}" immediateFlush="false" append="false">
<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console" />
<AppenderRef ref="MyFile" />
</Root>
</Loggers>
</Configuration>
You can specify file name as VM argument when starting
java -Dlog-file-name="testbest.log"
Or you can define it programmatically as system property
System.setProperty("log-file-name", "testbest.log");
I'm using the following .xml configuration to log to the console and also to a file.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="basePath">/logs</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<RollingFile name="File" fileName="${basePath}/log.log" filePattern="${basePath}/log-%d{yyyy-MM-dd}.log" immediateFlush="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
The problem is when I use the following code I get logs on the console but nothing is written to a file.
private static final Logger logger = LogManager.getLogger(App.class);
logger.error("Start App");
Also sometimes I get the following messages when I debug:
Note: when I print out System.getProperty("log4j.configurationFile") I get null
This is my log4j2.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="runid">$${date:YYYYMMddHHmmssSSS}</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY/MM/dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="File" fileName="D:\Users\Rich\Documents\TestData\PagemonTest12\LogRun\Log-${runid}.log">
<PatternLayout pattern="%d{YYYY/MM/dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
It creates a property called runid based on the current date and time. How can I access runid (the created runid, not the text in the xml file) as a String in my Java 7 application?
This is the code to get property from log4j2.xml:
if (ctx == null) {
ctx = (LoggerContext) LogManager.getContext();
config = (XMLConfiguration) ctx.getConfiguration();
}
app = config.getStrSubstitutor().getVariableResolver()
.lookup(Constants.CONF_APP_PROPERTIE);
${bundle:br.com.domain.sadt.server.config:config.log.folder}
where:
- br.com.domain.sadt.server.config = your config.properties
- config.log.folder = key
to use in xml
${folder}