001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.util.logging;
016:
017: import org.quickserver.net.server.*;
018: import org.quickserver.net.InitServerHook;
019:
020: import java.io.*;
021:
022: import org.quickserver.util.logging.*;
023: import java.util.logging.*;
024:
025: /**
026: * <p>SimpleLoggingHook - may be used to setup quick logging for a server. </p>
027: * This will write log using {@link SimpleTextFormatter} to
028: * <code>log\<ServerName>_%u%g.txt</code> with maximum of 20 rolling files,
029: * each of 1MB.
030: *
031: * <code>-Dorg.quickserver.util.logging.SimpleJDKLoggingHook.Level=FINE</code> may
032: * be used to control the logging level to file.
033: * <code>-Dorg.quickserver.util.logging.SimpleJDKLoggingHook.Count=20</code> may
034: * be used to control the number of files to use.
035: *
036: * @author Akshathkumar Shetty
037: * @since 1.4.6
038: */
039: public class SimpleJDKLoggingHook implements InitServerHook {
040: private QuickServer quickserver;
041:
042: public String info() {
043: return "Init Server Hook to setup logging.";
044: }
045:
046: public void handleInit(QuickServer quickserver) throws Exception {
047: Logger logger = null;
048: FileHandler txtLog = null;
049: File log = new File("./log/");
050: if (!log.canRead())
051: log.mkdir();
052: try {
053: String level = System
054: .getProperty("org.quickserver.util.logging.SimpleJDKLoggingHook.Level");
055:
056: logger = Logger.getLogger("");
057: logger.setLevel(Level.FINEST);
058:
059: int count = 20;
060: String temp = System
061: .getProperty("org.quickserver.util.logging.SimpleJDKLoggingHook.Count");
062: if (temp != null) {
063: try {
064: count = Integer.parseInt(temp);
065: } catch (Exception e) {/*Ignore*/
066: }
067: }
068:
069: txtLog = new FileHandler("log/" + quickserver.getName()
070: + "_%u%g.txt", 1024 * 1024, count, true);
071: txtLog.setFormatter(new SimpleTextFormatter());
072: setLevel(txtLog, level);
073: logger.addHandler(txtLog);
074:
075: logger = Logger.getLogger("filesrv");
076: quickserver.setAppLogger(logger);
077: } catch (IOException e) {
078: System.err.println("Could not create txtLog FileHandler : "
079: + e);
080: throw e;
081: }
082: }
083:
084: private static void setLevel(FileHandler target, String temp) {
085: if (temp == null) {
086: target.setLevel(Level.FINE);
087: return;
088: }
089: temp = temp.toUpperCase();
090:
091: if (temp.equals("SEVERE"))
092: target.setLevel(Level.SEVERE);
093: else if (temp.equals("WARNING"))
094: target.setLevel(Level.WARNING);
095: else if (temp.equals("INFO"))
096: target.setLevel(Level.INFO);
097: else if (temp.equals("CONFIG"))
098: target.setLevel(Level.CONFIG);
099: else if (temp.equals("FINE"))
100: target.setLevel(Level.FINE);
101: else if (temp.equals("FINER"))
102: target.setLevel(Level.FINER);
103: else if (temp.equals("FINEST"))
104: target.setLevel(Level.FINEST);
105: }
106: }
|