/*
* Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
* Copyright (C) $$Year-Rel$$ The Jalview Authors
*
* This file is part of Jalview.
*
* Jalview is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3
* of the License, or (at your option) any later version.
*
* Jalview is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jalview. If not, see .
* The Jalview Authors are detailed in the 'AUTHORS' file.
*/
package jalview.javascript.log4j;
import jalview.javascript.log4j.spi.LoggingEvent;
import java.util.Hashtable;
import java.util.Map;
public class Logger
{
private static Map registry;
private String name;
private Level level;
private boolean enabled = true;
private boolean isEnabled;
private Appender appender;
private Logger(String name)
{
this.name = name;
}
public static Logger getLogger(String name)
{
if (registry == null)
{
registry = new Hashtable<>();
getLogger("root");
}
Logger logger = registry.get(name);
if (logger == null)
{
registry.put(name, logger = new Logger(name));
logger.setLevel(Level.DEBUG);
}
return logger;
}
public static Logger getRootLogger()
{
return getLogger("root");
}
public void setLevel(Level l)
{
this.level = l;
}
public void addAppender(Appender appender)
{
this.appender = appender;
}
public boolean isDebugEnabled()
{
return isEnabled;
}
public void trace(Object o)
{
trace(o, null);
}
public void trace(Object o, Throwable e)
{
switch (level.level)
{
case Level.TRACE_INT:
log(o, e);
break;
}
}
public void debug(Object o)
{
debug(o, null);
}
public void debug(Object o, Throwable e)
{
switch (level.level)
{
case Priority.DEBUG_INT:
case Level.TRACE_INT:
log(o, e);
break;
}
}
public void info(Object o)
{
info(o, null);
}
public void info(Object o, Throwable e)
{
switch (level.level)
{
case Priority.INFO_INT:
case Priority.DEBUG_INT:
case Level.TRACE_INT:
log(o, e);
break;
}
}
public void warn(Object o)
{
warn(o, null);
}
public void warn(Object o, Throwable e)
{
switch (level.level)
{
case Priority.WARN_INT:
case Priority.INFO_INT:
case Priority.DEBUG_INT:
case Level.TRACE_INT:
log(o, e);
break;
}
}
public void error(Object o)
{
error(o, null);
}
public void error(Object o, Throwable e)
{
switch (level.level)
{
case Priority.ERROR_INT:
case Priority.WARN_INT:
case Priority.INFO_INT:
case Priority.DEBUG_INT:
case Level.TRACE_INT:
log(o, e);
break;
}
}
private void log(Object s, Throwable e)
{
switch (level.level)
{
case Priority.ERROR_INT:
case Priority.WARN_INT:
if (appender == null)
{
System.err.println(s);
return;
}
case Priority.INFO_INT:
case Priority.DEBUG_INT:
case Level.TRACE_INT:
if (appender == null)
{
System.out.println(s);
return;
}
break;
}
if (e != null)
{
e.printStackTrace();
}
appender.append(new LoggingEvent(this, s.toString(), level));
}
}