KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > logging > SimpleJDKLoggingHook


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util.logging;
16
17 import org.quickserver.net.server.*;
18 import org.quickserver.net.InitServerHook;
19
20 import java.io.*;
21
22 import org.quickserver.util.logging.*;
23 import java.util.logging.*;
24
25 /**
26  * <p>SimpleLoggingHook - may be used to setup quick logging for a server. </p>
27  * This will write log using {@link SimpleTextFormatter} to
28  * <code>log\&lt;ServerName&gt;_%u%g.txt</code> with maximum of 20 rolling files,
29  * each of 1MB.
30  *
31  * <code>-Dorg.quickserver.util.logging.SimpleJDKLoggingHook.Level=FINE</code> may
32  * be used to control the logging level to file.
33  * <code>-Dorg.quickserver.util.logging.SimpleJDKLoggingHook.Count=20</code> may
34  * be used to control the number of files to use.
35  *
36  * @author Akshathkumar Shetty
37  * @since 1.4.6
38  */

39 public class SimpleJDKLoggingHook implements InitServerHook {
40     private QuickServer quickserver;
41
42     public String JavaDoc info() {
43         return "Init Server Hook to setup logging.";
44     }
45
46     public void handleInit(QuickServer quickserver) throws Exception JavaDoc {
47         Logger logger = null;
48         FileHandler txtLog = null;
49         File log = new File("./log/");
50         if(!log.canRead())
51             log.mkdir();
52         try {
53             String JavaDoc level = System.getProperty(
54                 "org.quickserver.util.logging.SimpleJDKLoggingHook.Level");
55
56             logger = Logger.getLogger("");
57             logger.setLevel(Level.FINEST);
58
59             int count = 20;
60             String JavaDoc temp = System.getProperty(
61                 "org.quickserver.util.logging.SimpleJDKLoggingHook.Count");
62             if(temp!=null) {
63                 try {
64                     count = Integer.parseInt(temp);
65                 } catch(Exception JavaDoc e) {/*Ignore*/}
66             }
67
68             txtLog = new FileHandler("log/"+quickserver.getName()+"_%u%g.txt",
69                 1024*1024, count, true);
70             txtLog.setFormatter(new SimpleTextFormatter());
71             setLevel(txtLog, level);
72             logger.addHandler(txtLog);
73
74             logger = Logger.getLogger("filesrv");
75             quickserver.setAppLogger(logger);
76         } catch(IOException e){
77             System.err.println("Could not create txtLog FileHandler : "+e);
78             throw e;
79         }
80     }
81
82     private static void setLevel(FileHandler target, String JavaDoc temp) {
83         if(temp==null) {
84             target.setLevel(Level.FINE);
85             return;
86         }
87         temp = temp.toUpperCase();
88
89         if(temp.equals("SEVERE"))
90             target.setLevel(Level.SEVERE);
91         else if(temp.equals("WARNING"))
92             target.setLevel(Level.WARNING);
93         else if(temp.equals("INFO"))
94             target.setLevel(Level.INFO);
95         else if(temp.equals("CONFIG"))
96             target.setLevel(Level.CONFIG);
97         else if(temp.equals("FINE"))
98             target.setLevel(Level.FINE);
99         else if(temp.equals("FINER"))
100             target.setLevel(Level.FINER);
101         else if(temp.equals("FINEST"))
102             target.setLevel(Level.FINEST);
103     }
104 }
105
Popular Tags