I'm trying to set up Logback with my Web Application. My logback.xml file looks like below:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds">
<appender name="tests_file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>tests.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>tests.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<appender name="application_file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>application.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>application.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>100MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="tests" level="DEBUG">
<appender-ref ref="tests_file" />
</logger>
<logger name="application" level="INFO">
<appender-ref ref="application_file" />
</logger>
</configuration>
This file is placed in WEB-INF/classes.
I have a servlet with the following constructor:
public class FrontController extends HttpServlet {
private Logger logger;
public FrontController() {
// Initialise the logger
logger = (Logger)LoggerFactory.getLogger("application");
logger.info("Starting application");
}
}
When I start up the servlet within Tomcat, nothing gets written to the application.log file that I've declared in logback.xml. Is the LoggerFactory just not finding the xml file or is my xml file wrong?
Thanks
Well, as it turns on, relative paths are bad. So instead of using simple file name in the file entry, i used the line:
<file>${user.dir}/logs/application.log</file>
Now the servlet logging writes to my eclipse installation folder under ../logs/application.log. All is well.
Related
Is is possible to create logs at multiple locations using logback-spring.xml ?
I tried doing that but as soon as I give two locations it creates logs at default location .
Below is the mentioned logback-spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<property name="springAppName" value="app-name"/>
<springProperty name="maxLogHistoryInDays" source="log.maxLogHistoryInDays"/>
<springProperty name="logPath" source="log.path"/>
<appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%d{dd MMM yyyy ;HH:mm:ss.SSS} %highlight(%level) [%thread] %property{HOSTNAME} ${springAppName:-} [%X{X-B3-TraceId}]
%logger{0}.%M\(%line\) - %msg%n
</pattern>
</encoder>
</appender>
<appender name="fileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${logPath}/${springAppName}/application.log</file>
<file>application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>application_%d{yyyy-MM-dd}_%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>${maxLogHistoryInDays}</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{dd MMM yyyy ;HH:mm:ss.SSS} %level [%thread] %property{HOSTNAME} ${springAppName:-} [%X{X-B3-TraceId}]
%logger{0}.%M\(%line\) - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppender" />
</root>
</configuration>
Here the logs are getting created at default location when two paths are given . And when only path is mentioned i.e., the first one it is working fine , logs are getting created at custom location.
Its not possible to provide multiple file paths in same appender block but You can do that by adding multiple appender blocks in your config file like -
<appender name="fileAppender" ............>
<appender name="fileAppender1" ............>
.
.
<root level="INFO">
<appender-ref ref="consoleAppender" />
<appender-ref ref="fileAppender" />
<appender-ref ref="fileAppender1" />
</root>
you can provide different file path in different appender blocks. While doing se make sure there is no collision in file. log file name and rolling file name must be different in both.
<appender name="fileAppender"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./application.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>application_%d{yyyy-MM-dd}_%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{dd MMM yyyy ;HH:mm:ss.SSS} %level [%thread] %property{HOSTNAME} ${springAppName:-} [%X{X-B3-TraceId}]
%logger{0}.%M\(%line\) - %msg%n</pattern>
</encoder>
</appender>
<appender name="fileAppender1"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>./application1.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>application1_%d{yyyy-MM-dd}_%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>1</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{dd MMM yyyy ;HH:mm:ss.SSS} %level [%thread] %property{HOSTNAME} ${springAppName:-} [%X{X-B3-TraceId}]
%logger{0}.%M\(%line\) - %msg%n</pattern>
</encoder>
</appender>
I am using SizeAndTimeBasedRollingPolicy of logback to create log files based on time and size. But log files are not creating in specified folder.
But when I used FixedWindowRollingPolicy or TimeBasedRollingPolicy, it is working as expected and log files are getting created in specified folder.
Spring boot version 1.2.0.RELEASE used in my application.
Below is my logback.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<property name="LOG_PATH" value="logs"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>utf-8</charset>
<Pattern>%d{yyyy-MMM-dd HH:mm:ss} [%thread] %-5p %c:%L - %m%n</Pattern>
</encoder>
</appender>
<appender name="SAVE-TO-FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/log.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{yyyy-MMM-dd HH:mm:ss} [%thread] %-5p %c:%L - %m%n</Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/archived/log_%d{dd-MM-yyyy}_%i.log</fileNamePattern>
<maxFileSize>10MB</maxFileSize>
<maxHistory>10</maxHistory>
<totalSizeCap>100MB</totalSizeCap>
</rollingPolicy>
</appender>
<root level="${logback.loglevel}">
<appender-ref ref="CONSOLE" />
<appender-ref ref="SAVE-TO-FILE" />;
</root>
</configuration>
Thanks in advance for any help.
I am using SLF4j and logback to log messages. It works fine and writes to configured file. But when I build a executable jar and try to run from prompt as java -jar executableFile. It neither writes to configured file, also displays tons of debug messages on console.
this my logback.xml:
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<!-- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%date{ISO8601} %-5level %logger{36} %X{sourceThread} - %msg%n</pattern>
</encoder>
</appender>-->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/LogWriter/log-test.log </file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/var/log/LogWriter/log-test.log.%i.log.zip</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>10MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date{ISO8601} %-5level %logger{36} %X{sourceThread} - %msg%n</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
The reason is that your main class is not finding logback.xml, so logback uses the default appender - the console in this case. You need to make sure that when packing the jar, the logback.xml is in the correct place.
I have the following logback.xml file:
<configuration>
<!--Daily rolling file appender -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>/usr/share/tomcat6/logs/api.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>/usr/share/tomcat6/logs/api/api.%d{yyyy-MM-dd}.gz</FileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
</encoder>
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="FILE" />
<appender-ref ref="STDOUT" />
</root>
</configuration>
My log file is working just fine. The folling file aspect however is not. Instead of gzipping the file and moving it into the api folder, it is putting it in the same directory and renaming it to
api.log(string of numbers).tmp
e.g.
api.log849916939395200.tmp
Does anyone know why this is happening?
Just remove the file tag from appender. Use something like this,
<appender name="contentDeliveryLogAppender" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- daily rollover -->
<fileNamePattern>${ICEX_HOME}/logs/content-delivery.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- keep 1 days' worth of history -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d [%thread] %-5level %logger{36} H:${HOSTNAME} - SC:%X{optionalParam} %msg%n</pattern>
</encoder>
</appender>
This is working for me as recommended by documentation of logback here
I had the similar issue. To fix this issue change the pattern to
/usr/share/tomcat6/logs/api/api.%d{yyyy-MM-dd}.%i.gz.
You missed the %i in the end.
This is my logback configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<contextName>xpofacebook</contextName>
<appender name="ERROR"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>log/xpofacebook-error.log</File>
<Append>true</Append>
<BufferedIO>false</BufferedIO>
<ImmediateFlush>true</ImmediateFlush>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</Pattern>
</layout>
<rollingPolicy
class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>log/error.log.%i.zip</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>3</MaxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>5MB</MaxFileSize>
</triggeringPolicy>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
</appender>
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>log/xpofacebook-application.log</File>
<Append>true</Append>
<BufferedIO>false</BufferedIO>
<ImmediateFlush>true</ImmediateFlush>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%date{ISO8601} [%-5level] %logger{35} - %msg%n</Pattern>
</layout>
<rollingPolicy
class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern>log/xpofacebook-application.log.%i.zip</FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>3</MaxIndex>
</rollingPolicy>
<triggeringPolicy
class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>5MB</MaxFileSize>
</triggeringPolicy>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="ERROR" />
<appender-ref ref="FILE" />
</root>
</configuration>
so far it works great
i need to add the following code to each class that i want to log messages in:
private static Logger log = Red5LoggerFactory.getLogger(ClassName.class, "xpofacebook");
i want the main log file xpofacebook-application.log to log messages outside of the classes that i work with.
If a flex client is connected to the red5 server and is trying to invoke a command that does not exist, the following error message will be show:
[NioProcessor-1] org.red5.server.service.ServiceInvoker - Method
getLiveChallenges with parameters [] not found in
com.xpogames.xpofacebook.Application#55e03a61
How can i make sure that these type of error messages will be included in my log file as well?
This will take a change in the core logging functionality of Red5. Right now the stuff outside your application is controlled by a different logger because they fall within a classloader above yours. Without getting into the fun of Java classloaders here, I will simply say that a fix or workaround will eventually come.