Log file not being created in linux/EC2 instance - java

Hi I have created logback.xml the log files are not created when I am deploying the app in the ec2 server.
I tested in windows/eclipse and it creates the log file in windows path. But it is not created in linux/EC2 machine.
Below is my logback.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="/home/ec2-user/logs" />
<appender name="FILE-AUDIT" class="ch.qos.logback.core.FileAppender">
<!-- class="ch.qos.logback.core.rolling.RollingFileAppender"> -->
<file>${DEV_HOME}/debug.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>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.technomedha.subscribe" level="DEBUG"
additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="DEBUG">
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
Please help me in this.

I changed the path of value="/home/ec2-user/logs" to value = "/tmp/logs" since the /tmp folder has rwx permissions in the linux box.
After changing the path it worked for me perfectly and the logs are stored in the folder.

Related

Split log file in Spring Boot Gradle App Batchwise

I am working on Spring Boot Gradle App and I am facing issues while generating the log files.
Currently my app is generating 1 big log file and my spring boot uses below logback-spring.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration>
<configuration>
<springProperty scope="context" name="app_name" source="spring.application.name"/>
<springProperty scope="context" name="environment" source="param_env" defaultValue="default_env"/>
<springProperty scope="context" name="tenant" source="param_tenant" defaultValue="default_tenant"/>
<property name="GENERIC_PATTERN"
value="[%date{ISO8601}] [%p] [%t] [%c{2}] %4L | %m%n"/>
<property name="CONSOLE_LOG_PATTERN" value="$GENERIC_PATTERN"/>
<property name="CHARSET" value="UTF-8"/>
<property name="LOG_DIR"
value="logs/${tenantname:-${tenant}}/${envname:-${environment}}/${param_gslogname:-${app_name}}"/>
<logger name="org.springframework" level="INFO"/>
<logger name="org.springframework.web" level="INFO"/>
<logger name="org.gs" level="INFO"/>
<logger name="com.gs" level="INFO"/>
<logger name="liquibase" level="FINE"/>
<logger name="org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor"
level="INFO"/>
<logger name="org.springframework.security" level="INFO"/>
<logger name="org.hibernate" level="INFO"/>
<!-- Auto configuration report-->
<logger name="org.springframework.boot.autoconfigure" level="DEBUG"/>
<logger name="org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener"
level="TRACE"/>
<springProfile name="local,component-test,component-test-security,scheduler-test">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>${CHARSET}</charset>
<pattern>${GENERIC_PATTERN}</pattern>
</encoder>
</appender>
<logger name="zuul.web.request.logger" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
</logger>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
<!-- logging configuration for NOT LOCAL profile -->
<springProfile name="dev, test, uat, preprod, prod, perf">
<appender name="FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/${param_gslogname:-${app_name}}-${HOSTNAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>
${LOG_DIR}/${param_gslogname:-${app_name}}-${HOSTNAME}.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>1GB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
<!-- keep 30 days worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<mdc/>
<pattern>
<omitEmptyFields>true</omitEmptyFields>
<pattern>
{
"app_name": "${app_name}",
"HOSTNAME":"${HOSTNAME}",
"environment":"${environment}"
}
</pattern>
</pattern>
<logLevel/>
<timestamp/>
<threadName/>
<logstashMarkers/>
<tags/>
<arguments/>
<version/>
<stackTrace/>
<loggerName/>
<threadName/>
<message/>
</providers>
</encoder>
</appender>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>${CHARSET}</charset>
<pattern>${GENERIC_PATTERN}</pattern>
</encoder>
</appender>
<appender name="CONSOLE_JSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>timestamp</timestamp>
<message>logdata</message>
<version>[ignore]</version>
<levelValue>[ignore]</levelValue>
</fieldNames>
<timeZone>UTC</timeZone>
</encoder>
</appender>
<appender name="API_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/api-${param_gslogname:-${app_name}}-${HOSTNAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>
${LOG_DIR}/api-${param_gslogname:-${app_name}}-${HOSTNAME}.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>1GB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
<!-- keep 30 days worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>[ignore]</timestamp>
<message>logdata</message>
<version>[ignore]</version>
<levelValue>[ignore]</levelValue>
</fieldNames>
<timeZone>UTC</timeZone>
</encoder>
</appender>
<appender name="APPLICATION_FILE_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/startup-${param_gslogname:-${app_name}}-${HOSTNAME}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>
${LOG_DIR}/startup-${param_gslogname:-${app_name}}-${HOSTNAME}.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>1GB</maxFileSize>
<totalSizeCap>10GB</totalSizeCap>
<!-- keep 30 days worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<fieldNames>
<timestamp>timestamp</timestamp>
<message>logdata</message>
<version>[ignore]</version>
<levelValue>[ignore]</levelValue>
</fieldNames>
<timeZone>UTC</timeZone>
</encoder>
</appender>
<logger name="api.logger" level="INFO" additivity="false">
<appender-ref ref="API_FILE_APPENDER"/>
</logger>
<logger name="application.startup.logger" level="INFO" additivity="false">
<appender-ref ref="APPLICATION_FILE_APPENDER"/>
</logger>
<!-- Added because of version 1.7.30 and we can't upgrade because our server is not supporting latest version-->
<appender name="SENTRY" class="io.sentry.logback.SentryAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
<encoder>
<pattern>${GENERIC_PATTERN}</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE_APPENDER"/>
<appender-ref ref="SENTRY"/>
</root>
</springProfile>
</configuration>
My app works in way that whenever new request comes it generates 1 batch id for 1 request and all the processing stuffs or info logs are being written to one log file called gs_log_file.
and likewise when there is another request ie the 2nd request then 2nd batch id is being generated and all the processing stuffs or info logs are being written to the same log file as mentioned in point no. 1 (gs_log_file). and so on for subsequent requests the logs are being written to the same log file (gs_log_file).
So my issue is how can I generate the batchwise log file for each request or the batch id.
e.g
gs_log_file_batchId1 for batch id 1(request1).
gs_log_file_batchId2 for batch id 2(request2).
gs_log_file_batchId3 for batch id 3(request3).
gs_log_file_batchIdn for batch id n(requestn).
my app api is like this with pathVariable batchJobId -> /process-batch-jobs/{batchId}
How can I improve my implementation so that my app will put the log info in particular log file separately according to per request or per batch.
Should I go for multi-threading, again not sure on this part.
Or Should I manually process the batch id and according to that batch id I should generate the files and then log data into that log file
Once I generate the log files like gs_log_file_batchId1 for batch id 1(request1) and gs_log_file_batchId2 for batch id 2(request2), I should be also able to read this log file via UI...so I ll search these log file from UI based on some batch id or request or some customer id, here batchid can be pathvariable or request param or some sort of filter.
Also please let me know how can I externalize the configuration so that I can supply some argument to app so that app will generate the log file smartly based on supplied argument like log file size or the no of lines in log file?
Can anyone please help me on this?

Changing logback.xml file in Jenkins without rebuilding code

I have configured logback.xml to change log level during build time using "scan" property. Using this I can change log level without rebuilding the code. I can change log level by updating logback.xml file
<?xml version="1.0" encoding="UTF-8"?><!--
For more configuration information and examples see
http://logback.qos.ch/manual/configuration.html-->
<configuration scan="true" scanPeriod="10 seconds">
<!--<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />-->
<!-- Debugging appender (duplicates the normal log, PLUS any debug messages) -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %level{5} %c{3} --- %message%n</pattern>
</encoder>
</appender>
<!---->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./log/elk-file.log</file>
<append>true</append>
<!--cleanHistoryOnStart>true</cleanHistoryOnStart-->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>./log/elk-file_%d{yyyyMMdd}-%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>200MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date{ISO8601} [%thread] %level{5} %c{3} - %message%n</pattern>
</encoder>
</appender>
<!-- Our logger writes to file, console and sends the data to Logstash -->
<logger name="ro.fortsoft.elk.testdata" level="DEBUG" additivity="false">
<appender-ref ref="STASH"/>
</logger>
<logger level="INFO" name="rollingFileLogger">
<appender-ref ref="FILE" />
</logger>
<!-- ROOT logger setup -->
<root level="DEBUG">
<appender-ref ref="CONSOLE"/>
</root>
How can I do this in Jenkins, i-e change logback.xml and change log level without rebuilding the code.

How to move Child Package logs to different log File in logback?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="appLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!--See also http://logback.qos.ch/manual/appenders.html#RollingFileAppender-->
<File>${appLogFile}</File>
<encoder>
<Pattern>%d{dd-MM-yy HH:mm:ss.SSS}\t%X{reqId}\t%thread\t%level\t%logger{36}\t%M\t%line\t%msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>${appLogFile}.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
</appender>
<appender name="REPORT-XYZ-LOG-APPENDER"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${appLogFile}.reports_xyz.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{dd-MM-yy HH:mm:ss.SSS}\t%X{reqId}\t%23thread\t%-5level\t%-38logger{36}\t%-25M\t%4line\t%msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${appLogFile}.reports_xyz.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<appender name="REPORT-LOG-APPENDER"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${appLogFile}.reports.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>
%d{dd-MM-yy HH:mm:ss.SSS}\t%X{reqId}\t%thread\t%level\t%logger{36}\t%M\t%line\t%msg%n
</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>${appLogFile}.reports.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.xyz.a.b.c" level="INFO" additivity="false">
<appender-ref ref="REPORT-XYZ-LOG-APPENDER" />
</logger>
<logger name="com.xyz" level="INFO" additivity="false">
<appender-ref ref="appLog"/>
</logger>
<root level="INFO">
<appender-ref ref="REPORT-LOG-APPENDER" />
</root>
</configuration>
I want to move the logs for package "com.xyz.a.b.c" to different file in place of where package "com.xyz" logs are going. But after using above logback.xml file, logs are still going to ${appLogFile} ( appLogFile is system Property and have absolute file path) for both parent as well as child packages. Can someone help me what i am doing wrong here ?
I want to move logs for com.xyz.a.b.c --> File1
com.xyz --> File2
I got the answer. it was my mistake only. changed the logback.xml file to move it

how to cofigure slf4j in spring-mvc for logging?

I want to use SLF4j in Spring mvc-hibernate project. i have made
logback.xml for logging
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="c:/logs" />
<appender name="FILE-AUDIT"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/debug.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>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<logger name="com.mkyong.web" level="debug"
additivity="false">
<appender-ref ref="FILE-AUDIT" />
</logger>
<root level="error">
<appender-ref ref="FILE-AUDIT" />
</root>
</configuration>
but i do not know to cofigure logback.xml in spring-mvc.
In which folder in project should i keep my logback.xml?
how do i configure logback.xml in DefaultServlet-servlet.xml
No need to do anything in Spring configuration. Just add the slf4j and logback dependencies (e.g. in pom.xml if you're using maven), keep logback.xml in the classpath (e.g. src/main/resources) and use the SLF4J API in your spring-mvc Controllers.
You can also see more details here:
https://wiki.base22.com/display/btg/How+to+setup+SLF4J+and+LOGBack+in+a+web+app+-+fast

Adding timestamp to a log file using Logback-test.xml

Currently my Spring-boot application logs to a file named: myLog.log, this is working as intended, however I would like the log file to have a timestamp at the end of it and create a new file each time it is ran.
I have tried to implement this in my logback-test.xml file shown below, but it is just giving me the filename: myLog.log without a timestamp.
How can I fix this?
Logback-test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="INFO"/>
<!-- Send debug messages to System.out -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- By default, encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>path/to/my/file/mylog.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>mylog.%i{yyyy-MM-dd_HH:mm:ss.SSS}}.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>2MB</MaxFileSize>
</triggeringPolicy>
</appender>
<logger name="com.my.package" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
<appender-ref ref="FILE" />
</logger>
<!-- By default, the level of the root level is set to DEBUG -->
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
You can define a variable like this:
<timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>
(note: don't use colons in datePattern)
Then use it directly in your appender's file element:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>path/to/my/file/mylog-${myTimestamp}.log</file>
...
</appender>
Or in a simple FileAppender:
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>path/to/my/file/mylog-${myTimestamp}.log</file>
<encoder>
<Pattern>%d{yyyy-MM-dd_HH:mm:ss.SSS} - %msg%n</Pattern>
</encoder>
</appender>

Categories

Resources