Skip to content

Instantly share code, notes, and snippets.

@73ddy
Last active January 3, 2016 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 73ddy/8422886 to your computer and use it in GitHub Desktop.
Save 73ddy/8422886 to your computer and use it in GitHub Desktop.
Tomcat as Windows Service Logs Rotation
package com.kodelog.schedulerjob;
import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Executor {
public static final Logger LOG = Logger.getLogger(Executor.class);
public static void main(String[] args) throws Exception {
new ClassPathXmlApplicationContext("spring-quartz.xml");
while(true) {
LOG.info("Some logging!");
Thread.sleep(2000);
}
}
}
# Root logger option
log4j.rootLogger=INFO, file
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=${logFile}
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
package com.kodelog.schedulerjob;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class LogRotator implements Serializable {
private static final long serialVersionUID = -1908730657317231953L;
public static final Logger LOG = Logger.getLogger(LogRotator.class);
private static final String PROP_LOG_FILE = "logFile";
private static final String DATE_FORMAT = "MM-dd-yyyy-HH-mm";
public void rotateLog() {
InputStream is = null;
Properties prop = new Properties();
try {
is = getClass().getResourceAsStream("/log4j.properties");
prop.load(is);
} catch (Exception e) {
LOG.error("Failed to find logger properties.", e);
} finally {
try {
is.close();
} catch (IOException e) {
// ignore
}
}
System.setProperty(PROP_LOG_FILE, PROP_LOG_FILE + "-" + getCurrentDate() + ".log");
LogManager.resetConfiguration();
PropertyConfigurator.configure(prop);
LOG.info("The logger properties were successfully reloaded.");
}
private String getCurrentDate() {
DateFormat df = new SimpleDateFormat(DATE_FORMAT);
return df.format(new Date());
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kodelog</groupId>
<artifactId>logrotate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<!-- QuartzJobBean in spring-context-support.jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<!-- Spring + Quartz need transaction -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<!-- Quartz framework -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>1.8.6</version>
</dependency>
<!-- Log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
</dependencies>
</project>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="logRotatorTask" class="com.kodelog.schedulerjob.LogRotator" />
<bean id="logRotatorJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="logRotatorTask" />
<property name="targetMethod" value="rotateLog" />
</bean>
<!-- Cron Trigger, run every minute -->
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="logRotatorJob" />
<property name="cronExpression" value="0 * * * * ?" />
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="jobDetails">
<list>
<ref bean="logRotatorJob" />
</list>
</property>
<property name="triggers">
<list>
<ref bean="cronTrigger" />
</list>
</property>
</bean>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment