X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Finternal%2Fkernel%2FNodeLogger.java;h=0b410379402af36dfaa35fe21b75b3d7c5822be5;hb=b71546ddc74d6ca49d252806aafd491c75dfe1fb;hp=045229ce1fc81eadfe7fb8bade2e317c4f99270f;hpb=4613b5b46877a9013c282daabc5692efa5692afb;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeLogger.java b/org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeLogger.java index 045229ce1..0b4103794 100644 --- a/org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeLogger.java +++ b/org.argeo.cms/src/org/argeo/cms/internal/kernel/NodeLogger.java @@ -1,21 +1,7 @@ -/* - * Copyright (C) 2007-2012 Argeo GmbH - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ package org.argeo.cms.internal.kernel; import java.io.IOException; +import java.net.URI; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; @@ -37,41 +23,32 @@ import java.util.Properties; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.log4j.AppenderSkeleton; -import org.apache.log4j.Level; -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; -import org.apache.log4j.PropertyConfigurator; -import org.apache.log4j.spi.LoggingEvent; +import org.argeo.api.cms.CmsConstants; +import org.argeo.api.cms.CmsLog; +import org.argeo.cms.ArgeoLogListener; +import org.argeo.cms.ArgeoLogger; import org.argeo.cms.CmsException; import org.argeo.cms.auth.CurrentUser; -import org.argeo.node.ArgeoLogListener; -import org.argeo.node.ArgeoLogger; -import org.argeo.node.NodeConstants; import org.argeo.osgi.useradmin.UserAdminConf; +import org.osgi.framework.Bundle; import org.osgi.framework.Constants; import org.osgi.framework.ServiceReference; import org.osgi.service.cm.ConfigurationAdmin; import org.osgi.service.log.LogEntry; +import org.osgi.service.log.LogLevel; import org.osgi.service.log.LogListener; import org.osgi.service.log.LogReaderService; -import org.osgi.service.log.LogService; /** Not meant to be used directly in standard log4j config */ class NodeLogger implements ArgeoLogger, LogListener { /** Internal debug for development purposes. */ private static Boolean debug = false; - // private final static Log log = LogFactory.getLog(NodeLogger.class); - private Boolean disabled = false; private String level = null; - private Level log4jLevel = null; - // private Layout layout; +// private Level log4jLevel = null; private Properties configuration; @@ -98,28 +75,33 @@ class NodeLogger implements ArgeoLogger, LogListener { } }; - @SuppressWarnings("unchecked") public NodeLogger(LogReaderService lrs) { - Enumeration logEntries = lrs.getLog(); - while (logEntries.hasMoreElements()) - logged(logEntries.nextElement()); - lrs.addLogListener(this); - - // configure log4j watcher - String log4jConfiguration = KernelUtils.getFrameworkProp("log4j.configuration"); - if (log4jConfiguration != null && log4jConfiguration.startsWith("file:")) { - if (log4jConfiguration.contains("..")) { - if (log4jConfiguration.startsWith("file://")) - log4jConfiguration = log4jConfiguration.substring("file://".length()); - else if (log4jConfiguration.startsWith("file:")) - log4jConfiguration = log4jConfiguration.substring("file:".length()); - } - try { - Path log4jconfigPath = Paths.get(log4jConfiguration); - Thread log4jConfWatcher = new Log4jConfWatcherThread(log4jconfigPath); - log4jConfWatcher.start(); - } catch (Exception e) { - stdErr("Badly formatted log4j configuration URI " + log4jConfiguration + ": " + e.getMessage()); + if (lrs != null) { + Enumeration logEntries = lrs.getLog(); + while (logEntries.hasMoreElements()) + logged(logEntries.nextElement()); + lrs.addLogListener(this); + + // configure log4j watcher + String log4jConfiguration = KernelUtils.getFrameworkProp("log4j.configuration"); + if (log4jConfiguration != null && log4jConfiguration.startsWith("file:")) { + if (log4jConfiguration.contains("..")) { + if (log4jConfiguration.startsWith("file://")) + log4jConfiguration = log4jConfiguration.substring("file://".length()); + else if (log4jConfiguration.startsWith("file:")) + log4jConfiguration = log4jConfiguration.substring("file:".length()); + } + try { + Path log4jconfigPath; + if (log4jConfiguration.startsWith("file:")) + log4jconfigPath = Paths.get(new URI(log4jConfiguration)); + else + log4jconfigPath = Paths.get(log4jConfiguration); + Thread log4jConfWatcher = new Log4jConfWatcherThread(log4jconfigPath); + log4jConfWatcher.start(); + } catch (Exception e) { + stdErr("Badly formatted log4j configuration URI " + log4jConfiguration + ": " + e.getMessage()); + } } } } @@ -134,7 +116,7 @@ class NodeLogger implements ArgeoLogger, LogListener { // setLayout(new PatternLayout(pattern)); appender = new AppenderImpl(); reloadConfiguration(); - Logger.getRootLogger().addAppender(appender); +// Logger.getRootLogger().addAppender(appender); logDispatcherThread = new LogDispatcherThread(); logDispatcherThread.start(); @@ -144,7 +126,7 @@ class NodeLogger implements ArgeoLogger, LogListener { } public void destroy() throws Exception { - Logger.getRootLogger().removeAppender(appender); +// Logger.getRootLogger().removeAppender(appender); allUsersListeners.clear(); for (List lst : userListeners.values()) lst.clear(); @@ -168,24 +150,33 @@ class NodeLogger implements ArgeoLogger, LogListener { // @Override public void logged(LogEntry status) { - Log pluginLog = LogFactory.getLog(status.getBundle().getSymbolicName()); - Integer severity = status.getLevel(); - if (severity == LogService.LOG_ERROR) { + CmsLog pluginLog = CmsLog.getLog(status.getBundle().getSymbolicName()); + LogLevel severity = status.getLogLevel(); + if (severity.equals(LogLevel.ERROR) && pluginLog.isErrorEnabled()) { // FIXME Fix Argeo TP if (status.getException() instanceof SignatureException) return; pluginLog.error(msg(status), status.getException()); - } else if (severity == LogService.LOG_WARNING) - pluginLog.warn(msg(status), status.getException()); - else if (severity == LogService.LOG_INFO && pluginLog.isDebugEnabled()) + } else if (severity.equals(LogLevel.WARN) && pluginLog.isWarnEnabled()) { + if (pluginLog.isTraceEnabled()) + pluginLog.warn(msg(status), status.getException()); + else + pluginLog.warn(msg(status)); + } else if (severity.equals(LogLevel.INFO) && pluginLog.isDebugEnabled()) pluginLog.debug(msg(status), status.getException()); - else if (severity == LogService.LOG_DEBUG && pluginLog.isTraceEnabled()) + else if (severity.equals(LogLevel.DEBUG) && pluginLog.isTraceEnabled()) + pluginLog.trace(msg(status), status.getException()); + else if (severity.equals(LogLevel.TRACE) && pluginLog.isTraceEnabled()) pluginLog.trace(msg(status), status.getException()); } private String msg(LogEntry status) { StringBuilder sb = new StringBuilder(); sb.append(status.getMessage()); + Bundle bundle = status.getBundle(); + if (bundle != null) { + sb.append(" '" + bundle.getSymbolicName() + "'"); + } ServiceReference sr = status.getServiceReference(); if (sr != null) { sb.append(' '); @@ -198,9 +189,9 @@ class NodeLogger implements ArgeoLogger, LogListener { } else { sb.append(arrayToString(objectClasses)); } - Object cn = sr.getProperty(NodeConstants.CN); + Object cn = sr.getProperty(CmsConstants.CN); if (cn != null) - sb.append(" " + NodeConstants.CN + ": " + cn); + sb.append(" " + CmsConstants.CN + ": " + cn); Object factoryPid = sr.getProperty(ConfigurationAdmin.SERVICE_FACTORYPID); if (factoryPid != null) sb.append(" " + ConfigurationAdmin.SERVICE_FACTORYPID + ": " + factoryPid); @@ -352,8 +343,8 @@ class NodeLogger implements ArgeoLogger, LogListener { */ protected void reloadConfiguration() { if (configuration != null) { - LogManager.resetConfiguration(); - PropertyConfigurator.configure(configuration); +// LogManager.resetConfiguration(); +// PropertyConfigurator.configure(configuration); } } @@ -365,18 +356,18 @@ class NodeLogger implements ArgeoLogger, LogListener { return; if (level != null && !level.trim().equals("")) { - if (log4jLevel == null || !log4jLevel.toString().equals(level)) - try { - log4jLevel = Level.toLevel(level); - } catch (Exception e) { - System.err.println("Log4j level could not be set for level '" + level + "', resetting it to null."); - e.printStackTrace(); - level = null; - } - - if (log4jLevel != null && !event.getLoggingEvent().getLevel().isGreaterOrEqual(log4jLevel)) { - return; - } +// if (log4jLevel == null || !log4jLevel.toString().equals(level)) +// try { +// log4jLevel = Level.toLevel(level); +// } catch (Exception e) { +// System.err.println("Log4j level could not be set for level '" + level + "', resetting it to null."); +// e.printStackTrace(); +// level = null; +// } +// +// if (log4jLevel != null && !event.getLoggingEvent().getLevel().isGreaterOrEqual(log4jLevel)) { +// return; +// } } try { @@ -403,12 +394,12 @@ class NodeLogger implements ArgeoLogger, LogListener { } protected void dispatchEvent(ArgeoLogListener logListener, LogEvent evt) { - LoggingEvent event = evt.getLoggingEvent(); - logListener.appendLog(evt.getUsername(), event.getTimeStamp(), event.getLevel().toString(), - event.getLoggerName(), event.getThreadName(), event.getMessage(), event.getThrowableStrRep()); +// LoggingEvent event = evt.getLoggingEvent(); +// logListener.appendLog(evt.getUsername(), event.getTimeStamp(), event.getLevel().toString(), +// event.getLoggerName(), event.getThreadName(), event.getMessage(), event.getThrowableStrRep()); } - private class AppenderImpl extends AppenderSkeleton { + private class AppenderImpl { //extends AppenderSkeleton { public boolean requiresLayout() { return false; } @@ -416,17 +407,17 @@ class NodeLogger implements ArgeoLogger, LogListener { public void close() { } - @Override - protected void append(LoggingEvent event) { - if (events != null) { - try { - String username = CurrentUser.getUsername(); - events.put(new LogEvent(username, event)); - } catch (InterruptedException e) { - // silent - } - } - } +// @Override +// protected void append(LoggingEvent event) { +// if (events != null) { +// try { +// String username = CurrentUser.getUsername(); +// events.put(new LogEvent(username, event)); +// } catch (InterruptedException e) { +// // silent +// } +// } +// } } @@ -474,36 +465,36 @@ class NodeLogger implements ArgeoLogger, LogListener { private class LogEvent { private final String username; - private final LoggingEvent loggingEvent; +// private final LoggingEvent loggingEvent; - public LogEvent(String username, LoggingEvent loggingEvent) { + public LogEvent(String username) { super(); this.username = username; - this.loggingEvent = loggingEvent; +// this.loggingEvent = loggingEvent; } - @Override - public int hashCode() { - return loggingEvent.hashCode(); - } - - @Override - public boolean equals(Object obj) { - return loggingEvent.equals(obj); - } - - @Override - public String toString() { - return username + "@ " + loggingEvent.toString(); - } +// @Override +// public int hashCode() { +// return loggingEvent.hashCode(); +// } +// +// @Override +// public boolean equals(Object obj) { +// return loggingEvent.equals(obj); +// } +// +// @Override +// public String toString() { +// return username + "@ " + loggingEvent.toString(); +// } public String getUsername() { return username; } - public LoggingEvent getLoggingEvent() { - return loggingEvent; - } +// public LoggingEvent getLoggingEvent() { +// return loggingEvent; +// } } @@ -531,7 +522,7 @@ class NodeLogger implements ArgeoLogger, LogListener { if (log4jConfigurationPath.equals(parentDir.resolve(changed))) { if (isInternalDebugEnabled()) debug(log4jConfigurationPath + " has changed, reloading."); - PropertyConfigurator.configure(log4jConfigurationPath.toUri().toURL()); +// PropertyConfigurator.configure(log4jConfigurationPath.toUri().toURL()); } } // reset the key