Upgrade logging framework. #PL-4755

This commit is contained in:
Konstantin Krivopustov 2015-09-14 07:23:23 +00:00
parent 0742514a12
commit 8c2032f6c0
15 changed files with 110 additions and 294 deletions

View File

@ -1 +0,0 @@
com.haulmont.cuba.core.sys.logging.CubaLogFactory

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -14,6 +14,7 @@ import com.haulmont.cuba.core.Transaction;
import com.haulmont.cuba.core.global.Metadata; import com.haulmont.cuba.core.global.Metadata;
import com.haulmont.cuba.core.global.UserSessionSource; import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.core.global.UuidSource; 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.app.UserSessionsAPI;
import com.haulmont.cuba.security.entity.*; import com.haulmont.cuba.security.entity.*;
import com.haulmont.cuba.security.global.NoUserSessionException; import com.haulmont.cuba.security.global.NoUserSessionException;
@ -37,7 +38,7 @@ import java.util.UUID;
* @version $Id$ * @version $Id$
*/ */
@ManagedBean(UserSessionManager.NAME) @ManagedBean(UserSessionManager.NAME)
public class UserSessionManager { public class UserSessionManager implements UserSessionFinder {
public static final String NAME = "cuba_UserSessionManager"; public static final String NAME = "cuba_UserSessionManager";

View File

@ -5,6 +5,7 @@
package com.haulmont.cuba.core.sys; package com.haulmont.cuba.core.sys;
import com.haulmont.bali.datastruct.Pair; import com.haulmont.bali.datastruct.Pair;
import com.haulmont.cuba.core.sys.logging.LogMdc;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -153,6 +154,7 @@ public class AppContext {
if (log.isTraceEnabled()) if (log.isTraceEnabled())
log.trace("setSecurityContext " + securityContext + " for thread " + Thread.currentThread()); log.trace("setSecurityContext " + securityContext + " for thread " + Thread.currentThread());
securityContextHolder.set(securityContext); securityContextHolder.set(securityContext);
LogMdc.setup(securityContext);
} }
/** /**

View File

@ -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);
}

View File

@ -5,8 +5,15 @@
package com.haulmont.cuba.core.sys.jmx; 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.DynamicMBean;
import javax.management.JMException; import javax.management.JMException;
import java.lang.reflect.Field;
import java.util.Map;
/** /**
* Tweaked MBean exporter. * Tweaked MBean exporter.
@ -21,8 +28,34 @@ import javax.management.JMException;
*/ */
public class MBeanExporter extends org.springframework.jmx.export.MBeanExporter { public class MBeanExporter extends org.springframework.jmx.export.MBeanExporter {
private Logger log = LoggerFactory.getLogger(MBeanExporter.class);
public MBeanExporter() { public MBeanExporter() {
setAssembler(new AnnotationMBeanInfoAssembler()); 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 @Override

View File

@ -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<Boolean> 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);
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -51,7 +51,7 @@ public class RemoteProxyBeanCreator implements BeanFactoryPostProcessor, Applica
@Override @Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 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; BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

View File

@ -31,7 +31,7 @@ public class PortalRemoteProxyBeanCreator extends RemoteProxyBeanCreator {
String useLocal = AppContext.getProperty("cuba.useLocalServiceInvocation"); String useLocal = AppContext.getProperty("cuba.useLocalServiceInvocation");
if (Boolean.valueOf(useLocal)) { 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; BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
for (Map.Entry<String, String> entry : services.entrySet()) { for (Map.Entry<String, String> entry : services.entrySet()) {
String name = entry.getKey(); String name = entry.getKey();

View File

@ -1 +0,0 @@
com.haulmont.cuba.web.sys.logging.WebLogFactory

View File

@ -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;
/**
* <p>$Id$</p>
*
* @author krivopustov
*/
public class WebLogFactory extends LogFactoryImpl {
@Override
public Log getInstance(String s) throws LogConfigurationException {
Log log = super.getInstance(s);
return new WebLogWrapper(log);
}
}

View File

@ -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;
}
}

View File

@ -31,7 +31,7 @@ public class WebRemoteProxyBeanCreator extends RemoteProxyBeanCreator {
String useLocal = AppContext.getProperty("cuba.useLocalServiceInvocation"); String useLocal = AppContext.getProperty("cuba.useLocalServiceInvocation");
if (Boolean.valueOf(useLocal)) { 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; BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
for (Map.Entry<String, String> entry : services.entrySet()) { for (Map.Entry<String, String> entry : services.entrySet()) {
String name = entry.getKey(); String name = entry.getKey();