Logback is not generating log files on Ubuntu - java

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.

Related

Logback log to main file and clients in a separate 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.

Logback does not write logs to a file

I have a Java-standalone REST application, which is inside Tomсat. I need to output logs to a file, I use logback. But logs are not written to the file. They are displayed in the console, although I removed the console appender. If you try on a Windows machine, everything goes fine. Trying on RedHat (does not have a GUI), the logs are not output to the file (although they should) and are output to the console (although they should not be displayed). The application works exactly, responds to queries.
Logback.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<property name="DEV_HOME" value="/var/tmp" />
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${DEV_HOME}/mytest.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${DEV_HOME}/mytest-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>2KB</maxFileSize>
<maxHistory>4</maxHistory>
<totalSizeCap>10KB</totalSizeCap>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="file" />
</root>
</configuration>
In Eclipse I do export to Runnable jar file, I throw it on a Linux machine. I run as follows:
java -jar test.jar &
Yes I had the same scenario with jboss EAP 7.0 every thing was working fine on the window machine but as soon as i was taking it to linux machine . it wont write a single word.
[application.properties for logging]
second i had move log4j.properties in Jboss bin folder. application.properties file and the same file in springboot application resources.
After that restart the server and you will found the loggs written on the specified folder

JAVA: Configure logback in standalone apps

I have a Java standalone app. with a main method, with these 2 imports:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
and
private static final Logger log = LoggerFactory.getLogger(PecadorDeLaPradera.class);
In the same folder I also have a logback.xml but I don't know how to tell the program that uses the logback.xml to config the log
I couldn't find any class similar to org.apache.log4j.PropertyConfigurator
I have this logback.xml in the same folder of the Java class I am running:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- trace, debug, info, warn, error, fatal -->
<timestamp key="myTimestamp" datePattern="yyyy-MM-dd'_'HH-mm-ss.SSS"/>
<contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
<resetJUL>true</resetJUL>
</contextListener>
<!-- To enable JMX Management -->
<jmxConfigurator/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>pecador.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>handler.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>3</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>1MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{"yyyy-MM-dd HH:mm"} [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<!-- <logger name="org.springframework.orm.jpa" level="debug" /> -->
<logger name="com.calzada.pecador" level="debug" />
<root level="info">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</configuration>
I also run the appl. with Program arguments:
-Dlogback.configurationFile=/Users/calzada/Dev/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml
and I got this error:
java.util.MissingResourceException: Can't find bundle for base name -Dlogback.configurationFile=/Users/nullpointer/Development/J2EE/eclipseWSJ2EE/myApp/src/com/calzada/pecador/logback.xml, locale en_ES
The library used is logback-classic-1.2.3.jar
Here's how logback configures itself:
Logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, logback tries to find a file called logback.groovy in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath.
If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.
If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.
You can also point Logback at a specific configuration file using the system parameter logback.configurationFile. From the docs:
You may specify the location of the default configuration file with a system property named "logback.configurationFile". The value of this property can be a URL, a resource on the class path or a path to a file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
All the above taken from the docs.
According to your question you have a logback.xml but this file is in the "same folder" as your class. Unless your class is in the root package this means that logback.xml is not in the root of the classpath so Logback will not discover it. In order for Logback to configure itself from this file you can do one of the following:
Place your logback.xml in the root of your classpath (for a Maven project this is as simple as copying logback.xml to src/main/resources)
Run your Java program with -Dlogback.configurationFile=/path/to/logback.xml

Logback-classic in Jboss and Spring

I'm working on a project where I thought I would try using logback-classic for debugging and log rotation. I'm using this in a Maven context for building and creating a .war file to be deployed in JBoss 7.1 Application Server.
I've placed a logback.xml file in the resources folder in the code and a logback-test.xml in test/resources.
The active jar I'm using is the SLF4J to print the actual debugging.
public class MyClass extends MultiActionController {
private Logger logger = LoggerFactory.getLogger(MyClass.class);
public MyClass() {
logger.debug("hello");
}
}
When I run a JUnit test on the code in Maven itself it works, but after building a .war file i don't get any debugging in STDOUT nor can I find a file created.
I know that I've removed the actual logging from STDOUT in the config file, but where is the logging going...
the logback.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/myproject.%d{yyyy-MM-dd}.log</FileNamePattern>
<MaxHistory>30</MaxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{yyMMdd HH:mm:ss.SSS} [%-5.5level] [%-25.25logger] %msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
</root>
</configuration>
The folder "logs" do I need to explicit create it in the JBoss instance or need to refer relative to it for this to work? Or is there something I missed? Do I need to put the logback.xml file in the JBoss instance?
best,
Henrik
Firstly you should ensure that the logback.xml is at the classpath, e.g. WEB-INF/classes/logback.xml as the mentioning at Chapter 3: Logback configuration.
The Chapter 4: Appenders mentions about FileAppender and RollingFileAppender properties as
file *String* The name of the file to write to. If the file does not exist, it is created. On the MS Windows platform users frequently forget to escape back slashes. For example, the value c:\temp\test.log is not likely to be interpreted properly as '\t' is an escape sequence interpreted as a single tab character (\u0009). Correct values can be specified as c:/temp/test.log or alternatively as c:\temp\test.log. The File option has no default value.
If the parent directory of the file does not exist, FileAppender will automatically create it, including any necessary but nonexistent parent directories.
The example is for logback.xml is as the following:-
<configuration>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logFile.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 30 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<root level="DEBUG">
<appender-ref ref="FILE" />
</root>
</configuration>
I hope this may help.
You need to define the file-tag (as the previous answers already said). But you also need to know how it's interpreted. Logback sees it as relative to the application it's used in. So if you only use
<file>logs/<myfile>.log</file>
logback should create a "logs"-folder in the root of your JBoss (more specific: the folder where the .sh-script you start it with is located). Check from the root of your JBoss to see if you can find your logs-folder and file from there.

Is it possible to write a log into scr/main/resources in logback?

I have a logback appender:
<appender name="logfile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/opt/loghome/recon/log.%d{MM-dd-yyyy}.log </fileNamePattern>
<maxHistory>30</maxHistory>
</rollingPolicy>
<append>false</append>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
I would like to change /opt/loghome/recon/log.%d{MM-dd-yyyy}.log.zip so that the log will be created in src/main/resources in my project. The aim of this is to then have the log on the classpath. Can this be done?
Thanks
I guess you're using maven.
Maven builds jars/wars/ears whatever
The jars don't contain src/main/resources
In fact this src/main/resources path doesn't exist once your project is compiled.
So I don't really get this idea to put your logs into src/main/resources.
I think its much more "healthy" to include your /opt/loghome/recon/ in the classpath when you're running your project.
Does it make sense?

Categories

Resources