use of org.apache.log4j.Appender in project Lucee by lucee.
the class LoggerAndSourceData method close.
public void close() {
if (_log != null) {
Appender a = _appender;
_log = null;
layout = null;
if (a != null)
a.close();
_appender = null;
}
}
use of org.apache.log4j.Appender in project Lucee by lucee.
the class Log4jUtil method getAppender.
public static final Appender getAppender(Config config, Layout layout, String name, ClassDefinition cd, Map<String, String> appenderArgs) {
if (appenderArgs == null)
appenderArgs = new HashMap<String, String>();
// Appender
Appender appender = null;
if (cd != null && cd.hasClass()) {
// Console Appender
if (ConsoleAppender.class.getName().equalsIgnoreCase(cd.getClassName())) {
// stream-type
boolean doError = false;
String st = Caster.toString(appenderArgs.get("streamtype"), null);
if (!StringUtil.isEmpty(st, true)) {
st = st.trim().toLowerCase();
if (st.equals("err") || st.equals("error"))
doError = true;
}
appenderArgs.put("streamtype", doError ? "error" : "output");
// get print writer
PrintWriter pw;
if (doError) {
if (config.getErrWriter() == null)
pw = new PrintWriter(System.err);
else
pw = config.getErrWriter();
} else {
if (config.getOutWriter() == null)
pw = new PrintWriter(System.out);
else
pw = config.getOutWriter();
}
appender = new ConsoleAppender(pw, layout);
} else if (DatasourceAppender.class.getName().equalsIgnoreCase(cd.getClassName())) {
// datasource
String dsn = Caster.toString(appenderArgs.get("datasource"), null);
if (StringUtil.isEmpty(dsn, true))
dsn = Caster.toString(appenderArgs.get("datasourceName"), null);
if (!StringUtil.isEmpty(dsn, true))
dsn = dsn.trim();
appenderArgs.put("datasource", dsn);
// username
String user = Caster.toString(appenderArgs.get("username"), null);
if (StringUtil.isEmpty(user, true))
user = Caster.toString(appenderArgs.get("user"), null);
if (!StringUtil.isEmpty(user, true))
user = user.trim();
else
user = null;
appenderArgs.put("username", user);
// password
String pass = Caster.toString(appenderArgs.get("password"), null);
if (StringUtil.isEmpty(pass, true))
pass = Caster.toString(appenderArgs.get("pass"), null);
if (!StringUtil.isEmpty(pass, true))
pass = pass.trim();
else
pass = null;
appenderArgs.put("password", pass);
try {
appender = new DatasourceAppender(config, layout, dsn, user, pass);
} catch (PageException e) {
SystemOut.printDate(e);
appender = null;
}
} else if (RollingResourceAppender.class.getName().equalsIgnoreCase(cd.getClassName())) {
// path
Resource res = null;
String path = Caster.toString(appenderArgs.get("path"), null);
if (!StringUtil.isEmpty(path, true)) {
path = path.trim();
path = ConfigWebUtil.translateOldPath(path);
res = ConfigWebUtil.getFile(config, config.getConfigDir(), path, ResourceUtil.TYPE_FILE);
if (res.isDirectory()) {
res = res.getRealResource(name + ".log");
}
}
if (res == null) {
res = ConfigWebUtil.getFile(config, config.getConfigDir(), "logs/" + name + ".log", ResourceUtil.TYPE_FILE);
}
// charset
Charset charset = CharsetUtil.toCharset(Caster.toString(appenderArgs.get("charset"), null), null);
if (charset == null) {
charset = config.getResourceCharset();
appenderArgs.put("charset", charset.name());
}
// maxfiles
int maxfiles = Caster.toIntValue(appenderArgs.get("maxfiles"), 10);
appenderArgs.put("maxfiles", Caster.toString(maxfiles));
// maxfileSize
long maxfilesize = Caster.toLongValue(appenderArgs.get("maxfilesize"), 1024 * 1024 * 10);
appenderArgs.put("maxfilesize", Caster.toString(maxfilesize));
// timeout
// timeout in seconds
int timeout = Caster.toIntValue(appenderArgs.get("timeout"), 60);
appenderArgs.put("timeout", Caster.toString(timeout));
try {
appender = new RollingResourceAppender(layout, res, charset, true, maxfilesize, maxfiles, timeout, null);
} catch (IOException e) {
SystemOut.printDate(e);
}
} else // class defintion
{
Object obj = ClassUtil.loadInstance(cd.getClazz(null), null, null);
if (obj instanceof Appender) {
appender = (Appender) obj;
AppenderSkeleton as = obj instanceof AppenderSkeleton ? (AppenderSkeleton) obj : null;
Iterator<Entry<String, String>> it = appenderArgs.entrySet().iterator();
Entry<String, String> e;
String n;
while (it.hasNext()) {
e = it.next();
n = e.getKey();
if (as != null) {
if ("threshold".equalsIgnoreCase(n)) {
Level level = Level.toLevel(e.getValue(), null);
if (level != null) {
as.setThreshold(level);
continue;
}
}
}
try {
Reflector.callSetter(obj, e.getKey(), e.getValue());
} catch (PageException e1) {
// TODO log
SystemOut.printDate(e1);
}
}
}
}
}
if (appender instanceof AppenderSkeleton) {
((AppenderSkeleton) appender).activateOptions();
} else if (appender == null) {
PrintWriter pw;
if (config.getOutWriter() == null)
pw = new PrintWriter(System.out);
else
pw = config.getOutWriter();
appender = new ConsoleAppender(pw, layout);
}
return appender;
}
use of org.apache.log4j.Appender in project cxf by apache.
the class Log4jLogger method getHandlers.
public synchronized Handler[] getHandlers() {
List<Handler> ret = new ArrayList<>();
Enumeration<?> en = log.getAllAppenders();
while (en.hasMoreElements()) {
Appender ap = (Appender) en.nextElement();
if (ap instanceof HandlerWrapper) {
ret.add(((HandlerWrapper) ap).getHandler());
}
}
return ret.toArray(new Handler[ret.size()]);
}
use of org.apache.log4j.Appender in project knime-core by knime.
the class NodeLogger method removeWriter.
/**
* Removes the previously added {@link java.io.Writer} from the logger.
*
* @param writer The Writer to remove.
*/
public static void removeWriter(final Writer writer) {
synchronized (WRITER) {
Appender o = WRITER.get(writer);
if (o != null) {
if (o != LOG_FILE_APPENDER) {
Logger.getRootLogger().removeAppender(o);
WRITER.remove(writer);
}
} else {
getLogger(NodeLogger.class).warn("Could not delete writer: " + writer);
}
}
}
use of org.apache.log4j.Appender in project knime-core by knime.
the class NodeLogger method removeWorkflowDirAppender.
/**
* Removes any extra workflow directory appender if it exists.
* @param workflowDir the directory of the workflow that should no longer be logged
*/
private void removeWorkflowDirAppender(final File workflowDir) {
if (workflowDir == null) {
// if the workflowDir is null we do not need to remove the extra log appender
return;
}
final String workflowDirPath = workflowDir.getPath();
if (workflowDirPath != null) {
synchronized (WF_APPENDER) {
final Appender appender = WF_APPENDER.remove(workflowDirPath);
if (appender != null) {
appender.close();
// Remove the appender from all open node loggers
@SuppressWarnings("unchecked") final Enumeration<Logger> allLoggers = Logger.getRootLogger().getLoggerRepository().getCurrentLoggers();
while (allLoggers.hasMoreElements()) {
allLoggers.nextElement().removeAppender(appender);
}
}
}
}
}
Aggregations