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.
Related
My logback-spring.xml reads fine from application.properties but not from application.yml. In my project, we have been asked to only use the YAML format as this format is being used in other microservices within the same project, so I cannot add propertes file. please help me why my application.yml is not read in logback.xml
I have tried variety of ways and searched similar questions on stackoverflow but no question has correct answer**. please dont mark this as duplicate**.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<property resource ="application.yml"/>
<property name="LOGS" value="./logs" />
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable
</Pattern>
</layout>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOGS}/${spring.application.name}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d %p %C{1.} [%t] %m%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily and when the file reaches 10 MegaBytes -->
<fileNamePattern>${LOGS}/archived/spring-boot-logger-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- LOG everything at INFO level -->
<root level="info">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>
<!-- LOG "com.baeldung*" at TRACE level -->
<logger name="com.ms" level="trace" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
</configuration>
Above is my logback-spring.xml. Please refer my application.yml below:-
spring:
application:
name: Logbacking-service
You can use below in your logback file as explained in the docs here
<springProperty name = "appname" source= "spring.application.name"/>
and then use this elsewhere
<file>${LOGS}/${appname}.log</file>
I tested the exact code you used and it was indeed giving issue so the above solution should work as it work for me as well. Earlier with your code the log file name that was getting generated was "appname_IS_UNDEFINED.log", and post the above change the name was "Logbacking-service.log".
If you enable trace logging level in your logback for "org.springframework.core.env.PropertySourcesPropertyResolver" you will see the location of application.yml from where it reads the properties. This will help you understand from where the spring boot is trying to find configuration. Something like below
Searching for key 'spring.profiles.active' in PropertySource 'applicationConfig: [classpath:/application.yml]
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
I already looked up on stackoverflow but I did not find something comparable.
I use spring boot with following dependencies:
spring-boot 2.2.1 with
spring-boot-starter-logging 2.2.1
logback-core 1.2.3
logback-classic 1.2.3
I want to use a custom pattern for my log output to print my mdc.variables on console. Which works via application.properties
logging.level.com.mypackage=TRACE
logging.pattern.level=%d{yyyy-MM-dd HH:mm:ss} [%t] [myMdc:%X{myMdcVariable}] %-5level %logger{36} - %m%n
But I get double entries but I can not set the additivity property via application.properties. I have to use logback-spring.xml. Which looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.mypackage" level="TRACE" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%t] [myMdc:%X{myMdcVariable}] %-5level %logger{36} - %m%n
</Pattern>
</encoder>
</appender>
</configuration>
This solves the double entries. But the pattern is not use for the logoutput. I can have either double entries or no pattern because it is not possible to combine configuration via application.properties and logback-spring.xml. What am I doing wrong?
I finally found out what is wrong. I checked the defaults.xml and the ConsoleAppender.xml on github e.g.
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot/src/main/resources/org/springframework/boot/logging/logback/console-appender.xml
The solution was to define the constant CONSOLE_LOG_PATTERN. And to define it before importing the defaults.xml and console-appender.xml
My working config file looks now like this:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="CONSOLE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} [%t] [myMdc:%X{myMdcVariable] %-5level %logger{36} - %m%n"/>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="com.mypackage" level="TRACE" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>
${CONSOLE_LOG_PATTERN}
</pattern>
</encoder>
</appender>
</configuration>
I want to change log format which will be written to console.
I'm using spring boot, so I'm including base.xml in logback.xml.
<include resource="org/springframework/boot/logging/logback/base.xml"/>
But I realised that if I use the code above and use appender="CONSOLE" to specify log format, my log message will be duplicate.
I know that if I comment the line above out, I can stop duplication, but I must specify all the settings which has been settle in the base.xml in that case.
Could someone give me advice how should I specify log format as I use base.xml?
Here is my logbook.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="jdbc" level="OFF" />
<logger name="jdbc.sqltiming" level="WARN" />
<logger name="jdbc.sqlonly" level="INFO" />
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{0} -
%msg \(%file:%line\)%n</pattern>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>c:/tmp/app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/app.%d{yyyy-MM-dd}.log.tar.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss'Z'} - %m%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="CONSOLE" />
</root>
<logger name="FILE" additivity="false">
<level value="INFO" />
<appender-ref ref="FILE" />
</logger>
</configuration>
As of now You cant do that as a configuration. Please check the link. The feature has been requested but not yet implemented.
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?