/* * Copyright (C) 2021 Christian Pierre MOMON * * This file is part of Logar, simple tool to manage http log files. * * Logar is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * Logar 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with Logar. If not, see . */ package fr.devinsy.logar.app.log; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.zip.GZIPOutputStream; import org.apache.commons.io.IOUtils; import org.april.logar.util.LineIterator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import fr.devinsy.cmdexec.CmdExecException; import fr.devinsy.cmdexec.CmdExecUtils; /** * The Class LogFile. */ public final class LogFile { private static Logger logger = LoggerFactory.getLogger(LogFile.class); /** * Instantiates a new log file. */ private LogFile() { } /** * Load access log. * * @param file * the file * @return the logs * @throws IOException * Signals that an I/O exception has occurred. */ public static Logs loadAccessLog(final File file) throws IOException { Logs result; result = loadAccessLog(file, LogMode.NORMAL); return result; } /** * Load access log. * * @param file * the file * @param mode * the mode * @return the logs * @throws IOException * Signals that an I/O exception has occurred. */ public static Logs loadAccessLog(final File file, final LogMode mode) throws IOException { Logs result; result = new Logs(); LineIterator iterator = new LineIterator(file); while (iterator.hasNext()) { String line = iterator.next(); Log log = LogParser.parseAccessLog(line); if (mode == LogMode.REDUCED) { log.reduce(); } result.add(log); } // return result; } /** * Load error log. * * @param file * the file * @return the logs * @throws IOException * Signals that an I/O exception has occurred. */ public static Logs loadErrorLog(final File file) throws IOException { Logs result; result = new Logs(); LineIterator iterator = new LineIterator(file); while (iterator.hasNext()) { String line = iterator.next(); Log log = LogParser.parseErrorLog(line); result.add(log); } // return result; } /** * Load log file. * * @param file * the file * @return the logs * @throws IOException * Signals that an I/O exception has occurred. */ public static Logs loadLogFile(final File file) throws IOException { Logs result; result = loadLogFile(file, LogMode.NORMAL); // return result; } /** * Load log file. * * @param file * the file * @param mode * the mode * @return the logs * @throws IOException * Signals that an I/O exception has occurred. */ public static Logs loadLogFile(final File file, final LogMode mode) throws IOException { Logs result; if (file == null) { throw new IllegalArgumentException("Null parameter."); } else { if (file.getName().contains("access")) { result = loadAccessLog(file, mode); } else if (file.getName().contains("error")) { result = loadErrorLog(file); } else { throw new IllegalArgumentException("Bad named file (missing access or error)."); } } // return result; } /** * Save logs. * * @param target * the target * @param logs * the logs * @throws FileNotFoundException * the file not found exception * @throws IOException * Signals that an I/O exception has occurred. */ public static void saveLogs(final File target, final Logs logs) throws FileNotFoundException, IOException { PrintWriter out = null; try { if (target.getName().endsWith(".gz")) { out = new PrintWriter(new GZIPOutputStream(new FileOutputStream(target))); } else { out = new PrintWriter(new FileOutputStream(target)); } for (Log log : logs) { out.println(log.getLine()); } } finally { IOUtils.closeQuietly(out); } } /** * Sort log file. * * @param file * the file * @throws IOException * Signals that an I/O exception has occurred. */ public static void sortLogFile(final File file) throws IOException { File workFile = new File(file.getParent(), file.getName() + ".tmp"); Logs logs = loadLogFile(file, LogMode.REDUCED); logs.sortByDatetime(); saveLogs(workFile, logs); File backup = new File(file.getParentFile(), file.getName() + ".bak"); if (file.renameTo(backup)) { if (!workFile.renameTo(file)) { backup.renameTo(file); } } // Check. try { String out = CmdExecUtils.run("/bin/bash -c \"zcat " + file.getAbsolutePath() + "| sort | sha1sum \""); System.out.print(out); out = CmdExecUtils.run("/bin/bash -c \"zcat " + file.getAbsolutePath() + ".bak | sort | sha1sum \""); System.out.println(out); } catch (CmdExecException exception) { exception.printStackTrace(); } } }