From 8c2032f6c0d8f5abf2903bdfc8ad10ce5f865ad6 Mon Sep 17 00:00:00 2001 From: Konstantin Krivopustov Date: Mon, 14 Sep 2015 07:23:23 +0000 Subject: [PATCH] Upgrade logging framework. #PL-4755 --- .../org.apache.commons.logging.LogFactory | 1 - .../cuba/core/sys/logging/CubaLogFactory.java | 18 -- .../cuba/core/sys/logging/CubaLogWrapper.java | 46 ----- .../cuba/security/sys/UserSessionManager.java | 3 +- .../haulmont/cuba/core/sys/AppContext.java | 2 + .../cuba/core/sys/UserSessionFinder.java | 19 ++ .../cuba/core/sys/jmx/MBeanExporter.java | 33 ++++ .../core/sys/logging/AbstractLogWrapper.java | 168 ------------------ .../cuba/core/sys/logging/LogMdc.java | 51 ++++++ .../sys/remoting/RemoteProxyBeanCreator.java | 2 +- .../PortalRemoteProxyBeanCreator.java | 2 +- .../org.apache.commons.logging.LogFactory | 1 - .../cuba/web/sys/logging/WebLogFactory.java | 23 --- .../cuba/web/sys/logging/WebLogWrapper.java | 33 ---- .../remoting/WebRemoteProxyBeanCreator.java | 2 +- 15 files changed, 110 insertions(+), 294 deletions(-) delete mode 100644 modules/core/src/META-INF/services/org.apache.commons.logging.LogFactory delete mode 100644 modules/core/src/com/haulmont/cuba/core/sys/logging/CubaLogFactory.java delete mode 100644 modules/core/src/com/haulmont/cuba/core/sys/logging/CubaLogWrapper.java create mode 100644 modules/global/src/com/haulmont/cuba/core/sys/UserSessionFinder.java delete mode 100644 modules/global/src/com/haulmont/cuba/core/sys/logging/AbstractLogWrapper.java create mode 100644 modules/global/src/com/haulmont/cuba/core/sys/logging/LogMdc.java delete mode 100644 modules/web/src/META-INF/services/org.apache.commons.logging.LogFactory delete mode 100644 modules/web/src/com/haulmont/cuba/web/sys/logging/WebLogFactory.java delete mode 100644 modules/web/src/com/haulmont/cuba/web/sys/logging/WebLogWrapper.java diff --git a/modules/core/src/META-INF/services/org.apache.commons.logging.LogFactory b/modules/core/src/META-INF/services/org.apache.commons.logging.LogFactory deleted file mode 100644 index df46b4c79f..0000000000 --- a/modules/core/src/META-INF/services/org.apache.commons.logging.LogFactory +++ /dev/null @@ -1 +0,0 @@ -com.haulmont.cuba.core.sys.logging.CubaLogFactory \ No newline at end of file diff --git a/modules/core/src/com/haulmont/cuba/core/sys/logging/CubaLogFactory.java b/modules/core/src/com/haulmont/cuba/core/sys/logging/CubaLogFactory.java deleted file mode 100644 index 285d0214f7..0000000000 --- a/modules/core/src/com/haulmont/cuba/core/sys/logging/CubaLogFactory.java +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (c) 2008-2013 Haulmont. All rights reserved. - * Use is subject to license terms, see http://www.cuba-platform.com/license for details. - */ -package com.haulmont.cuba.core.sys.logging; - -import org.apache.commons.logging.impl.LogFactoryImpl; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogConfigurationException; - -public class CubaLogFactory extends LogFactoryImpl { - - @Override - public Log getInstance(String s) throws LogConfigurationException { - Log log = super.getInstance(s); - return new CubaLogWrapper(log); - } -} diff --git a/modules/core/src/com/haulmont/cuba/core/sys/logging/CubaLogWrapper.java b/modules/core/src/com/haulmont/cuba/core/sys/logging/CubaLogWrapper.java deleted file mode 100644 index 8bb57e8358..0000000000 --- a/modules/core/src/com/haulmont/cuba/core/sys/logging/CubaLogWrapper.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (c) 2008-2013 Haulmont. All rights reserved. - * Use is subject to license terms, see http://www.cuba-platform.com/license for details. - */ -package com.haulmont.cuba.core.sys.logging; - -import com.haulmont.cuba.core.sys.AppContext; -import com.haulmont.cuba.core.sys.SecurityContext; -import com.haulmont.cuba.security.global.UserSession; -import com.haulmont.cuba.security.sys.UserSessionManager; -import org.apache.commons.logging.Log; -import org.springframework.context.ApplicationContext; - -/** - * @author krivopustov - * @version $Id$ - */ -public class CubaLogWrapper extends AbstractLogWrapper { - - public CubaLogWrapper(Log delegate) { - super(delegate); - } - - @Override - protected String getUserInfo() { - String logUserName = AppContext.getProperty("cuba.logUserName"); - if (logUserName == null || logUserName.equals("") || Boolean.valueOf(logUserName)) { - SecurityContext securityContext = AppContext.getSecurityContext(); - if (securityContext != null) { - if (securityContext.getUser() != null) { - return securityContext.getUser(); - } else { - ApplicationContext context = AppContext.getApplicationContext(); - if (context != null) { - UserSessionManager usm = context.getBean(UserSessionManager.NAME, UserSessionManager.class); - UserSession session = usm.findSession(securityContext.getSessionId()); - if (session != null) { - return session.getUser().getLogin(); - } - } - } - } - } - return null; - } -} \ No newline at end of file diff --git a/modules/core/src/com/haulmont/cuba/security/sys/UserSessionManager.java b/modules/core/src/com/haulmont/cuba/security/sys/UserSessionManager.java index 38f46259a3..7e2a20f3d3 100644 --- a/modules/core/src/com/haulmont/cuba/security/sys/UserSessionManager.java +++ b/modules/core/src/com/haulmont/cuba/security/sys/UserSessionManager.java @@ -14,6 +14,7 @@ import com.haulmont.cuba.core.Transaction; import com.haulmont.cuba.core.global.Metadata; import com.haulmont.cuba.core.global.UserSessionSource; import com.haulmont.cuba.core.global.UuidSource; +import com.haulmont.cuba.core.sys.UserSessionFinder; import com.haulmont.cuba.security.app.UserSessionsAPI; import com.haulmont.cuba.security.entity.*; import com.haulmont.cuba.security.global.NoUserSessionException; @@ -37,7 +38,7 @@ import java.util.UUID; * @version $Id$ */ @ManagedBean(UserSessionManager.NAME) -public class UserSessionManager { +public class UserSessionManager implements UserSessionFinder { public static final String NAME = "cuba_UserSessionManager"; diff --git a/modules/global/src/com/haulmont/cuba/core/sys/AppContext.java b/modules/global/src/com/haulmont/cuba/core/sys/AppContext.java index 872abd29cc..3b2d6f8f9a 100644 --- a/modules/global/src/com/haulmont/cuba/core/sys/AppContext.java +++ b/modules/global/src/com/haulmont/cuba/core/sys/AppContext.java @@ -5,6 +5,7 @@ package com.haulmont.cuba.core.sys; import com.haulmont.bali.datastruct.Pair; +import com.haulmont.cuba.core.sys.logging.LogMdc; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContext; @@ -153,6 +154,7 @@ public class AppContext { if (log.isTraceEnabled()) log.trace("setSecurityContext " + securityContext + " for thread " + Thread.currentThread()); securityContextHolder.set(securityContext); + LogMdc.setup(securityContext); } /** diff --git a/modules/global/src/com/haulmont/cuba/core/sys/UserSessionFinder.java b/modules/global/src/com/haulmont/cuba/core/sys/UserSessionFinder.java new file mode 100644 index 0000000000..e5a4baaee4 --- /dev/null +++ b/modules/global/src/com/haulmont/cuba/core/sys/UserSessionFinder.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2008-2015 Haulmont. All rights reserved. + * Use is subject to license terms, see http://www.cuba-platform.com/license for details. + */ + +package com.haulmont.cuba.core.sys; + +import com.haulmont.cuba.security.global.UserSession; + +import java.util.UUID; + +/** + * @author Konstantin Krivopustov + * @version $Id$ + */ +public interface UserSessionFinder { + + UserSession findSession(UUID sessionId); +} diff --git a/modules/global/src/com/haulmont/cuba/core/sys/jmx/MBeanExporter.java b/modules/global/src/com/haulmont/cuba/core/sys/jmx/MBeanExporter.java index 3a68ef8553..1a9dc585b3 100644 --- a/modules/global/src/com/haulmont/cuba/core/sys/jmx/MBeanExporter.java +++ b/modules/global/src/com/haulmont/cuba/core/sys/jmx/MBeanExporter.java @@ -5,8 +5,15 @@ package com.haulmont.cuba.core.sys.jmx; +import org.apache.commons.logging.LogFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.jmx.support.MBeanRegistrationSupport; + import javax.management.DynamicMBean; import javax.management.JMException; +import java.lang.reflect.Field; +import java.util.Map; /** * Tweaked MBean exporter. @@ -21,8 +28,34 @@ import javax.management.JMException; */ public class MBeanExporter extends org.springframework.jmx.export.MBeanExporter { + private Logger log = LoggerFactory.getLogger(MBeanExporter.class); + public MBeanExporter() { setAssembler(new AnnotationMBeanInfoAssembler()); + // hack logging + try { + Field loggerField = MBeanRegistrationSupport.class.getDeclaredField("logger"); + loggerField.setAccessible(true); + loggerField.set(this, LogFactory.getLog(org.springframework.jmx.export.MBeanExporter.class)); + } catch (NoSuchFieldException | IllegalAccessException ignore) { + } + } + + @Override + public void afterSingletonsInstantiated() { + // hack logging + Map beans = null; + try { + Field beansField = org.springframework.jmx.export.MBeanExporter.class.getDeclaredField("beans"); + beansField.setAccessible(true); + beans = (Map) beansField.get(this); + } catch (NoSuchFieldException | IllegalAccessException ignore) { + } + if (beans != null) { + log.info("Registering beans for JMX exposure: " + beans.keySet()); + } + + super.afterSingletonsInstantiated(); } @Override diff --git a/modules/global/src/com/haulmont/cuba/core/sys/logging/AbstractLogWrapper.java b/modules/global/src/com/haulmont/cuba/core/sys/logging/AbstractLogWrapper.java deleted file mode 100644 index 86451a99b8..0000000000 --- a/modules/global/src/com/haulmont/cuba/core/sys/logging/AbstractLogWrapper.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright (c) 2008-2013 Haulmont. All rights reserved. - * Use is subject to license terms, see http://www.cuba-platform.com/license for details. - */ -package com.haulmont.cuba.core.sys.logging; - -import com.haulmont.cuba.core.sys.AppContext; -import org.apache.commons.lang.BooleanUtils; -import org.apache.commons.logging.Log; - -/** - * @author krivopustov - * @version $Id$ - */ -public abstract class AbstractLogWrapper implements Log { - - protected final Log delegate; - - protected ThreadLocal inLogging = new ThreadLocal<>(); - - public AbstractLogWrapper(Log delegate) { - this.delegate = delegate; - } - - protected String getInfo() { - if (BooleanUtils.isTrue(inLogging.get())) { - return ""; - } - - inLogging.set(true); - try { - StringBuilder sb = new StringBuilder(); - - String logAppName = AppContext.getProperty("cuba.logAppName"); - if (logAppName == null || logAppName.equals("") || Boolean.valueOf(logAppName)) { - String webContextName = AppContext.getProperty("cuba.webContextName"); - if (webContextName != null && !webContextName.equals("")) { - sb.append("[").append(webContextName).append("] "); - } - } - - String userInfo = getUserInfo(); - if (userInfo != null) { - sb.append("[").append(userInfo).append("] "); - } - - return sb.toString(); - } finally { - inLogging.set(null); - } - } - - protected abstract String getUserInfo(); - - @Override - public boolean isDebugEnabled() { - return delegate.isDebugEnabled(); - } - - @Override - public boolean isErrorEnabled() { - return delegate.isErrorEnabled(); - } - - @Override - public boolean isFatalEnabled() { - return delegate.isFatalEnabled(); - } - - @Override - public boolean isInfoEnabled() { - return delegate.isInfoEnabled(); - } - - @Override - public boolean isTraceEnabled() { - return delegate.isTraceEnabled(); - } - - @Override - public boolean isWarnEnabled() { - return delegate.isWarnEnabled(); - } - - @Override - public void trace(Object o) { - if (delegate.isTraceEnabled()) { - delegate.trace(getInfo() + o); - } - } - - @Override - public void trace(Object o, Throwable throwable) { - if (delegate.isTraceEnabled()) { - delegate.trace(getInfo() + o, throwable); - } - } - - @Override - public void debug(Object o) { - if (delegate.isDebugEnabled()) { - delegate.debug(getInfo() + o); - } - } - - @Override - public void debug(Object o, Throwable throwable) { - if (delegate.isDebugEnabled()) { - delegate.debug(getInfo() + o, throwable); - } - } - - @Override - public void info(Object o) { - if (delegate.isInfoEnabled()) { - delegate.info(getInfo() + o); - } - } - - @Override - public void info(Object o, Throwable throwable) { - if (delegate.isInfoEnabled()) { - delegate.info(getInfo() + o, throwable); - } - } - - @Override - public void warn(Object o) { - if (delegate.isWarnEnabled()) { - delegate.warn(getInfo() + o); - } - } - - @Override - public void warn(Object o, Throwable throwable) { - if (delegate.isWarnEnabled()) { - delegate.warn(getInfo() + o, throwable); - } - } - - @Override - public void error(Object o) { - if (delegate.isErrorEnabled()) { - delegate.error(getInfo() + o); - } - } - - @Override - public void error(Object o, Throwable throwable) { - if (delegate.isErrorEnabled()) { - delegate.error(getInfo() + o, throwable); - } - } - - @Override - public void fatal(Object o) { - if (delegate.isFatalEnabled()) { - delegate.fatal(getInfo() + o); - } - } - - @Override - public void fatal(Object o, Throwable throwable) { - if (delegate.isFatalEnabled()) { - delegate.fatal(getInfo() + o, throwable); - } - } -} diff --git a/modules/global/src/com/haulmont/cuba/core/sys/logging/LogMdc.java b/modules/global/src/com/haulmont/cuba/core/sys/logging/LogMdc.java new file mode 100644 index 0000000000..99b1118dff --- /dev/null +++ b/modules/global/src/com/haulmont/cuba/core/sys/logging/LogMdc.java @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2008-2015 Haulmont. All rights reserved. + * Use is subject to license terms, see http://www.cuba-platform.com/license for details. + */ + +package com.haulmont.cuba.core.sys.logging; + +import com.haulmont.cuba.core.sys.AppContext; +import com.haulmont.cuba.core.sys.SecurityContext; +import com.haulmont.cuba.core.sys.UserSessionFinder; +import com.haulmont.cuba.security.global.UserSession; +import org.slf4j.MDC; +import org.springframework.context.ApplicationContext; + +/** + * @author Konstantin Krivopustov + * @version $Id$ + */ +public class LogMdc { + + public static final String USER = "user"; + + public static void setup(SecurityContext securityContext) { + String userProp = AppContext.getProperty("cuba.logUserName"); + if (userProp == null || Boolean.valueOf(userProp)) { + if (securityContext != null) { + String user = securityContext.getUser(); + if (user == null) { + UserSession session = securityContext.getSession(); + if (session != null) + user = session.getUser().getLogin(); + else if (securityContext.getSessionId() != null) { + ApplicationContext applicationContext = AppContext.getApplicationContext(); + if (applicationContext.containsBean("cuba_UserSessionManager")) { + UserSessionFinder sessionFinder = (UserSessionFinder) applicationContext.getBean("cuba_UserSessionManager"); + session = sessionFinder.findSession(securityContext.getSessionId()); + if (session != null) { + user = session.getUser().getLogin(); + } + } + } + } + if (user != null) { + MDC.put(USER, "/" + user); + } + } else { + MDC.remove(USER); + } + } + } +} diff --git a/modules/global/src/com/haulmont/cuba/core/sys/remoting/RemoteProxyBeanCreator.java b/modules/global/src/com/haulmont/cuba/core/sys/remoting/RemoteProxyBeanCreator.java index 001ceda527..16ed027936 100644 --- a/modules/global/src/com/haulmont/cuba/core/sys/remoting/RemoteProxyBeanCreator.java +++ b/modules/global/src/com/haulmont/cuba/core/sys/remoting/RemoteProxyBeanCreator.java @@ -51,7 +51,7 @@ public class RemoteProxyBeanCreator implements BeanFactoryPostProcessor, Applica @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { - log.info("Configuring remote proxy beans for " + support.getBaseUrl()); + log.info("Configuring remote proxy beans for " + support.getBaseUrl() + ": " + services.keySet()); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; diff --git a/modules/portal/src/com/haulmont/cuba/portal/sys/remoting/PortalRemoteProxyBeanCreator.java b/modules/portal/src/com/haulmont/cuba/portal/sys/remoting/PortalRemoteProxyBeanCreator.java index 0e652002a6..dd2729548d 100644 --- a/modules/portal/src/com/haulmont/cuba/portal/sys/remoting/PortalRemoteProxyBeanCreator.java +++ b/modules/portal/src/com/haulmont/cuba/portal/sys/remoting/PortalRemoteProxyBeanCreator.java @@ -31,7 +31,7 @@ public class PortalRemoteProxyBeanCreator extends RemoteProxyBeanCreator { String useLocal = AppContext.getProperty("cuba.useLocalServiceInvocation"); if (Boolean.valueOf(useLocal)) { - log.info("Configuring proxy beans for local service invocations"); + log.info("Configuring proxy beans for local service invocations: " + services.keySet()); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; for (Map.Entry entry : services.entrySet()) { String name = entry.getKey(); diff --git a/modules/web/src/META-INF/services/org.apache.commons.logging.LogFactory b/modules/web/src/META-INF/services/org.apache.commons.logging.LogFactory deleted file mode 100644 index 179bd5617f..0000000000 --- a/modules/web/src/META-INF/services/org.apache.commons.logging.LogFactory +++ /dev/null @@ -1 +0,0 @@ -com.haulmont.cuba.web.sys.logging.WebLogFactory \ No newline at end of file diff --git a/modules/web/src/com/haulmont/cuba/web/sys/logging/WebLogFactory.java b/modules/web/src/com/haulmont/cuba/web/sys/logging/WebLogFactory.java deleted file mode 100644 index c86778973a..0000000000 --- a/modules/web/src/com/haulmont/cuba/web/sys/logging/WebLogFactory.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2008-2013 Haulmont. All rights reserved. - * Use is subject to license terms, see http://www.cuba-platform.com/license for details. - */ -package com.haulmont.cuba.web.sys.logging; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogConfigurationException; -import org.apache.commons.logging.impl.LogFactoryImpl; - -/** - *

$Id$

- * - * @author krivopustov - */ -public class WebLogFactory extends LogFactoryImpl { - - @Override - public Log getInstance(String s) throws LogConfigurationException { - Log log = super.getInstance(s); - return new WebLogWrapper(log); - } -} diff --git a/modules/web/src/com/haulmont/cuba/web/sys/logging/WebLogWrapper.java b/modules/web/src/com/haulmont/cuba/web/sys/logging/WebLogWrapper.java deleted file mode 100644 index 03a47bfe54..0000000000 --- a/modules/web/src/com/haulmont/cuba/web/sys/logging/WebLogWrapper.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright (c) 2008-2013 Haulmont. All rights reserved. - * Use is subject to license terms, see http://www.cuba-platform.com/license for details. - */ -package com.haulmont.cuba.web.sys.logging; - -import com.haulmont.cuba.core.sys.AppContext; -import com.haulmont.cuba.core.sys.SecurityContext; -import com.haulmont.cuba.core.sys.logging.AbstractLogWrapper; -import org.apache.commons.logging.Log; - -/** - * @author krivopustov - * @version $Id$ - */ -public class WebLogWrapper extends AbstractLogWrapper { - - public WebLogWrapper(Log delegate) { - super(delegate); - } - - @Override - protected String getUserInfo() { - String logUserName = AppContext.getProperty("cuba.logUserName"); - if (logUserName == null || logUserName.equals("") || Boolean.valueOf(logUserName)) { - SecurityContext securityContext = AppContext.getSecurityContext(); - if (securityContext != null) { - return securityContext.getUser(); - } - } - return null; - } -} \ No newline at end of file diff --git a/modules/web/src/com/haulmont/cuba/web/sys/remoting/WebRemoteProxyBeanCreator.java b/modules/web/src/com/haulmont/cuba/web/sys/remoting/WebRemoteProxyBeanCreator.java index 50dfde11a7..4d931eab5d 100644 --- a/modules/web/src/com/haulmont/cuba/web/sys/remoting/WebRemoteProxyBeanCreator.java +++ b/modules/web/src/com/haulmont/cuba/web/sys/remoting/WebRemoteProxyBeanCreator.java @@ -31,7 +31,7 @@ public class WebRemoteProxyBeanCreator extends RemoteProxyBeanCreator { String useLocal = AppContext.getProperty("cuba.useLocalServiceInvocation"); if (Boolean.valueOf(useLocal)) { - log.info("Configuring proxy beans for local service invocations"); + log.info("Configuring proxy beans for local service invocations: " + services.keySet()); BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory; for (Map.Entry entry : services.entrySet()) { String name = entry.getKey();