JAL-3949 An attempt at converting to Log4j 2 -- no output achieved!
[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.FATAL_INT:
94     case Priority.ERROR_INT:
95     case Priority.WARN_INT:
96     case Priority.INFO_INT:
97     case Priority.DEBUG_INT:
98       log(o, e);
99       break;
100     }
101   }
102
103   public void info(Object o)
104   {
105     info(o, null);
106   }
107
108   public void info(Object o, Throwable e)
109   {
110     switch (level.level)
111     {
112     case Priority.FATAL_INT:
113     case Priority.ERROR_INT:
114     case Priority.WARN_INT:
115     case Priority.INFO_INT:
116       log(o, e);
117       break;
118     }
119
120   }
121
122   public void warn(Object o)
123   {
124     warn(o, null);
125   }
126
127   public void warn(Object o, Throwable e)
128   {
129     switch (level.level)
130     {
131     case Priority.FATAL_INT:
132     case Priority.ERROR_INT:
133     case Priority.WARN_INT:
134       log(o, e);
135       break;
136     }
137
138   }
139
140   public void error(Object o)
141   {
142     error(o, null);
143   }
144
145   public void error(Object o, Throwable e)
146   {
147     switch (level.level)
148     {
149     case Priority.FATAL_INT:
150     case Priority.ERROR_INT:
151       log(o, e);
152       break;
153     }
154
155   }
156
157   private void log(Object s, Throwable e)
158   {
159     switch (level.level)
160     {
161     case Priority.ERROR_INT:
162       if (appender == null)
163       {
164         System.err.println(s);
165         return;
166       }
167       break;
168     case Priority.WARN_INT:
169       if (appender == null)
170       {
171         System.err.println(s);
172         return;
173       }
174       break;
175     case Priority.INFO_INT:
176       if (appender == null)
177       {
178         System.out.println(s);
179         return;
180       }
181       break;
182     case Priority.DEBUG_INT:
183       if (appender == null)
184       {
185         System.out.println(s);
186         return;
187       }
188       break;
189     }
190     e.printStackTrace();
191     appender.append(new LoggingEvent(this, s.toString(), level));
192   }
193
194 }