How to set the server-log location in Tomcat - java

I recently started working in a Spring boot application. The application uses spring-logback for logging implementation.
The application is bundled as a war in different environments. However, the location of writing logs is different in each environment and is mentioned in a logging.properties file.
Therefore, I currently have to change the location before preparing the war every time.
How can I set the path of the logs in the Tomcat configuration so that the path is auto-picked for an environment.
Currently mu logback appender looks like this:
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logging.file}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${logging.file}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>${log.file.size}</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>${log.file.history}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>${logging.pattern.file}</pattern>
</encoder>
</appender>
All the properties here are picked from logging.properties file inside the src/main/resources

spring-boot provides several logging.* settings that can be applied in application.properties, like:
logging.level.=DEBUG
logging.file=myfile.log
logging.path=d:/logs/
If you set logging.path, Spring Boot writes spring.log to the specified directory. Names can be an exact location or relative to the current directory.

Related

store archived logback files in a completely separate path

<appender name="MAIN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_DIR}/your-app-logs.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${LOG_GZ_DIR}/your-app-logs.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<!-- keep 90 days' worth of history capped at 3GB total size -->
<maxHistory>90</maxHistory>
</rollingPolicy>
</appender>
I'm facing issue with logback where if I set the archive path same as the path of the parent it works well for example if I set
${LOG_DIR}=/someDirectory and ${LOG_GZ_DIR}=/someDirectory/archive
but when I try to set the two paths to completely different locations for example
${LOG_DIR}=/someDirectory , ${LOG_GZ_DIR}=/otherDirectory/archive
then I can that /otherDirectory/archive is created but no archive files created there.
is there a way to make this work the archive has to start as the parent path ?

Application logging with AWS Elastic Beanstalk

I have deployed a WAR to AWS Elastic Beanstalk on environment with Tomcat 8.5 with Java 8. The app loads and is working fine. However, when I go to check the logs (Environment -> Logs -> Last 100 lines OR Full Logs) I do not see the logs written by the app. There are some other logs like tomcat/activity/boot etc, but not the logs written by my application. The logs work as expected when running locally, just not on AWS.
Below is my logback.xml file:
<configuration debug="true">
<appender name="APPLOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/my_app.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>/var/log/my_app_%d{yyyy-MM-dd}_%i.log</FileNamePattern>
<maxHistory>7</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>5MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %-5level %c{1} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="LOGFILE" />
</root>
</configuration>
In one of the Medium post I learned that we need to configure the EB environment so that the application's logs are included in the logs bundle. So I put the following config file in the application's resources directory:
files:
"/opt/elasticbeanstalk/tasks/bundlelogs.d/applogs.conf" :
mode: "000755"
owner: root
group: root
content: |
/var/log/*.log
"/opt/elasticbeanstalk/tasks/taillogs.d/applogs.conf" :
mode: "000755"
owner: root
group: root
content: |
/var/log/*.log
But that did not help, there was no change in the app's log behavior.
This has been a frustrating ordeal so far, hoping for some help.
I had faced similar issues with logging when I deployed my app to AWS.
A couple of things of note:
(1) The config file is correct (that's what I used), but that needs to go into an .ebextensions folder in your webapp directory:
(2) You will need to change the location of your log file. Your app can't write to the var/log folder, but can write to the var/log/tomcat folder because of folder permissions issue.
You can find more details on my question here: AWS Elastic Beanstalk Application Logging with Logback
Hope it works out for you!

Logback : TimeBasedRollingPolicy does not change the directory sometimes

I'm using the logback in my application and following rolling file appender.
<appender name="OVERALL_SYNCHRONOUS" class="ch.qos.logback.core.rolling.RollingFileAppender">
<Append>true</Append>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- Daily rollover -->
<fileNamePattern>${logRoot}/${dirName}/application_overall.log.zip</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%d{dd.MM.yy HH:mm:ss.SSS} [%-5level] %65.65(%logger{0}.%aspectjFreeMethod\(%line\)): %X{MDC_CtrlName}%msg%n</pattern>
</encoder>
</appender>
This works fine most of the times but sometimes on the day change it zips the previous that is as expected but does not change the directory for the new log file. It creates the new file in the same folder of the previous day.
When day changes from 18.11.15 to 19.11.15 application zipped the application_overall.log as it should and create "application_overall.log.zip".
But application continues to log in the application_overall.log in the folder "2015-11-18".
No application_overall.log was created in the folder "2015-11-19" whereas all the other log files of the application were zipped in folder "2015-11-18" and a new log file is created in the current date folder i.e. "2015-11-19".
After restarting, a new log file of application_overall.log was created in the folder "2015-11-19".
EDIT:
logroot and dirName are defined as below :-
<insertFromJNDI env-entry-name="java:comp/env/log/logRoot" as="logRoot"
scope="context"/>
<insertFromJNDI env-entry-name="java:comp/env/log/dateDirNamePattern" as="datePattern" scope="context"/>
<property name="dirName" value="%d{${datePattern:-yyyy-MM-dd}}" scope="context"/>
What could be the possible reason for this behaviour and how can I solve this issue?

config play framework log file location in production

I'm using play framework in a project. In order to config the log file related information, I use logback to config it. Here is the sample configuration:
<appender name="DEBUGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/usr/frank/logs/debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover with compression -->
<fileNamePattern>debug-log-%d{yyyy-MM-dd}.gz</fileNamePattern>
<!-- keep 1 week worth of history -->
<maxHistory>7</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%date - [%level] - from %logger in %thread %message%n</pattern>
</encoder>
</appender>
When I run the test like ./activator test or run the application in dev mode ./activator run, I can find the log file get created at the place specified at the file section. But when I run the application in production mode, like ./activator start, the file will not get generated.
Can anyone tell me why?
In Production mode you have to pass the parameter logger.file as shown below.
-Dlogger.file="/usr/frank/logs/debug.log"

How to configure logback to create log file name having server name

My java EAR application runs on 2 application servers. Each write the logs locally with log file names. I want to know how to add the unique app server name to the log file name that is being written in each server. This would help me identify the files from each server when I am looking into the log files after downloading them from server and while sending to coworkers for debugging. Currently we have to put them into separate folders as both folders have files with the same names. Thank you.
Following is the snippet from logback.xml that names the file
<appender name="ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logfile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>logfile-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 10MB -->
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
If you use groovy configuration for logback, you can try
import java.net.InetAddress
println InetAddress.getLocalHost()
This will give you the local server name and IP address.

Categories

Resources