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"
Related
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.
I have a Java server application for which I log all the general data to server.log and each individual client to its own hostname.log file. I want to put the hostname.log files in a different directory that server.log ideally in some organized fashion since there are thousands of hostname.log files.
This is the config I'm using now:
<appender name="SiftAppender" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<key>descriminatorid</key>
<defaultValue>server</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${descriminatorid}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${cmb.log.dir}/${descriminatorid}.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${cmb.log.dir}/archive/${descriminatorid}.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
<!-- keep ${maxbackupindex} days' worth of history capped at ${maxfilesize} total size -->
<maxHistory>${cmb.log.maxbackupindex}</maxHistory>
<totalSizeCap>${cmb.log.maxfilesize}</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>%date{HH:mm:ss.SSS} [%thread] %-5level %msg%n</pattern>
</encoder>
</appender>
</sift>
</appender>
This config logs everything to $cmb.log.dir. What I'd like is something like this:
Server logs: $cmb.log.dir/server.log
Client logs: $cmb.log.dir/client/${descriminatorid}.log
Or even better would be:
Client logs: $cmb.log.dir/client/${firstLetter}/${descriminatorid}.log
where ${firstLetter} is the first letter of $descriminitorid. That way the logs would be distributed in a more scalable hierarchy.
The solution I found was very simple. Since I am generating ${descriminatorid} in my java code I simple included the directory structure i wanted in that value. So instead of "descriminatorid" being "hostname" it's now "client/firstletter/hostname".
I was definitely trying to overthink this one.
We have a desktop application and recently introduce logback to the system.All the logs are going to the same place.And it's rolling nicely for single client.But if we run two client of the same application log file not rolling even after it's exceed its limit.If one client close rolling happens nicely.
<appender name="LOG-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>\log\log.txt</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>\log\old\log.%d{yyyyMMdd}_%i.txt</fileNamePattern>
<maxHistory>2</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>100KB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{yyyy.MM.dd HH:mm:ss} > [%thread] %-5level - %msg%n</Pattern>
</layout>
By default writing from multiple applications to the same file is supported (it might work for you, but you can see unexpected behavior when they both try to write at the same time).
You can solve this by enable prudent mode for your appender. This is done by setting prudent="true" as an attribute of your <appender> tag.
Note that for RollingFileAppender extra restrictions apply. For you this means you can not specify the 'file' property. Also you can not use the SizeAndTimeBasedFNATP but you have to change to a TimeBasedRollingPolicy.
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.
We have configured logback for our logging needs in our application. When we run our application on Windows machine, it works as expected and generates the log files with proper logs. However when we deployed the same runnable jar file on Ubuntu machine, it is not generating log files. Following is the code in logback.xml
<configuration>
<appender name="SIFT" class="ch.qos.logback.classic.sift.SiftingAppender">
<!-- in the absence of the class attribute, it is assumed that the
desired discriminator type is
ch.qos.logback.classic.sift.MDCBasedDiscriminator -->
<discriminator>
<key>uniqueNumber</key>
<defaultValue>unknown</defaultValue>
</discriminator>
<sift>
<appender name="FILE-${uniqueNumber}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${uniqueNumber}.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>${uniqueNumber}_%i.log</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>10000</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>5MB</MaxFileSize>
</triggeringPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d [%thread] %level %mdc %logger{35} - %msg%n</pattern>
</layout>
</appender>
</sift>
</appender>
<root level="DEBUG">
<appender-ref ref="SIFT" />
</root>
</configuration>
If anything goes wrong with the configuration logback prints out a lot of debug messages to System.out. If you do not see those, then perhaps the files ARE generated but you just don't know where.
You can explicitly enable logback printing with <configuration debug="true"> which should give you much more to work on.
If you just cannot locate the files consider using lsof to locate the full path of the open files of your application.
I had a similar problem to yours, even with a simpler logback configuration.
In my logback.xml file I used absolute paths instead of relative ones for the appender, but my configuration is Linux-only, and the machines where we deploy our Java application all share the same configuration/partitioning-scheme.
The files, at least this is what was happening to me, should be one directory up of your application directory. Lets say you are executing your JAR from PATH/my.jar, the logs should be in ../PATH.