Change Quartz appender in log4j2 - java

I'm trying to redirect all Quartz logging to a separate file, but it still keeps logging into the console. What am I doing wrong in the configuration file?
Here's a simplified version of my log4j2.xml
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<!--stuff-->
</Console>
<RollingFile name="Quartz">
<!--stuff-->
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.quartz" level="ALL">
<AppenderRef ref="Quartz"/>
</Logger>
<Logger name="com.rotoplastyc" level="ALL">
<AppenderRef ref="Console" />
</Logger>
<Root level="OFF">
</Root>
</Loggers>
</Configuration>

Found out that, as #teppic suggested in the comments, I needed the log4j-slf4j-impl lib in order for it to work properly, I was currently using the slf4j-simple lib which only logs into the console.

Related

What does ${sys:logger.out.type} mean in Log4j2?

I am currently working on tidying up the logs for an internal tool we have. I came across the use of ${sys:logger.out.type} inside the Loggers in log4j2.xml.
Here's a copy of our log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="OFF">
<Appenders>
<Console name="stdout" target="SYSTEM_OUT">..
<PatternLayout pattern="..."/>
</Console>
<RollingFile name="FILE" fileName="${sys:microservice.log}" filePattern="${sys:microservice.log}.%i">
<PatternLayout>
<Pattern>...</Pattern>
</PatternLayout>
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="300 MB" />
</Policies>
<DefaultRolloverStrategy max="5" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="warn" additivity="false">
<appender-ref ref="${sys:logger.out.type}"/>
</Logger>
<Logger name="org.springframework.jdbc.core" level="off" additivity="false">
<appender-ref ref="${sys:logger.out.type}"/>
</Logger>
<Logger name="org.springframework.jdbc.core.StatementCreatorUtils" level="off" additivity="false">
<appender-ref ref="${sys:logger.out.type}"/>
</Logger>
<Logger name="com.myOrgName" level="trace" additivity="false">
<appender-ref ref="${sys:logger.out.type}"/>
</Logger>
<Root level="info" additivity="false">
<appender-ref ref="${sys:logger.out.type}"/>
</Root>
</Loggers>
</configuration>
In my previous StackOverflow question, a very kind user redirected me to the Log4j Property Substitution page to better understand the semantics of ${sys:logger.out.type}.
After reading through the documentation, I understand the sys Prefix is referring to a system property but don't quite understand what logger.out.type means?
Taking a stab here, is it referring to the output type of a Logger? Which could mean it's referring to the Console element named stdout?
Appreciate any insights & feedback from the community. Should any of my understanding be mistaken, please correct me, I am also learning :) Thank you!
A property like ${sys:logger.out.type} in a log4j 2.x configuration file resolves to the value of the Java system property named logger.out.type.
You set system properties by passing the command line argument -Dlogger.out.type=stdout (or more generally -D<property name>=<value>) to the JVM when you invoke the java command.
You can also provide defaults, so ${sys:logger.out.type:-FILE} would choose your FILE appender if no value was set for that system property.

Turning off Log4j2 by setting Root level to "off" doesnt work

I have a problem with logging within Junit tests.
In log4j2.xml, I have changed <Root level="info"> to <Root level="off">:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d [%t] %-5p %c{1.} - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="off">
<AppenderRef ref="STDOUT"/>
</Root>
<!-- hibernate debugging -->
<Logger name="org.hibernate.SQL" level="DEBUG">
<AppenderRef ref="STDOUT" />
</Logger>
<Logger name="org.hibernate.type" level="TRACE">
<AppenderRef ref="STDOUT" />
</Logger>
</Loggers>
</Configuration>
But it doesn't help, I still get the same amount of logs. I need to make the logs dissapear to make progress in an test performance investigation, to be able to run my own allways despised System.out.println() in some of the tests. How can I achive that?

log4j 2 not showing dependency logs

I am using apache HttpClient to execute a POST request:
CloseableHttpResponse response = HttpClients.createDefault().execute(request);
I want to see the request (and other apache client library log statements) in the logs, but I can only see my application logs, no other logs from any dependency.
Here is my log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Lambda name="Lambda">
<PatternLayout>
<pattern>%d %X{AWSRequestId} %t %-5p [%X{userId}] %c{1}:%L - %m%n</pattern>
</PatternLayout>
</Lambda>
</Appenders>
<Loggers>
<Logger name="org.apache.http" level="debug">
<AppenderRef ref="Lambda"/>
</Logger>
<Root level="debug">
<AppenderRef ref="Lambda"/>
</Root>
</Loggers>
</Configuration>
Using this answer, I added the following, but it still doesn't show the apache logs.
<Logger name="org.apache.http.client" level="debug">
<AppenderRef ref="Lambda"/>
</Logger>
<Logger name="org.apache.http.impl.client" level="debug">
<AppenderRef ref="Lambda"/>
</Logger>
<Logger name="org.apache.http.impl.conn" level="debug">
<AppenderRef ref="Lambda"/>
</Logger>
I can confirm that this log4j2.xml is being used by log4j because the logs are following the <pattern> I wrote.
What am I missing? Please help.
Adding the Commons Logging Bridge dependency fixed it:-
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.11.0</version>
</dependency>
See this for more details.

Log4j2 - create new log file

I have configuration for save logs to file, but logs are append to existing content. I want to create always new file. How can I do that?
My log4j2.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="File" fileName="out.log">
<PatternLayout pattern="[%d{ISO8601} %-5level] %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="File"/>
</Root>
<Root level="info">
<AppenderRef ref="File"/>
</Root>
</Loggers>
</Configuration>
The FileAppender has a property named append which has a default value of true, so configure it like this:
<File name="File" fileName="out.log" append="false">
Documentation can be found at https://logging.apache.org/log4j/2.x/manual/appenders.html#FileAppender

Log4j2 with MemoryMappedFile and break files

Is that possible to use MemoryMappedFile with Policies to break the files until fixed sizes, for example 250MB? My log4j2.xml is like this but I want to break the log files to 250MB and I need to use MemoryMappedFile for IO performance.
<Configuration monitorInterval="30">
<Appenders>
<MemoryMappedFile name="MemoryMap" fileName="output/jscsi-out.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p [%t] %c{2} - %m%n(%L)" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="250 MB" />
</Policies>
</MemoryMappedFile>
</Appenders>
<Loggers>
<Logger name="br.com" level="debug" additivity="false">
<AppenderRef ref="MemoryMap" />
</Logger>
<Logger name="org.jscsi.target.TargetServer" level="debug"
additivity="false">
<AppenderRef ref="MemoryMap" />
</Logger>
<Logger name="org.jscsi.target.storage" level="info"
additivity="false">
<AppenderRef ref="MemoryMap" />
</Logger>
<Logger name="org.jscsi.service" level="debug" additivity="false">
<AppenderRef ref="MemoryMap" />
</Logger>
<Root level="error" includeLocation="true">
<AppenderRef ref="MemoryMap" />
</Root>
</Loggers>
</Configuration>
(Background: I am the author of the MemoryMappedFile appender as well as Async Loggers.)
In some sense the MemoryMappedFile appender is still a work in progress (as of Log4j 2.5). As you mention, there is no Rolling variant. Also, I haven't been able to do any significant performance testing. I made a start some time ago but other issues took priority.
Initial performance testing indicates that synchronous logging, even when using the MemoryMappedFile appender, is unlikely to be faster than asynchronous logging via Async Loggers.
Generally, if you need the rollover behaviour I would suggest you use Async Loggers in combination with the RollingRandomAccessFile appender for now.

Categories

Resources