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?!
Related
I am upgrading log4j1 to logj42 in one of the springBoot porject (maven, Java 1.8) with log4j-core,log4j-api as maven dependency.
Log file mentioned in log4j2.xml as Rolling file appender is not creating log file, I am able to see the logs in console but no log file. Please reply if anyone has faced same issue.
By default spring boot contains an integration with logback logging library.
So you'll have to exclude the "default" and add a special starter that will handle the integration:
In the pom.xml make sure you have the following:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Optional: If you want to use an asynchronous logging, add also the following dependency:
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.6</version> <!-- check the actual version it might be different -->
</dependency>
After this step you can start configuring the log4j2. There are plenty of sample configurations and tutorials about this, examples: here and here.
I won't dive into possible configurations of log4j2, but from the spring boot's standpoint I'll mention that you should place the log4j2's configurations into src/main/resources/log4j2.xml
You can try with the below log4j2.xml. This is working for me.
<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config">
<Properties>
<Property name="basePath">/home/user/myapp/logs</Property>
</Properties>
<Appenders>
<!-- File Appender -->
<File name="FILE" fileName="${basePath}/my-app.log" append="true">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</File>
<!-- Console Appender -->
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%-5p | %d{yyyy-MM-dd HH:mm:ss} | [%t] %C{2} (%F:%L) - %m%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.abc.xyz" level="debug" />
<Root level="info">
<AppenderRef ref="STDOUT" />
<AppenderRef ref="FILE" />
</Root>
</Loggers>
</Configuration>
Change the log file name and path accordingly.
Also change the package for the logger. <Logger name="com.abc.xyz" level="debug" />
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
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.
I am upgrading the version of log4j for a web application running on Tomcat 8. I am upgrading from version 1.2.12 to the latest 2.11.1.
The application is using Maven. So I added the following dependencies to my application pom.xml (version is managed in parent pom):
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
I removed the old configuration file log4j.properties and replaced it by a new log4j2.xml file in the folder src/main/resources (find below my log4j2 file content).
When I am running directly a main function, the logs are correctly written in console and in a file. However when I deploy the application on Tomcat, and run it, no log is written.
I checked my application WAR, and both libraries are present in WEB-INF/lib folder.
I also tested by adding exclusions to log4j-1** to external libraries that have dependency on it, but nothing changed...
Did someone already had the same issue, and how did you correct it?
Regards,
Matthieu
PS: content of my log4j2.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status=WARN strict="true">
<Properties>
<Property name="pattern">%d [%t] (%F:%L) %x %-5p [%c{1}] %m%n</Property>
<Property name="filePath">C:/temp/logs/my_app/my_app.log</Property>
</Properties>
<Appenders>
<Appender type="Console" name="STDOUT">
<Layout type="PatternLayout" pattern="${pattern}"/>
</Appender>
<Appender type="RollingFile" name="FILE" fileName="${filePath}" filePattern="${filePath}.%i">
<Layout type="PatternLayout" pattern="${pattern}"/>
<Policies>
<SizeBasedTriggeringPolicy size="500KB"/>
</Policies>
<DefaultRolloverStrategy max="1"/>
</Appender>
</Appenders>
<Loggers>
<Logger name="my.package.debug" level="DEBUG" additivity="false">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="FILE"/>
</Logger>
<Root level="WARN">
<AppenderRef ref="STDOUT"/>
<AppenderRef ref="FILE"/>
</Root>
</Loggers>
</Configuration>
I finally found my issue, it was due to another library (infinispan-embeded) which was redefining Log4j classes, such as LogManager. I was loading thoses classes instead of those in log4j libraries, and they was in older version than what I wanted.
I just replaced the library, and logs are correctly written in file.
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?