add logging handler
[jSite2.git] / src / net / pterodactylus / util / logging / Logging.java
1 /*
2  * jFCPlib-high-level-client - Logging.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package net.pterodactylus.util.logging;
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.logging.Handler;
26 import java.util.logging.Level;
27 import java.util.logging.LogRecord;
28 import java.util.logging.Logger;
29
30 /**
31  * Sets up logging.
32  * 
33  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
34  * @version $Id$
35  */
36 public class Logging {
37
38         /** The log handler. */
39         private static final LogHandler logHandler = new LogHandler();
40
41         /**
42          * Adds a listener to the log handler.
43          * 
44          * @param loggingListener
45          *            The listener to add
46          */
47         public static void addLoggingListener(LoggingListener loggingListener) {
48                 logHandler.addLoggingListener(loggingListener);
49         }
50
51         /**
52          * Removes a listener from the log handler.
53          * 
54          * @param loggingListener
55          *            The listener to remove
56          */
57         public static void removeLoggingListener(LoggingListener loggingListener) {
58                 logHandler.removeLoggingListener(loggingListener);
59         }
60
61         /**
62          * Sets up logging and installs the log handler.
63          */
64         public static void setup() {
65                 Logger rootLogger = Logger.getAnonymousLogger().getParent();
66                 rootLogger.addHandler(logHandler);
67                 rootLogger.setLevel(Level.ALL);
68                 logHandler.setLevel(Level.INFO);
69         }
70
71         /**
72          * The log handler simply forwards every log message it receives to all
73          * registered listeners.
74          * 
75          * @see LoggingListener
76          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
77          * @version $Id$
78          */
79         private static class LogHandler extends Handler {
80
81                 /**
82                  * Package-private constructor.
83                  */
84                 LogHandler() {
85                 }
86
87                 /** The list of the listeners. */
88                 private final List<LoggingListener> loggingListeners = Collections.synchronizedList(new ArrayList<LoggingListener>());
89
90                 //
91                 // EVENT MANAGEMENT
92                 //
93
94                 /**
95                  * Adds a listener to the log handler.
96                  * 
97                  * @param loggingListener
98                  *            The listener to add
99                  */
100                 public void addLoggingListener(LoggingListener loggingListener) {
101                         loggingListeners.add(loggingListener);
102                 }
103
104                 /**
105                  * Removes a listener from the log handler.
106                  * 
107                  * @param loggingListener
108                  *            The listener to remove
109                  */
110                 public void removeLoggingListener(LoggingListener loggingListener) {
111                         loggingListeners.remove(loggingListener);
112                 }
113
114                 /**
115                  * Notifies all listeners that a log record was received.
116                  * 
117                  * @param logRecord
118                  *            The received log record
119                  */
120                 private void fireLogged(LogRecord logRecord) {
121                         for (LoggingListener loggingListener: loggingListeners) {
122                                 loggingListener.logged(logRecord);
123                         }
124                 }
125
126                 //
127                 // INTERFACE Handler
128                 //
129
130                 /**
131                  * {@inheritDoc}
132                  */
133                 @Override
134                 public void close() throws SecurityException {
135                 }
136
137                 /**
138                  * {@inheritDoc}
139                  */
140                 @Override
141                 public void flush() {
142                 }
143
144                 /**
145                  * {@inheritDoc}
146                  */
147                 @Override
148                 public void publish(LogRecord logRecord) {
149                         fireLogged(logRecord);
150                 }
151
152         }
153
154 }