How do I avoid repeating the code in log4j2.xml when using Spring Boot? - spring

There's a default log4j2.xml configuration in Spring Boot, I like the default coloring, but I don't want to have to copy the contents of the configuration in log4j2-spring.xml file is there a way to simply pull the value from somewhere?
I already tried
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}" charset="${sys:CONSOLE_LOG_CHARSET}"/>
</Console>
and
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${CONSOLE_LOG_PATTERN}" charset="${sys:CONSOLE_LOG_CHARSET}"/>
</Console>
and
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${env:CONSOLE_LOG_PATTERN}" />
</Console>

Related

log4j2 not generating logs on Staging machine

I have upgraded log4j 1 to log4j2 in our Spring project. Logs works fine on Dev and QA machines. But when we deploy it on Staging, logs are not being produced. When we turn on debug under there are logs on startup. But it gets silent after that without error. This is my configuration file.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="my-project" packages="">
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{ISO8601} %-5p (%x) [%t] %c{1} - %m%n"/>
</Console>
<!-- Debug Logger -->
<RollingFile name="DEBUG_LOG" fileName="/var/log/tomcat/my-project-backoffice-debug.log"
filePattern="/var/log/tomcat/my-project-backoffice-debug.log.%d{yyyy-MM-dd}.gz">
<ThresholdFilter level="DEBUG"/>
<PatternLayout >
<pattern>%d{ISO8601} %-5p (%x) [%t] %c{1} - %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<!-- Info Logger -->
<RollingFile name="INFO_LOG" fileName="/var/log/tomcat/my-project-backoffice-info.log"
filePattern="/var/log/tomcat/my-project-backoffice-info.log.%d{yyyy-MM-dd}.gz">
<ThresholdFilter level="INFO"/>
<PatternLayout >
<pattern>%d{ISO8601} %-5p (%x) [%t] %c{1} - %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<!-- Warn Logger -->
<RollingFile name="WARN_LOG" fileName="/var/log/tomcat/my-project-backoffice-warn.log"
filePattern="/var/log/tomcat/my-project-backoffice-warn.log.%d{yyyy-MM-dd}.gz">
<ThresholdFilter level="WARN"/>
<PatternLayout >
<pattern>%d{ISO8601} %-5p (%x) [%t] %c{1} - %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
<!-- Error Logger -->
<RollingFile name="ERROR_LOG" fileName="/var/log/tomcat/my-project-backoffice-error.log"
filePattern="/var/log/tomcat/my-project-backoffice-error.log.%d{yyyy-MM-dd}.gz">
<ThresholdFilter level="ERROR"/>
<PatternLayout >
<pattern>%d{ISO8601} %-5p (%x) [%t] %c{1} - %m%n</pattern>
</PatternLayout>
<TimeBasedTriggeringPolicy />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.springframework" level="INFO"/>
<Root level="info">
<AppenderRef ref="DEBUG_LOG"/>
<AppenderRef ref="INFO_LOG"/>
<AppenderRef ref="WARN_LOG"/>
<AppenderRef ref="ERROR_LOG"/>
</Root>
</Loggers>
Thanks.
Update: The Solution:
One of older log4j dependency (transitive) was being loaded on Stage machine. Which is weird because the package got generated by Jenkins worked fine on Dev and QA machines. So I made sure to remove all transitive dependencies and it worked fine after that. These are the transitive dependencies I deleted.
commons-logging-1.1.1.jar
log4j-slf4j-impl-2.11.2.jar
log4j-1.2.17.jar

how to include xml in log-back.xml

I am writing a custom spring boot starter project for logging. My logback-spring.xml become large and want to put the appenders in separate file (console-appender.xml and file-appender.xml) and want to include in the logback-spring.xml file like below. I place all the three xmls in src/main/resources folder in my custom starter project.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="JSONLOGSENABLED" source="spring.logs.json.enabled" defaultValue="true" />
<springProperty scope="context" name="JSONLOGSAPPENDER" source="spring.logs.json.appender" defaultValue="console" />
<if condition='property("JSONLOGSENABLED").equalsIgnoreCase("true")'>
<then>
<include file="{path to file}/console-appender.xml"/>
<include file="{path to file}/file-appender.xml"/>
<root level="INFO">
<if condition='property("JSONLOGSAPPENDER").equalsIgnoreCase("file")'>
<then>
<appender-ref ref="FILEJSON" />
</then>
<else>
<appender-ref ref="CONSOLEJSON" />
</else>
</if>
</root>
</then>
</if>
<logger
name="org.springframework.web.filter.CommonsRequestLoggingFilter">
<level value="DEBUG" />
</logger>
</configuration>
file-appender.xml
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>testFile.log</file>
<append>true</append>
<!-- set immediateFlush to false for much higher logging throughput -->
<immediateFlush>true</immediateFlush>
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
But when use this starter in my spring boot application, it is not able to resolve the console and file appender files.
I have tried putting following paths :
<include file="/src/main/resources/console-appender.xml"/>
<include file="./console-appender.xml"/>
<include resource="console-appender.xml"/>
How to include files properly in this case ?
Yes it's possible. Logback uses Joran so you have to follow the Joran rules.
For more info see documentation.
Anyway this is the preferred way to do what you're looking for.
For instance I define two files, then you adapt it as you prefer.
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="console-appender.xml"/>
<root level="debug">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
console-appender.xml
<included>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{"yyyy-MM-dd HH:mm:ss.SSS", Europe/Rome} [%mdc{id}] [%-11thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
</included>
Please note the <included> tag is mandatory

log4j2 not writing to log file specified in xml file

I'm running into an issue with log4j2 on my springboot api. We're using ubuntu on an EC2 instance and can't get the log4j2.xml to create a log file where we want it to, nor write to a log file that we manually create in that location.
We've checked the xml file multiple times, and as far as we can tell the issue isn't with the file itself. We've also messed around with permissions for writability of the log file and we don't think that's the problem either; we're both super new to linux though so we could be wrong.
<Configuration>
<Properties>
<Properties>
<Property name="filename">log/api.log</Property>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property>
</Properties>
</Properties>
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<File name="MyFile" fileName="log/api.log">
<PatternLayout
pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</File>
<RollingFile name="appLog"
fileName="api.log"
filePattern="application-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<SizeBasedTriggeringPolicy size="19500KB" />
</Policies>
<DefaultRolloverStrategy max="1" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="org.hibernate.SQL" level="debug" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="MyFile"/>
<AppenderRef ref="appLog"/>
</Logger>
<Logger name="org.hibernate.type.descriptor.sql" level="trace" additivity="false">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="MyFile"/>
<AppenderRef ref="appLog"/>
</Logger>
<Root level="info">
<AppenderRef ref="ConsoleAppender"/>
<AppenderRef ref="MyFile"/>
<AppenderRef ref="appLog"/>
</Root>
</Loggers>
</Configuration>
Any thoughts?
You have two files specified; log/api.log and api.log. Log4j will create these in whatever the current working directory is when the application is started. Since you haven't stated where the files are actually being created not much more can be said.

Maven log4j configuration under IntelliJ Idea

I want to configure Maven's logging under IntelliJ 14.0.3.
I am using Maven 3.2.3. After reading a Maven Logging 3.1.x I configured log4j2 to highlight console output. This is the configuration:
<configuration>
<properties>
<property name="maven.logging.root.level">INFO</property>
</properties>
<appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="%highlight{[%p{WARN=WARNING}]} %msg%n%throwable" />
</Console>
</appenders>
<loggers>
<root level="${sys:maven.logging.root.level}">
<appender-ref ref="console"/>
</root>
</loggers>
</configuration>
When I run it under cmd everything is ok.
IntelliJ unfortunately ignores log4j settings regarding the PatternLayout. Maybe am using the wrong appender?!

Can't get a RollingFile working with log4j2

I'm trying to configure log4j2 to write a rolling log file to disk, but I can't get it to work. No log file appears at the given path and the Glassfish server.log doesn't show any Spring logging at all. I've read a lot of similar questions on SO, but none of the proposed solutions have worked in this case. Can anyone help me? I'm using Spring 3.0 on a Glassfish 3.1 application server.
From my pom.xml:
<properties>
<junit.version>4.11</junit.version>
<tiles.version>3.0.3</tiles.version>
<slf4j.version>1.7.5</slf4j.version>
<log4j.version>2.0-beta9</log4j.version>
</properties>
<dependencies>
<dependency>
<artifactId>jcl-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
And here's my log4j.xml:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Properties>
<Property name="fileName" value="C:/temp/rolling-file.log"/>
<Property name="fileNamePattern" value="C:/temp/rolling-file-$d{dd-MM-yyyy}-%i.log"/>
<Property name="logPattern" value="%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c - %m%n"/>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${logPattern}"/>
</Console>
<RollingFile name="RollingFile" fileName="${fileName}" filePattern="${fileNamePattern}">
<PatternLayout pattern="${logPattern}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="20MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="my.root.package" level="info" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Logger>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Besides the excellent point made by M. Deinum, it turns out my log4j2.xml needed to be changed to the following:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="fileName">C:/temp/rolling-file.log</Property>
<Property name="fileNamePattern">C:/temp/rolling-file-%d{dd-MM-yyyy}-%i.log</Property>
<Property name="logPattern">%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c - %m%n</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${logPattern}"/>
</Console>
<RollingFile name="MyRollingFile" fileName="${fileName}" filePattern="${fileNamePattern}">
<PatternLayout pattern="${logPattern}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="20MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="my.root.package" level="info" additivity="false">
<AppenderRef ref="Console"/>
<AppenderRef ref="MyRollingFile"/>
</Logger>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Performed changes:
The value attributes of the Property elements were not parsed correctly. Specifying them as body of the Property element seems to solve this.
There was a mistake in the value of the fileNamePattern property. I used $d to denote the date, but it should be %d.
Apparently log4j2 doesn't like a RollingFile element with the name "RollingFile". After I changed it, logging started working.
You might need to include log4j-api to the pom dependencies (http://logging.apache.org/log4j/2.x/faq.html#which_jars ).
About the properties: if the problem goes away by putting the values in the config directly:
<RollingFile name="RollingFile" fileName="C:/temp/rolling-file.log"
filePattern="C:/temp/rolling-file-$d{dd-MM-yyyy}-%i.log">
<PatternLayout pattern="%d{dd-MM-yyyy HH:mm:ss,SSS} [%t] %-5p %c - %m%n"/>
then you may have found a bug. In that case, could your raise this issue in the log4j2 Jira issue tracker?

Resources