log with log4j configuration not work - java

i have this configuration in my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="FileLogger" fileName="HelloLog4j.log" append="false">
<PatternLayout pattern="%d %t %-5p %c{2} - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="hello" level="trace">
<AppenderRef ref="FileLogger"/>
</Logger>
<Root level="trace">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
i put this file in src/resources, and i follow this guide: Guide
but when i run my project i have this error:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

I would avoid the use of DOMConfigurator, stick to the standard: put log4j2.xml in your classpath. Where that is depends on your app and your runtime environment.

The XML file has to be inside the src folder itself and not inside src/resources or anything inside it, it has to be at root level of the folder.However if that does not work out here is a slightly different way of doing it.
You can use DOM Configurator to configure the logger manually.
Below is a sample snippet which describers how you can use it.
private static org.apache.log4j.Logger logger;
public static Logger getLogger(){
if(logger == null){
logger= LogManager.getLogger(ClassName.class);
DOMConfigurator.configure("path/to/log4jxml");
}
return logger;
}
Class name would be the class in which you are instantiating the class.It is good practice to use Singleton design pattern for this in a custom class and then instantiating it via the getLogger method.Don't forget to make your constructor private.
Edit:
A sample config for console appender.The conversion pattern is date layout.
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="console" />
</root>

Related

Is there a way to use different pattern for different packages when using log4j2?

I'm using log4j (through slf4j) and I'd like logs from some packages to output filename and line numbers but for others I just need the level and the message. Is there a way to achieve this? Is it possible to specific different layout.ConversionPattern for different classes/packages using the log4j2.properties file?
You can use more than one appender of the same type:
Here is an example:
<Appenders>
<Console name="ConsoleA" target="SYSTEM_OUT">
<PatternLayout pattern=="%-4r [%t] %-5p %c %x - %m%n" />
</Console>
<Console name="ConsoleB" target="SYSTEM_OUT">
<PatternLayout pattern=="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.foo.BarA" level="trace">
<AppenderRef ref="ConsoleA"/>
</Logger>
<Logger name="com.foo.BarB" level="trace">
<AppenderRef ref="ConsoleB"/>
</Logger>
</Loggers>
Read more at Log4J configuration manual

How to configure java application having 2 log4j2.xml files so that both configurations work fine

We are running a Java file named Calc.java and for this application, we are using log4j2.xml file for logging to a file named LogCalc.txt. Our Calc.java is using a .jar file named Addition.Jar, which is composed of Add.java, which also uses log4j2.xml file for its own logging to a file named LogAdd.txt.
The question here is, when we run Calc.java and access a method from Addition.Jar, logging is only happening in LogCalc.txt, the configurations in log4j2.xml of Calc.java is taken into account and log4j2.xml of LogAdd.txt is not taken into account. Due to this I am not able to get logs from Addition.jar, LogAdd.txt is empty.
How can we change our configurations such that we can see both logs from Calc.java in LogCalc.txt as well as from Add.java in LogAdd.txt, i.e., both XML configurations must work fine in our application and both of them logging to different files with their own configurations taken from respective XML files).
Our aim here is to make a project where main app uses log4j2.xml and also the included .jar in this project is using log4j2.xml, but we should get logs from both of them to their respective appenders separately without any problem.
Our Xml files look like this. log4j2.xml used in Addition.jar is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="A1" fileName="A1.log" append="false">
<PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.log4j.xml" level="debug">
<AppenderRef ref="A1"/>
</Logger>
<Root level="warn">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
</Configuration>
The log4j2.xml file used with Calc.java is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="A1" fileName="A1.log" append="false">
<PatternLayout pattern="%t %-5p %c{2} - %m%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="org.apache.log4j.xml" level="debug">
<AppenderRef ref="A1"/>
</Logger>
<Root level="info">
<AppenderRef ref="A1"/>
</Root>
</Loggers>
</Configuration>
Log4j will locate the first log4j2.xml that it finds on the classpath and use that for configuration. It does not currently support multiple configurations, although work is in progress to allow it to. Even if it did it would not work the way your configuration won't work because the loggers are the same.
I suggest you review the log4j guide online. Log4j does not log on the basis of jar files but by comparing the names of the loggers you are using against what is configured. If you want everything in one jar to go to one file then use a logger name such as Calc for the first jar and a logger name of LogCalc for the second jar. Then have your configuration route the events for each of the loggers to the appropriate files.

Log4j2 not logging to file

I'm using Log4j2 in my application and it seems to load and initialize fine.
Console gets logged to, the 2 specified file appenders create their respective files, but those files stay empty!
Is my config faulty? Did I start Log4j2 the wrong way?
Here's the config:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="TRACE">
<Appenders>
<File name="file" fileName="${sys:logdir}ftm.log">
<PatternLayout pattern="%d{MM.dd.yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<File name="file_error" fileName="${sys:logdir}ftm_error.log">
<PatternLayout pattern="%d{MM.dd.yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{MM.dd.yyyy HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="file" level="ALL"/>
<AppenderRef ref="STDOUT" level="ALL"/>
</Root>
<Logger name="FTM" level="ALL" additivity="true">
<AppenderRef ref="file_error" level="ERROR"/>
</Logger>
</Loggers>
</Configuration>
And here's the code:
LoggerContext contextLogging = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("log4j2.xml");
if (file != null && file.isFile() && file.canRead()){
contextLogging.setConfigLocation(file.toURI());
}
I start logging at various points throughout the application after this initialization bit.
I suspect your call to obtain the LoggerContext is causing Log4j to initialize. You should see a message saying the default configuration is being used. Why not just use one of the Configurator.initialize() methods?
Maybe LOG4J2 is buffering your log messages and will write them later.
Have you tried shutting down your app server?
This will force any buffered messages to be written to the file.

One logfile per run with log4j

How do you configure log4j.properties to have exactly one logfile per run of an app.
I've read that you should use a timestamp in the filename but that will create many files per run as time goes by.
I tried DailyRollingFileAppender and RollingFileAppender but can't find a way to configure exctly one log per run. The log should not be broken into multiple logs and it shouldn't be truncated and files of old runs should be preserved.
Each class has a static org.slf4j.Logger for it's own class name:
private static final Logger log = LoggerFactory.getLogger(Foo.class);
This is my current log4j.properties
log4j.rootLogger=error, RootAppender, RootErrorAppender
#log4j.logger.com.example=info, qspaBackendAppender, stderr
log4j.logger.com.example=info, qspaBackendAppender
log4j.additivity.com.example=true
#log4j.logger.com.example.util=trace, qspaBackendAppender, stderr
#log4j.additivity.com.example.util=true
log4j.appender.qspaBackendAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.qspaBackendAppender.file=logs/qspaBackend.log
log4j.appender.qspaBackendAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.qspaBackendAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n
log4j.appender.stderr=org.apache.log4j.ConsoleAppender
log4j.appender.stderr.Target=System.err
log4j.appender.stderr.layout=org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern=%-5p %c{1}:%L - %m%n
log4j.appender.RootAppender=org.apache.log4j.RollingFileAppender
log4j.appender.RootAppender.file=logs/root.log
log4j.appender.RootAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n
log4j.appender.RootErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.RootErrorAppender.file=logs/rootError.log
log4j.appender.RootErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootErrorAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n
log4j.appender.RootErrorAppender.threshold=error
I had troubles retrieving which Udo Klimaschewski's answer Udy was referring so I put here my solution.
log4j.properties:
# Root logger option
log4j.rootLogger=INFO, fileout
# Direct log messages to file
log4j.appender.fileout=org.apache.log4j.FileAppender
log4j.appender.fileout.File=/logs/myapp_${current.date}.log
log4j.appender.fileout.ImmediateFlush=true
log4j.appender.fileout.Threshold=debug
log4j.appender.fileout.Append=false
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.conversionPattern=%5p | %d | %m%n
Then put in the main class this block:
public class Starter {
static{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hhmmss");
System.setProperty("current.date", dateFormat.format(new Date()));
}
Combine the answer of Udo Klimaschewski and the answer from this question in order to get the desired result.
add the property append = false
add a current.time system property and use it in the file name
To start a new log file on app startup you may want to use OnStartupTriggeringPolicy
Triggering Policies
Example of XML config:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="log-path">C:/Logs/</Property>
</Properties>
<Appenders>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<RollingFile name="RollingFile" fileName="${log-path}/app.log"
filePattern="${log-path}/app-%d{yyyy-MM-dd}-%i.log">
<PatternLayout>
<pattern>%d{dd/MM/yyyy HH:mm:ss} [%-5p/%t]: %C{1}(%L): %m%n</pattern>
</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
<OnStartupTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="100"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="root" level="debug" additivity="false">
<appender-ref ref="RollingFile" level="debug"/>
<appender-ref ref="console" level="debug"/>
</Logger>
<Root level="debug" additivity="false">
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
Works well for me
Using filePattern Property
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<property name="filePattern">${date:yyyy-MM-dd-HH_mm_ss}</property>
</Properties>
<Appenders>
<File name="File" fileName="export/logs/app_${filePattern}.log" append="false">
<PatternLayout
pattern="%d{yyyy-MMM-dd HH:mm:ss a} [%t] %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>

log4j2 configuration

Someone can say me how I can change my log4j2.xml to add 1 log file: one level trace and one level info?
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug">
<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
</Console>
<File name="DEBUG_FILE" fileName="debug.txt">
<PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
</File>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
</root>
</loggers>
</configuration>
You could make the root logger TRACE level (all messages) and put a ThresholdFilter on the Console so that only some messages are displayed on the console.
This configuration will log only ERROR messages to the console, and at the same time will log all messages (TRACE, DEBUG, INFO, ERROR...) to the debug.txt file. Error and higher messages are logged both in the console and in the file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="ERROR">
<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
</Console>
<File name="DEBUG_FILE" fileName="debug.txt">
<PatternLayout pattern="%d %-5p %C{2} (%F:%L) - %m%n" />
</File>
</appenders>
<loggers>
<root level="trace">
<appender-ref ref="CONSOLE" />
<appender-ref ref="DEBUG_FILE" />
</root>
</loggers>
</configuration>
By the way, please be aware that your pattern layout contains conversion patterns that require location information (%C, %F and %L, to be exact).
This means that for every log message, log4j2 needs to take a snapshot of the stack, and then walk the stack trace to find the class and method that did the logging.
This is very, very slow.
Logging will be 2-5 times slower for synchronous logging and 4-20 times slower for asynchronous logging.
If performance is not an issue then it doesn't matter, but it is definitely something to be aware of. Personally I just use %c (the logger name) and my app's log messages are unique enough that I can quickly find where the message came from. Just my 2 cents.
--- update 2013-04-27 ---
I have since learned a simpler way to do this: you can set a level on the appender-ref.
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug">
<appenders>
<Console name="CONSOLE" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p %c{2} - %m%n" /> <!--without location-->
</Console>
<File name="DEBUG_FILE" fileName="debug.txt">
<PatternLayout pattern="%d %-5p %c{2} - %m%n" />
</File>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="CONSOLE" level="ERROR" />
<appender-ref ref="DEBUG_FILE" />
</root>
</loggers>
</configuration>

Categories

Resources