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. examplechange
bootstrap.shlineto