I am using spring boot and logback. I am able to log into file in my local environment with IDE (IntelliJ), but it fails to log on AWS EC2 redhat linux with a JBoss EAP 7.1. I tried to set the log file permission to 777 after it is created, but still not working. The way I start up the server is
sudo sh /home/ec2-user/jboss-eap-7.1/bin/standalone.sh
my logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--Local-->
<!--<property name="LOG_FOLDER" value="${user.dir}/log" />-->
<!--Server-->
<property name="LOG_FOLDER" value="/home/ec2-user/jboss-eap-7.1/applications/springboottest/log"/>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%d{HH:mm:ss.SSS}] THREAD=[%6.-6X{threadID}] %-5level [%30.-30logger{0}] - %msg%n</Pattern>
</layout>
</appender>
<appender name="ALL" class="ch.qos.logback.core.rolling.RollingFileAppender">
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FOLDER}/log.all.%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder>
<pattern>[%d{HH:mm:ss.SSS}] THREAD=[%6.-6X{threadID}] %-5level [%30.-30logger{0}] - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ALL"/>
</root>
</configuration>
Does anyone know why it cannot append to the file?
Any help is appreciated. Thanks
Related
I am trying to log the spring boot applications logs to a separate directory(apps-archive ie ${catalina.base}/logs/apps-archive/log.log) in the external tomcat. After I start the server the log file is empty, and there are some logs in catalina.2021-04-04.log file about application is deployed and so...
In my spring boot application I have logback-spring.xml in src/main/resources as
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>
%d [%thread] %-5level %-50logger{40} : %msg%n
</pattern>
</encoder>
</appender>
<property name="LOG_PATH" value="${catalina.base}/logs/apps-archive"/>
<appender name="SAVE-TO-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/log.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d [%thread] %-5level %-50logger{40} : %msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>
${LOG_PATH}/log_%d{dd-MM-yyyy}_%i.log
</fileNamePattern>
<maxFileSize>20MB</maxFileSize>
<maxHistory>10</maxHistory>
<totalSizeCap>400MB</totalSizeCap>
</rollingPolicy>
</appender>
<springProfile name="dev">
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="SAVE-TO-FILE"/>
</root>
<!-- Hibernate loggers -->
<logger level="DEBUG" name="org.hibernate"/>
<logger level="INFO" name="com.commerzbank.eudsgvo"/>
<logger level="DEBUG" name="org.hibernate.type.descriptor.sql"/>
<logger level="DEBUG" name="org.springframework"/>
</springProfile>
</configuration>
I have no configuration for logging in application.properties file. Can anyone please suggest me why logs file is empty in apps-archive directory on server.
The default profile spring boot apps run with is default.
Can you please change
<springProfile name="dev">
to
<springProfile name="default">
Regarding the issues with the RollingPolicy I don't have any ideas at the moment.
here is my logback-spring.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="DEBUG_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<file>./target/log/app-debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./log/target/app-debug.log-%d.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-30(%d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern>
</encoder>
</appender>
<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<file>./target/log/app-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./target/log/app-error.log-%d.gz</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-30(%d{HH:mm:ss.SSS} [%thread]) %-5level %logger{32} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="DEBUG_FILE"/>
</root>
<root level="ERROR">
<appender-ref ref="ERROR_FILE"/>
</root>
</configuration>
Now everything log correctly but I am unable to see INFO level on my console while running the app. I still have
logging:
level:
root: INFO
file:
name: ./log/app.log
in my .yml file but there is no whatsoever logging on console.
Can I write the log in file and display it on the console at the same time?
You can use ConsoleAppender in your existing logback config. Below is a small snippet for your reference,
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
And you can control the root log level and also include the reference of your appender "DEBUG_FILE" as well to print in the console in addition to the implementation which you have done to write in a file. Below snippet is set to info to match your use case,
<root level="info">
<appender-ref ref="STDOUT" />
<appender-ref ref="DEBUG_FILE" />
</root>
I am working on a 9 yo Spring application with log generation done with Logback.
Logging is working fine in development environment, but when we switch to our production environment, logs are still being written until the "Application started in ...ms". After that, nothing else will ever been written.
After some digging, I noticed that our logback is not built in our war but is set into Tomcat libs. It made me think that we could have override properties disabling our logging settings. Problem is, the developer who worked on that is not in my company anymore, so I'm basically searching blindly.
Do you think I am right with my first assumption? Where should I search for some conf files overriding my application.properties?
Here is our application.properties logging settings:
logging.config= classpath:./extranet_config/logback.xml
logging.level.com.sun.mail= trace
logging.exception-conversion-word=
logging.pattern.console=%d{HH:mm:ss.SSS} %-5level [%thread] %logger - %msg %n
logging.pattern.file=.%d{HH:mm:ss.SSS} [%thread] %-5level - %msg %n
logging.pattern.level=%5p
And the logback:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="LOGS_FOLDER" value="C://log/Extranet/logs" />
<property name="LOGS_TO_COLLECT_FOLDER" value="C://log/Extranet/logs-to-collect" />
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<encoder>
<Pattern>%d{HH:mm:ss.SSS} | %-5level | %-22thread | %-12logger | %msg %n</Pattern>
</encoder>
</appender>
<appender name="log-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>TRACE</level>
</filter>
<file>${LOGS_FOLDER}/extranet.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGS_TO_COLLECT_FOLDER}/extranet.%d{yyyyMMdd}.log
</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} | %-5level | %-22thread | %-12logger | %m %throwable{0}%n</pattern>
</encoder>
</appender>
<appender name="troubleshooting-file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<file>${LOGS_FOLDER}/extranet-troubleshooting.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${LOGS_FOLDER}/extranet-troubleshooting.%i.log
</fileNamePattern>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} | %-5level | %-22thread | %-12logger | %msg %n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="INFO"/>
<logger name="org.hibernate" level="ERROR"/>
<logger name="org.springframework.boot.web.support.ErrorPageFilter" level="OFF" />
<logger name="Application" level="DEBUG"/>
<logger name="QueryLogger" level="DEBUG"/>
<logger name="AOPLogger" level="TRACE"/>
<root>
<appender-ref ref="console" />
<appender-ref ref="log-file" />
<appender-ref ref="troubleshooting-file" />
</root>
</configuration>
Note: those two files are identical in prod and in development environment.
For some reason, our logback classpath was not read the same way on both servers, we changed them and everything is working properly now.
I use logback to print log in my SpringBoot application. When I check out the log file, I found that, all log message print twice! It is so strange .
Yeah, I have found some answer similar to my issue. But maybe they are not what I want.
here is my logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<property name="LOG_HOME" value="${user.home}/app/logs"/>
<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.SSS}-[%thread]-%-5level-%logger{50}: %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${LOG_HOME}/cloud-sync-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<!-- each file should be at most 10MB, keep 3 days worth of history, but at most 1GB -->
<maxFileSize>10MB</maxFileSize>
<maxHistory>3</maxHistory>
<totalSizeCap>1GB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
</encoder>
</appender>
<appender name ="ASYNC" class= "ch.qos.logback.classic.AsyncAppender">
<discardingThreshold >0</discardingThreshold>
<queueSize>256</queueSize>
<includeCallerData>true</includeCallerData>
<appender-ref ref ="FILE"/>
</appender>
<root level="INFO">
<appender-ref ref="Console"/>
<appender-ref ref="FILE"/>
<appender-ref ref="ASYNC"/>
</root>
</configuration>
How can I make sure log messages appear only once. Thank you.
It is because you have added ASYNC and FILE for INFO.
Please remove one of them based on your requirement.
Is it anyway to send startup logs to file, right now all logs up to the statement "Started Application in...." goes to stdout, I want all logging to file.
My logback config:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<property name="LOG_FILE" value="logs/app${PID}.log"/>
<appender name="AI-APPENDER"
class="com.microsoft.applicationinsights.logback.ApplicationInsightsAppender">
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="FILE"/>
<appender-ref ref="AI-APPENDER"/>
</root>
</configuration>
You need to configure your logging framework. Assuming you're just using the default from spring-boot then that's LogBack. So have a look at their docs or have a search, there are many useful resources (like this one)
If you add a logback.xml file with the following content to your resources folder you should get logging to both console and file (called application.log) in the same format as you now see for just console.
<configuration>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>application.log</file>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
You probably want to use a RollingFileAppender as this will allow you to create new files when the log file gets big.