Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.profiler.task; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import com.seajas.search.profiler.service.profiler.ProfilerService; import com.seajas.search.utilities.logging.SearchLogger; import com.seajas.search.utilities.logging.model.Logging; /** * Logging cleanup task. * * @author Jasper van Veghel <jasper@seajas.com> */ @Component public class LoggingCleanupTask { /** * Constants. */ private static final Integer RIGHT_PAD_SPACES = 5; /** * The logger. */ @Autowired private SearchLogger logger; /** * The profiler service. */ @Autowired private ProfilerService profilerService; /** * The logging path. */ @Value("${profiler.project.logging.location}") private String loggingPath; /** * The logging retention time (in hours). */ @Value("${profiler.project.logging.retention.time}") private Integer loggingRetentionTime; /** * Perform the actual cleaning. */ public void cleanup() { logger.info("Started log cleaner job"); // Keep track of the start date for cache clean-up Calendar currentDate = Calendar.getInstance(); currentDate.add(Calendar.HOUR, -loggingRetentionTime); List<Logging> entries = profilerService.cleanLogging(currentDate.getTime()); logger.info("Moving " + entries.size() + " entries from the logging database to on-disk storage"); // Create a date-formatter SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); SimpleDateFormat fullFormatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS"); // We keep a writer per day Map<String, Writer> writerMap = new HashMap<String, Writer>(); // Write the removed logging entries to their respective files for (Logging entry : entries) { String date = formatter.format(entry.getCreationDate()); try { Writer writer = writerMap.get(date); if (writer == null) writerMap.put(date, writer = new FileWriter(loggingPath + File.separator + date + ".log", true)); writer.write(fullFormatter.format(entry.getCreationDate()) + " " + StringUtils.rightPad(entry.getLevel().toUpperCase(), RIGHT_PAD_SPACES) + " " + entry.getMessage().trim() + "\n"); } catch (IOException e) { logger.error("Could not write the logging entry to the log file for date " + date); } } // Flush and then close the writers for (Map.Entry<String, Writer> entry : writerMap.entrySet()) { try { entry.getValue().flush(); entry.getValue().close(); } catch (IOException e) { logger.error("Could not flush and close the given log file", e); } } logger.info("Finishing log cleaner job"); } }