Change the time of day for rolling in logback - logback

How do I configure logback to change the time of day for it's automatic rolling? I can't find it in the manual... Here's my logback.xml snippet:
<appender name="data" class="ch.qos.logback.core.FileAppender">
<file>mylog.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>mylog.%d{yyyy-MM-dd}.log.gz</fileNamePattern>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>

This is a known unsupported feature.
[JIRA] (LOGBACK-205): rotate at an absolute time every day
Perhaps if I get time I will submit a pull request.

Try to do like this, Hope it will work for you.
<appender name="FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${LOGDIR}/filename.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<FileNamePattern>${LOGDIR}/file.%d{yyyy-MM-dd}.%i.log.gz
</FileNamePattern>
<!-- keep 30 days' worth of history -->
<MaxHistory>30</MaxHistory>
<!-- or whenever the file size reaches 10MB -->
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%date [%thread] %-5level %logger{36} - %msg%n
</Pattern>
</encoder>
The above code will compress your file on the day basis or If the log file size exceeds 10MB.
Note : I have added "%i" in filePattern, It will iterate your file name as file1,file2 etc.

Related

Does the logback.xml appender element support nested appender-ref element?

I just configured Perf4j on an app which uses Logback for logging. When I tried to setup the AsyncCoalescingStatisticsAppender I nested the appender-ref element which links on the other FileAppender.
But no messages came in to the referenced Appender.
When I was trying to debug it I found that no downstream appenders was set at all!
Then I replaced appender-ref element with a full appender declaration and the downstream appender was set.
I replaced this code
<appender name="CoalescingStatistics"
class="org.perf4j.logback.AsyncCoalescingStatisticsAppender">
<param name="TimeSlice" value="10000"/>
<appender-ref ref="filer" />
</appender>
with this:
<appender name="CoalescingStatistics" class="org.perf4j.logback.AsyncCoalescingStatisticsAppender">
<param name="TimeSlice" value="10000" />
<appender name="filer" class="ch.qos.logback.core.FileAppender">
<file>${logs.location}/perfStats.log</file>
<append>true</append>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
</appender>
Does the logback.xml appender element support nested appender-ref elements?

Under Spring Framework: WARN: WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader)

I have spent all day trying to solve the logging problem I'm having with log4j in a webapp. No matter what I do, I cannot get rid of the following:
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Just to be clear, I have read all of the articles here on Stack Overflow addressing this issue. I've read the log4j manual. I've been through a dozen different tutorials. I've tried the properties approach and the XML approach (log4j.properties and log4j.xml, respectively). Also, I have confirmed that the log4j.xml file is being read. Aside from the fact that the server tells me so during startup, I can influence the level of feedback through the .xml file. So, yes, the log4j.xml file is in the CLASSPATH.
I know I'm missing something simple and fundamental. Below are the relevant files:
LOG4J.XML (/WEB-INF):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender>
<!-- Application Loggers -->
<logger name="com.tiersoftinc.testlog">
<level value="info" />
</logger>
<!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
</logger>
<logger name="org.springframework.beans">
<level value="info" />
</logger>
<logger name="org.springframework.context">
<level value="info" />
</logger>
<logger name="org.springframework.web">
<level value="info" />
</logger>
<!-- Root Logger -->
<root>
<priority value="warn" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
and WEB.XML (/WEB-INF):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters, and the applicationContext.xml file -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/root-context.xml
/WEB-INF/spring/app-context.xml
</param-value>
</context-param>
<!-- Logging listener -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
and APP-CONTEXT.XML (/WEB-INF/spring):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- Activates various annotations to be detected in bean classes -->
<context:annotation-config />
<!-- Scans the classpath for annotated components that will be auto-registered as Spring beans.
For example #Controller and #Service. Make sure to set the correct base-package -->
<context:component-scan base-package="com.tiersoftinc.gridlab3" />
<!-- Configures the annotation-driven Spring MVC Controller programming model.
Note that, with Spring 3.0, this tag works in Servlet MVC only! -->
<mvc:annotation-driven />
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Imports datasource configuration -->
<import resource="app-context-mongo.xml"/>
</beans>
and APP-CONTEXT-MONGO.XML (/WEB-INF/spring):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<!-- Activate Spring Data MongoDB repository support -->
<mongo:repositories base-package="com.tiersoftinc.gridlab3.repository" />
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/spring/database.properties</value>
</list>
</property>
</bean>
<!-- MongoDB host -->
<mongo:mongo host="${mongo.host.name}" port="${mongo.host.port}"/>
<!-- Template for performing MongoDB operations -->
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"
c:mongo-ref="mongo" c:databaseName="${mongo.db.name}"/>
<!-- Service for initializing MongoDB with sample data using MongoTemplate -->
<bean id="initGridLab3Service" class="com.tiersoftinc.gridlab3.services.InitGridLab3Service" init-method="init"/>
</beans>
and, finally, ROOT-CONTEXT.XML (/WEB-INF/spring):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<context:annotation-config />
</beans>
What am I missing?
Thank you.
Have you tried this?
<logger name="org.springframework.web">
<level value="info" />
<appender-ref ref="console" />
</logger>
Put these lines in the beginning of your web.xml:
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:/main/resources/log4j.xml</param-value>
</context-param>

App.config issues

When i modified my app.config to include the ReflectSoftware logging infomration, Ninjatrader crashed. Can you point out where I did not configure it correctly?
?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.net>
<defaultProxy>
<proxy usesystemdefault="False"/>
</defaultProxy>
</system.net>
<configSections>
<section name="insightSettings" type="ReflectSoftware.Insight.ConfigurationHandler,ReflectSoftware.Insight"/>
</configSections>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!--
In order to recieve location information, you must ensure the layout has the parameter ${callsite} and all
its properties set accordantly.
-->
<extensions>
<add assembly="ReflectSoftware.Insight.Extensions.NLog"/>
</extensions>
<targets>
<target name="ReflectInsight"
xsi:type="ReflectInsight"
instanceName="nlogInstance1"
displayLevel="true"
displayLocation="true"
layout="${callsite:className=true:fileName=true:includeSourcePath=true:methodName=true}" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="ReflectInsight" />
</rules>
</nlog>
</configuration>
fixed by moving the xml fragment after Config sections

Hourly log4net RollingFileAppender

Is it possible to configure log4net to roll files each hour? If not - any hints on how to override RollingFileAppender with required functionality to make HourlyRollingFileAppender?
Yes: set the datePattern element's value to "yyyyMMdd-HH"
<appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logfile" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyyMMdd-HH" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
Ref: log4net Config Examples.

log4j appender threshold and category

Let me preface this question by saying I've exhausted Google, or at least what I've been trying to search for. "log4j threshold", "log4j threshold category", "log4j appender threshold category", etc. But I really don't understand the results I'm getting back from Google.
This is the full configuration I've been given. I can't figure out how to modify it to suit my needs.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<!-- ===================================================================== -->
<!-- -->
<!-- Log4j Configuration -->
<!-- -->
<!-- ===================================================================== -->
<!-- $Id: jboss-log4j.xml 62403 2007-04-18 15:26:43Z dimitris#jboss.org $ -->
<!--
| For more configuration infromation and examples see the Jakarta Log4j
| owebsite: http://jakarta.apache.org/log4j
-->
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<!-- ================================= -->
<!-- Preserve messages in a local file -->
<!-- ================================= -->
<appender name="FILE" class="org.jboss.logging.appender.DailyRollingFileAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="File" value="${jboss.server.log.dir}/server.log"/>
<param name="Append" value="false"/>
<!-- Rollover at midnight each day -->
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%c] %m%n"/>
</layout>
</appender>
<!-- ============================== -->
<!-- Append messages to the console -->
<!-- ============================== -->
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<errorHandler class="org.jboss.logging.util.OnlyOnceErrorHandler"/>
<param name="Target" value="System.out"/>
<param name="Threshold" value="DEBUG"/>
<layout class="org.apache.log4j.PatternLayout">
<!-- The default pattern: Date Priority [Category] Message\n -->
<param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c] %m%n"/>
</layout>
</appender>
<!-- ================ -->
<!-- Limit categories -->
<!-- ================ -->
<category name="com.arjuna">
<priority value="FATAL"/>
</category>
<category name="com.sun.facelets">
<priority value="ERROR"/>
</category>
<category name="jacorb">
<priority value="FATAL"/>
</category>
<category name="javax.enterprise.resource">
<priority value="WARNING"/>
</category>
<category name="javax.enterprise.resource.webcontainer.jsf">
<priority value="WARNING"/>
</category>
<category name="org.apache">
<priority value="FATAL"/>
</category>
<category name="org.hibernate">
<priority value="FATAL"/>
</category>
<category name="org.jboss">
<priority value="INFO"/>
</category>
<category name="org.jboss.ejb3.EJB3Deployer">
<priority value="WARNING" />
</category>
<category name="org.jboss.ejb3.JmxKernelAbstraction">
<priority value="WARNING" />
</category>
<category name="org.jboss.management">
<priority value="FATAL"/>
</category>
<category name="org.jboss.serial">
<priority value="FATAL"/>
</category>
<category name="org.jboss.wsf.framework">
<priority value="FATAL"/>
</category>
<category name="org.jgroups">
<priority value="FATAL"/>
</category>
<category name="org.quartz">
<priority value="FATAL" />
</category>
<!-- ======================= -->
<!-- Setup the Root category -->
<!-- ======================= -->
<root>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</log4j:configuration>
I don't understand how the appender's "threshold" level interacts with the categories. See, I only want com.foo.bar messages to show on the console. But it seems like I'm getting a lot more than that, for instance, org.jboss.wsf.framework is dumping out DEBUG messages, even though I have a category with a name that matches it and set to FATAL.
I'm certain I'm manipulating the correct config file, as jboss reports it's reloading the config after I change it. So how do I set the category/threshold levels right? What's the difference between the threshold and category?
Example output (snipped). Why does quartz show up on the console when I have it set to FATAL?
2009-06-22 00:58:37,666 INFO [org.quartz.plugins.history.LoggingJobHistoryPlugin] Job JobInitializationPlugin.JobInitializationPlugin_jobInitializer execution complete at 00:58:37 06/22/2009 and reports: null
2009-06-22 01:08:37,669 DEBUG [org.quartz.simpl.SimpleJobFactory] Producing instance of Job 'JobInitializationPlugin.JobInitializationPlugin_jobInitializer', class=org.quartz.jobs.FileScanJob
2009-06-23 15:44:17,790 INFO [org.jboss.wsf.stack.jbws.NativeServerConfig] 3.0.5.GA
2009-06-23 15:44:17,868 DEBUG [org.jboss.wsf.framework.deployment.DeploymentAspectManagerImpl] setDeploymentAspects on WSDeploymentAspectManagerEJB
2009-06-23 15:44:17,868 DEBUG [org.jboss.wsf.framework.deployment.DeploymentAspectManagerImpl] setDeploymentAspects on WSDeploymentAspectManagerEndpointAPI
To answer the specific question of why does Quartz show up on the logging, you would have to change the Quartz configuration as follows:
<category name="org.quartz" additivity="false">
<priority value="FATAL" />
</category>
The additivity attribute tells log4j to override the root setting and use this only for org.quartz.
In a previous version of the question you stated you only wanted those messages from those classes turned on, to do that you have to start with configuring the priority in the root element to fatal (or even NO) and then it will only log those packages/classes that you turn on explicitly.
To answer your question about how threshold interacts with category, basically think of it is as a publish/subscribe. The category sets what is published by the logger, the threshold sets the subscription level of the appender.
This is complicated slightly be the fact that category is not a single thing, but rather a hierarchy, so the fact that you set the publishing level on one category isn't the whole story. It may be overridden in the hierarchy, as it was in your case.
Move "<appender-ref ref="CONSOLE"/>" from <root> to <category name="com.foo.bar">.
I.e.:
<category name="com.foo.bar">
<priority value="DEBUG"/>
<appender-ref ref="CONSOLE"/>
</category>
<root>
<appender-ref ref="FILE"/>
</root>
With the config that you show the console shouldn't get any debug messages so check if any other config could be used or if some code is programatically changing the config.