183328d40865eaab34dcee8b6afb9be2a4dd1915
[jalview.git] / src / jalview / javascript / log4j / Logger.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.javascript.log4j;
22
23 import jalview.javascript.log4j.spi.LoggingEvent;
24
25 import java.util.Hashtable;
26 import java.util.Map;
27
28 public class Logger
29 {
30
31   private static Map<String, Logger> registry;
32
33   private String name;
34
35   private Level level;
36
37   private boolean enabled = true;
38
39   private boolean isEnabled;
40
41   private Appender appender;
42
43   private Logger(String name)
44   {
45     this.name = name;
46   }
47
48   public static Logger getLogger(String name)
49   {
50     if (registry == null)
51     {
52       registry = new Hashtable<>();
53       getLogger("root");
54     }
55     Logger logger = registry.get(name);
56     if (logger == null)
57     {
58       registry.put(name, logger = new Logger(name));
59       logger.setLevel(Level.INFO);
60     }
61     return logger;
62   }
63
64   public static Logger getRootLogger()
65   {
66     return getLogger("root");
67   }
68
69   public void setLevel(Level l)
70   {
71     this.level = l;
72   }
73
74   public void addAppender(Appender appender)
75   {
76     this.appender = appender;
77   }
78
79   public boolean isDebugEnabled()
80   {
81     return isEnabled;
82   }
83
84   public void debug(Object o)
85   {
86     debug(o, null);
87   }
88
89   public void debug(Object o, Throwable e)
90   {
91     switch (level.level)
92     {
93     case Priority.DEBUG_INT:
94       log(o, e);
95       break;
96     }
97   }
98
99   public void info(Object o)
100   {
101     info(o, null);
102   }
103
104   public void info(Object o, Throwable e)
105   {
106     switch (level.level)
107     {
108     case Priority.INFO_INT:
109     case Priority.DEBUG_INT:
110       log(o, e);
111       break;
112     }
113
114   }
115
116   public void warn(Object o)
117   {
118     warn(o, null);
119   }
120
121   public void warn(Object o, Throwable e)
122   {
123     switch (level.level)
124     {
125     case Priority.WARN_INT:
126     case Priority.INFO_INT:
127     case Priority.DEBUG_INT:
128       log(o, e);
129       break;
130     }
131
132   }
133
134   public void error(Object o)
135   {
136     error(o, null);
137   }
138
139   public void error(Object o, Throwable e)
140   {
141     switch (level.level)
142     {
143     case Priority.ERROR_INT:
144     case Priority.WARN_INT:
145     case Priority.INFO_INT:
146     case Priority.DEBUG_INT:
147       log(o, e);
148       break;
149     }
150
151   }
152
153   private void log(Object s, Throwable e)
154   {
155     switch (level.level)
156     {
157     case Priority.ERROR_INT:
158       if (appender == null)
159       {
160         System.err.println(s);
161         return;
162       }
163       break;
164     case Priority.WARN_INT:
165       if (appender == null)
166       {
167         System.err.println(s);
168         return;
169       }
170       break;
171     case Priority.INFO_INT:
172       if (appender == null)
173       {
174         System.out.println(s);
175         return;
176       }
177       break;
178     case Priority.DEBUG_INT:
179       if (appender == null)
180       {
181         System.out.println(s);
182         return;
183       }
184       break;
185     }
186     e.printStackTrace();
187     appender.append(new LoggingEvent(this, s.toString(), level));
188   }
189
190 }