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
Related
I have a simple spring boot project and I use log4j2 for logging. When I ran my app in IntelliJ I could see my log files got generated in the specified location but when I packaged it as a war file and deployed it in my local tomcat server, no logs were generated. Any idea?
==========edit============================
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">
<Properties>
<Property name="log.dir">C:\\users\myname\Desktop</Property>
<Property name="app.name">myapp</Property>
</Properties>
<Appenders>
<RollingFile name="fileLogger" fileName="${log.dir}/app-info.log" filePattern="${log.dir}/app-info-%d{yyyy-MM-dd}.log">
<PatternLayout>
<pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</pattern>
</PatternLayout>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true" />
</Policies>
</RollingFile>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Logger name="com.myapp.mypackage" level="info" additivity="true">
<appender-ref ref="fileLogger" level="info" />
</Logger>
<Root level="debug" additivity="false">
<appender-ref ref="console" />
</Root>
</Loggers>
</Configuration>
This is not exactly my log4j2 config file but mine is just as simple. I have one rolling file appender and I want to log the app info to it. If I run my app in IntelliJ, the log file will get generated and I can see the logs flowing in.
I tried C:/users/myname/Desktop too, it worked for IntelliJ but not for my local tomcat.
The same path worked for my friend, it generated log file on his desktop, but he couldn't think of any specific configurations that he had done.
I also tried setting log.dir to a bunch of nonsense, and put it in my local tomcat server, it actually ran without errors. So I think tomcat is not looking at my log4j2.xml at all?
=================update============================
Ok, I deleted CATALINA_HOME in environment variables and used another version of tomcat and it worked.
I tried tomcat 8.5, 9.0.30 and 9.0.37.
9.0.30 wouldn't run until I set the CATALINA_HOME environment variable; No logs were generated after I set it.
I deleted 9.0.30 and tried 8.5. I changed CATALINA_HOME, set it to the 8.5 folder, and no logs were generated.
I deleted 9.0.30 and used 9.0.37 instead, deleted all CATALINA_HOME variables, and it worked. Logs were generated.
It is likely that the generation path of the log files is not allowed when running in Tomcat. You should look in Catalina logs.
Ok, I deleted CATALINA_HOME in environment variables and used another version of tomcat and it worked..
Problems with missing log4j configuration are usually related to the application not finding a log4j config file where you think it should find it. You can add the java parameter -Dlog4j.debug=true to get some diagnostics from the application as it searches for a config file. You can also tell it directly to use a particular file with the option -Dlog4j.configuration=file:/<apath>/log4j.properties. Running applications within a container such as tomcat adds a further level of complexity to the searched-locations for the configuration.
I ran into a strange problem to me, and I ask your help. Judging by the fact that my search has not given definite results, I just forgot something obvious, but I can not understand what it was.
So, there is a program on Java. Logback configuration (logback.xml in "resources" path):
<configuration>
<property resource=".\app\LOG.properties" />
<appender name="STDOUT-BLACK"
...
</appender>
<appender name="STDOUT-RED" class="ch.qos.logback.core.ConsoleAppender">
...
</appender>
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${destination}</file>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${destination}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>2MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>7</maxHistory>
<totalSizeCap>10MB</totalSizeCap>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
<encoder>
<Pattern>%d{HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</encoder>
</appender>
<logger name="org.hibernate.type" level="TRACE" />
<logger name="org.hibernate.SQL" level="DEBUG" />
<root level="DEBUG">
<appender-ref ref="STDOUT-BLACK" />
<appender-ref ref="STDOUT-RED" />
<appender-ref ref="FILE" />
</root>
</configuration>
the additional configuration file is (resources\app\LOG.properties):
fileName=LOGFile.log
#destination=${DEV_HOME:-.\\logs}\\${fileName}
destination=C:\\Temp\\${fileName}
And it works fine in NetBeans.
BUT whean I make an Exe for Windows from this (using Gradle shadow and launch4j plugins) properties file seems to be ignorred. Each time I start my exe I have a file (in the same folder, as exe file) "destination_IS_UNDEFINED", where all my logs are stored correct. If I "hardcode" the file name in "logback.xml" all works as expected, but why he can't read "properties"? They are in Jar, from which Launch4j creates an exe, and I got there also some other properties Files, which program read correct.
<property resource=".\app\LOG.properties" />
The error is somewhere here... I tried different "/" as separator, but it haven't helped anyhow. What am I doung wrong?
And actually I have one more question: which is the right way to store properties otside Jar file? I mean it would be nice to have a folder, where all my properties are (just as it is in IDE), and user may change them without recompile the project (so the "*.exe" Program must "read" it from this folder instead of Jar). I'm sure it's made 1000 times by everione. Is there any sample or explanation about it anywhere (maybe I'm just looking for the wrong, because I know English is not enough, so I can not find what I need).
Thanks a lot!
EDITED:
So, the first "Problem" is solved, and it was really silly - there was just an extra point. Does anyone have any advice about the second question?
I would like to check the following line in the XML
<property resource=".\app\LOG.properties" />
Are you sure about the location of app folder?
I am facing this trouble for a long time now without accessing the debug logs of the managed Threads in my Spring boot application when run on Tomcat. All the logs appear when run on the Eclipse/STS.
In Tomcat logs, I can only see the main Tread Logs.
I am connection to a database through JDBC and this is happening in a separate thread. I tried to follow the log configuration documentation but none of them helps to get the debug logs of these threads. So I do not actually see the exact problem of what is causing the connection to fail.
Here is what I tried so far:
I tried with the following logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- <appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<Target>System.out</Target> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5p
%c{1}:%L - %m%n</pattern> </encoder> </appender> <logger name="com.biscoind"
additivity="false" level="TRACE"> <appender-ref ref="stdout" /> </logger>
<root level="debug"> <appender-ref ref="stdout" /> </root> -->
<include resource="org/springframework/boot/logging/logback/base.xml" />
<logger name="org.springframework.web" level="DEBUG" />
</configuration>
When that did not resolve the issue I removed this file and see if by default, if it logs all the treads. But it did not.
So, I added the following configurations to the application.properties
logging.level.org.springframework.web:TRACE
logging.level.org.hibernate:ERROR
Then It seemed to me that this is only logging out the above namespaces, I again added
debug=true
logging.level.org.springframework.web:DEBUG
logging.level.org.hibernate:DEBUG
Tried and it did not work.
I added my namespaces also and tried as follwing,
debug=true
logging.level.com.mydomain:DEBUG
logging.level.org.springframework.web:DEBUG
logging.level.org.hibernate:DEBUG
That did not work also, I am now confused on the what should I do with the config relative to logging to make the logs to appear for the tread executions.
Irrespective of the treads, because of the property spring.jpa.show-sql=true it logs the queries that are made.
It was not a problem with the threads at all. The application was working correctly in the development environment. The problem was in the deployment environment.
It turned out to be a Java version miss-match with the jar files and the JVM version. The jars were build using Java 8 and it was running on Java 7 JVM.
When the JMV was changed to Java-8. It worked fine. So Next time I will be more careful with the version mismatch.
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.
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.