View Javadoc

1   package cz.cuni.amis.pogamut.base.utils.logging;
2   
3   import java.util.logging.Handler;
4   import java.util.logging.LogRecord;
5   
6   /**
7    * Handler for the messages - instantiated without publisher.
8    * <p><p>
9    * Use setPublisher() or constructor with ILogPublisher parameter.
10   * <p><p>
11   * See LogPublisher and it's public static inner classes ConsolePublisher and FilePublisher.
12   *  
13   * @author Jimmy
14   *
15   */
16  public class LogHandler extends Handler implements Cloneable {
17  	
18  	public static class ConsoleLogHandler extends LogHandler {
19  		
20  		public ConsoleLogHandler() {
21  			super(new LogPublisher.ConsolePublisher());
22  		}
23  		
24  	}
25  	
26  	protected ILogPublisher publisher = null;
27  	
28  	/**
29  	 * Creates empty log handler without any publisher.
30  	 */
31  	public LogHandler() {			
32  	}
33  	
34  	@Override
35  	public int hashCode() {
36  		return 12345678;
37  	}
38  	
39  	public boolean equals(Object obj) {
40  		if (obj == null) return false;
41  		if (!(obj instanceof LogHandler)) return false;
42  		return publisher.equals(((LogHandler)obj).publisher);
43  	}
44  	
45  	/**
46  	 * Creates log handler with specific publisher.
47  	 * @param publisher
48  	 */
49  	public LogHandler(ILogPublisher publisher) {
50  		this.publisher = publisher;
51  	}
52  	
53  	/**
54  	 * Returns actual publisher of the hanlder.
55  	 * @return
56  	 */
57  	public synchronized ILogPublisher getPublisher() {
58  		return publisher;
59  	}
60  
61  	/**
62  	 * Sets new publisher to the handler.
63  	 * @param publisher
64  	 */
65  	public synchronized void setPublisher(ILogPublisher publisher) {
66  		this.publisher = publisher;
67  	}
68  	
69  	@Override
70  	public synchronized void close() throws SecurityException {
71  		ILogPublisher actualPublisher = publisher;		
72  		if (actualPublisher != null) actualPublisher.close();		
73  	}
74  
75  	@Override
76  	public synchronized void flush() {
77  		ILogPublisher actualPublisher = publisher;		
78  		if (actualPublisher != null) actualPublisher.flush();		
79  	}
80  
81  	@Override
82  	public synchronized void publish(LogRecord record) {
83  		ILogPublisher actualPublisher = publisher;		
84  		if (actualPublisher != null) actualPublisher.publish(record);
85  	}
86  
87  }