Log file is created automatically by springboot - spring

I wanted to use 2 log file for my springboot project.So, I created a logback.xml which is suitable for that purpose.
<?xml version = "1.0" encoding = "UTF-8"?> <configuration>
<timestamp key="timestamp" datePattern="ddMMyyyy"/>
<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>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/student${timestamp}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>logs/student_.%d{ddMMyyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/teacher${timestamp}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss};%msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>logs/teacher_.%d{ddMMyyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
<logger name="com.training.application" level="info"
additivity="false">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</logger>
<logger name="audit-log" level="info" additivity="false">
<appender-ref ref="FILE-AUDIT" />
<appender-ref ref="STDOUT" />
</logger>
<root level="error">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
My logger variables are like that :
private static Logger LOG = LoggerFactory.getLogger(TrainingBatch.class);//studentlog
private static Logger audit = LoggerFactory.getLogger("audit-log");//teacher log
My spring-boot applicatons runs with one argument.First one is bootstrap.sh start student or ./bootstrap.sh start teacher
My problem is when I give the command bootstrap.sh start student application log info is written
student_... log file and also LOG.info etc.That is okey.It is correct.But another log file (student_...) is also created automatically.Or vica -versa.
How do we stop this situation?I want that If I give student command in command line,I want student log file should be created or If I give teachercommand in command line, teacher log file should be created.Server log info is not important for me.I wish they should not been written to any log file.
My bootstrap.sh =>
#!/bin/bash
######### PARAMs ######################################
APP_NAME="school"
JAR_NAME="school-1.0.1.jar"
#LOG_FILE="APP_NAME="school".log"
MAIN_APP_PATH="/main/microservices/training/batch/school"
JAVA_PATH="/home/main/java/jdk-13.0.2/bin/java"
############### APPLICATION MANAGEMENT PROPERTIES ###################
JARFILE="${MAIN_APP_PATH}/app/${JAR_NAME}"
#LOG="${MAIN_APP_PATH}/logs/${LOG_FILE}"
JAVA_OPT="-
Dspring.config.location
=file:${MAIN_APP_PATH}/config/application.properties"
BOOT_MAIN_LOADER="org.springframework.boot.loader.PropertiesLauncher"
PID_FILE=pid.file
HOST=localhost
PORT_FILE=port.file
RUNNING=N
SECRET_KEY=$2
############ DO NOT MODIFY ########
if [ -f $PID_FILE ]; then
PID=`cat $PID_FILE`
if [ ! -z "$PID" ] && kill -0 $PID 2>/dev/null; then
RUNNING=Y
else
rm -f $PID_FILE
fi
fi
start()
{
if [ $RUNNING == "Y" ]; then
echo "Application already started"
else
if [ ! -f "$JARFILE" ]; then
echo "ERROR: jar file not found."
echo "Please check the file location: ${JARFILE}"
else
nohup $JAVA_PATH -jar $JAVA_OPT $JARFILE $SECRET_KEY &
echo $! > $PID_FILE
echo "Application $APP_NAME starting..."
sleep 1
#tail -0f $LOG
fi
fi
}

Instead of using two file appenders use one file appender with a variable role. example
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/{sys:role}${timestamp}.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>logs/{sys:role}_.%d{ddMMyyyy}.log
</fileNamePattern>
</rollingPolicy>
</appender>
change bootstrap.sh line
nohup $JAVA_PATH -jar $JAVA_OPT $JARFILE $SECRET_KEY &
to
nohup $JAVA_PATH -Drole="$2" -jar $JAVA_OPT $JARFILE $SECRET_KEY &

Related

Logback TimeBasedRollingPolicy not saving the log with the desired name of the log file

I want to configure TimeBasedRollingPolicy in my spring boot project. I want the log file to be saved in a pattern 'mylog.%d{yyyy-MM-dd}.log'.
My logback-spring.xml file looks like the following:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p{${LOG_LEVEL_PATTERN}} %15.15t - %-80.80c{1.} %X{username} %X{ipaddress} : %msg%n</pattern>
</encoder>
</appender>
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${catalina.base}/logs/mylog.log</file>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %5p{${LOG_LEVEL_PATTERN}} %15.15t - %-80.80c{1.} %X{username} %X{ipaddress} : %msg%n</pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/mylog.%d{yyyy-MM-dd}.log</fileNamePattern>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="Console" />
<appender-ref ref="RollingFile" />
</root>
</configuration>
But it's not working, the log file is created but it is not titled mylog but the default 'localhost_access_log.2022-05-16'. Can someone please help me with this?
Declare timestamp separately and refer it inside your fileNamePattern
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<timestamp key="timestamp" datePattern="yyyy-MM-dd"/>
// place all your appenders
<appender name="RollingFile"
.
.
.
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${catalina.base}/logs/mylog.${timestamp}.log</fileNamePattern>
</rollingPolicy>
</appender>
</configuration>

Logging system failed to initialize using configuration from 'null' - Failed to create parent directories

I'm trying to add logging strategy to my project.
On local, there was no Error but while CD with github action and AWS Codedeploy,
it failed with this Error.
Logging system failed to initialize using configuration from 'null' - Failed to create parent directories
Below is deploy.sh file
#!/usr/bin/env bash
REPOSITORY=/home/ubuntu/app
CURRENT_PID=$(pgrep -fl action | grep java | awk '{print $1}')
if [ -z "$CURRENT_PID" ]; then
else
echo "> kill -15 $CURRENT_PID"
kill -15 $CURRENT_PID
sleep 5
fi
JAR_NAME=$(ls -tr $REPOSITORY/*.jar | tail -n 1)
echo "> JAR NAME: $JAR_NAME"
chmod +x $JAR_NAME
nohup java -Duser.timezone=KST -jar $JAR_NAME > $REPOSITORY/nohup.out 2>&1 &
Below is appsepc.yml file
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/app/
overwrite: yes
permissions:
- object: /
pattern : "**"
mode : 777
owner : ubuntu
group : ubuntu
hooks:
ApplicationStart:
- location: deploy.sh
timeout: 60
runas: ubuntu
on EC2, it run with Springprofile - prod
<property name="LOG_PATTERN" value="[%d{yyyy-MM-dd HH:mm:ss}:%-4relative] %red([%thread]) %highlight(%-5level) %boldWhite([%C.%M:%yellow(%L)]) - %msg%n"/>
<springProfile name="dev">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</springProfile>
<springProfile name="prod">
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>${LOG_PATTERN}</Pattern>
</layout>
</appender>
<appender name="INFO_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./logs/info.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY
</onMismatch>
</filter>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./log-backup/info/info.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<appender name="WARN_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./logs/warn.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./log-backup/warn/warn.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<appender name="ERROR_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./logs/error.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./log-backup/error/error.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>30</maxHistory>
</rollingPolicy>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT"/>
<appender-ref ref="INFO_LOG"/>
<appender-ref ref="WARN_LOG"/>
<appender-ref ref="ERROR_LOG"/>
</root>
</springProfile>
But when I run .jar File on Git Bash (root User),
It works perfectly! There was no Error and it record logs well.
I think during CodeDeploy, there's no permission so it cannot create folders and error occured. It only could run File, not creating.
So I tried to give permission to IAM ( AmazonS3FullAccess / AWSCodeDeployFullAccess / AWSCodeDeployRole )
But still not work....
I almost shaveled for 12h and cannot solve this...
How can i give permission during deploying..?

Failed to start application after deploying to tomcat

I developed an application using spring-boot.After creating the war i have deployed it to tomcat using tomcat manager app but when i try to start it it gives me error
FAIL - Application at context path [/webstore-v2] could not be started
I checked catalina.out. it is showing an error like this
06:01:51,024 |-ERROR in ch.qos.logback.core.rolling.RollingFileAppender[trace] - openFile(./logs/trace.log,true) call failed. java.io.FileNotFoundException: ./logs/trace.log (No such file or directory)
at java.io.FileNotFoundException: ./logs/trace.log (No such file or directory)
at at java.io.FileOutputStream.open0(Native Method)
at at java.io.FileOutputStream.open(FileOutputStream.java:270)
at at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at at ch.qos.logback.core.recovery.ResilientFileOutputStream.<init>(ResilientFileOutputStream.java:26)
at at ch.qos.logback.core.FileAppender.openFile(FileAppender.java:204)
at at ch.qos.logback.core.FileAppender.start(FileAppender.java:127)
at at ch.qos.logback.core.rolling.RollingFileAppender.start(RollingFileAppender.java:100)
at at ch.qos.logback.core.joran.action.AppenderAction.end(AppenderAction.java:90)
at at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:309)
at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:193)
at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:179)
at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110)
at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53)
at at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:75)
at at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:150)
at at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84)
at at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
at at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150)
at at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124)
at at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:417)
at at ch.qos.logback.classic.util.StatusViaSLF4JLoggerFactory.addStatus(StatusViaSLF4JLoggerFactory.java:32)
at at ch.qos.logback.classic.util.StatusViaSLF4JLoggerFactory.addInfo(StatusViaSLF4JLoggerFactory.java:20)
at at ch.qos.logback.classic.servlet.LogbackServletContainerInitializer.onStartup(LogbackServletContainerInitializer.java:32)
at at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5160)
at at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
at at org.apache.catalina.manager.ManagerServlet.start(ManagerServlet.java:1391)
at at org.apache.catalina.manager.HTMLManagerServlet.start(HTMLManagerServlet.java:698)
at at org.apache.catalina.manager.HTMLManagerServlet.doPost(HTMLManagerServlet.java:218)
So, i guess this can be the cause because of logback.xml.here is my logback
<configuration>
<property name="defaultPattern"
value="[%-5level] [%d{yyyy-MM-dd HH:mm:ss.SSS}] %logger{36} - %msg%n"/>
<property name="defaultPath" value="./logs"/>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${defaultPattern}</pattern>
</encoder>
</appender>
<appender name="trace" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${defaultPath}/trace.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${defaultPath}/trace-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${defaultPattern}</pattern>
</encoder>
</appender>
<appender name="http-client" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${defaultPath}/http-client.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${defaultPath}/http-client-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${defaultPattern}</pattern>
</encoder>
</appender>
<appender name="webstore-v2" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${defaultPath}/webstore-v2.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${defaultPath}/webstore-v2-%d{yyyy-MM-dd}.log</fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${defaultPattern}</pattern>
</encoder>
</appender>
<logger name="io.applova.webstore" level="trace" additivity="false">
<appender-ref ref="trace"/>
</logger>
<logger name="io.applova.webstore" level="debug" additivity="false">
<appender-ref ref="webstore-v2"/>
</logger>
<logger name="http-client" level="debug" additivity="false">
<appender-ref ref="http-client"/>
</logger>
<root level="info" additivity="false">
<appender-ref ref="stdout"/>
<appender-ref ref="trace"/>
</root>
</configuration>
logs directory can not be created i guess.What should i do?

AWS CloudWatch logging with Spring Boot

What should be the approach to store logs of multiple spring boot application(s) in cloud watch?
Sample spring-boot - logback-spring.xml configuration file is below.
<?xml version="1.0" encoding="UTF-8"?>
<property name="LOGS" value="/logs/abc/" />
<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}/abc-log.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/abc-log-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>2</maxHistory>
<totalSizeCap>10MB</totalSizeCap>
</rollingPolicy>
</appender>
<!-- LOG everything at INFO level -->
<root level="info">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</root>
<logger name="com.abc" level="trace" additivity="false">
<appender-ref ref="RollingFile" />
<appender-ref ref="Console" />
</logger>
I was able to achieve the same using Cloudwatch log agent.
Step 1 - Create awslogs.conf file to point to the log location.
[/logs/abcd/8080-abcd.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /logs/abcd/8080-abcd.log
buffer_duration = 5000
log_stream_name = {hostname}
initial_position = start_of_file
log_group_name = ABCD Group Name
[/logs/defg/8081-defg.log]
datetime_format = %Y-%m-%d %H:%M:%S
file = /logs/abcd/8081-defg.log
buffer_duration = 5000
log_stream_name = {hostname}
initial_position = start_of_file
log_group_name = DEFG Group Name
Step 2 - Install cloud watch log agent as part of UserData/Bootstrap script for ec2.
yum install wget -y
wget https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py
python ./awslogs-agent-setup.py --region $aws_region --non-interactive --configfile=/configlocation/awslogs.conf
Step 3 - Need a role for ec2 machine to create logs in CloudWatch.

How to log all requests and responses for Play Framework 2.5.x?

I want to log requests and responses for my application. I expect they should be present in access.log file. However, my access.log file is empty. I have enables access logging in configuration.
Here is my prod-logger.xml :
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${user.dir}/web/logs/application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Daily rollover with compression -->
<fileNamePattern>application-log-%d{yyyy-MM-dd}.gz</fileNamePattern>
<!-- keep 30 days worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss ZZZZ} [%level] from %logger in %thread - %message%n%xException</pattern>
</encoder>
</appender>
<appender name="ACCESS_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${user.dir}/web/logs/access.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover with compression -->
<fileNamePattern>access-log-%d{yyyy-MM-dd}.gz</fileNamePattern>
<!-- keep 1 week worth of history -->
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date{yyyy-MM-dd HH:mm:ss ZZZZ} %message%n</pattern>
<!-- this quadruples logging throughput -->
<immediateFlush>false</immediateFlush>
</encoder>
</appender>
<!-- additivity=false ensures access log data only goes to the access log -->
<logger name="access" level="INFO" additivity="false">
<appender-ref ref="ACCESS_FILE"/>
</logger>
<root level="INFO">
<appender-ref ref="FILE"/>
</root>

Resources