Permalink
Browse files

[PAXLOGGING-138]also patch JdkHandler in pax-logging-logback

  • Loading branch information...
ffang committed May 3, 2012
1 parent a06d511 commit 29c27a7c23b80795404ca898290f16da9eb1c512
@@ -20,6 +20,8 @@
import org.ops4j.pax.logging.PaxLogger;
import org.ops4j.pax.logging.PaxLoggingService;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.FrameworkUtil;
import java.util.logging.Handler;
import java.util.logging.Level;
@@ -32,8 +34,15 @@
public class JdkHandler extends Handler
{
+ private static final SecurityManagerEx securityManager;
+
private PaxLoggingService m_logService;
+ static
+ {
+ securityManager = new SecurityManagerEx();
+ }
+
public JdkHandler( PaxLoggingService logService )
{
m_logService = logService;
@@ -78,9 +87,9 @@ public void publish( LogRecord record )
{
Level level = record.getLevel();
String loggerName = record.getLoggerName();
- // TODO: Can't associate a bundle with the JDK logger. So how??
+ Bundle callerBundle = getCallerBundle();
String fqcn = java.util.logging.Logger.class.getName();
- PaxLogger logger = m_logService.getLogger( null, loggerName, fqcn );
+ PaxLogger logger = m_logService.getLogger( callerBundle, loggerName, fqcn );
String message;
try
{
@@ -103,4 +112,25 @@ else if (levelInt <= Level.WARNING.intValue())
else
logger.error(message, throwable);
}
+
+ private Bundle getCallerBundle() {
+ Bundle ret = null;
+ Class[] classCtx = securityManager.getClassContext();
+ for (int i = 0; i < classCtx.length; i++) {
+ if (!classCtx[i].getName().startsWith("org.ops4j.pax.logging")
+ && !classCtx[i].getName().startsWith("java.util.logging")) {
+ ret = FrameworkUtil.getBundle(classCtx[i]);
+ break;
+ }
+ }
+ return ret;
+ }
+
+ static class SecurityManagerEx extends SecurityManager
+ {
+ public Class[] getClassContext()
+ {
+ return super.getClassContext();
+ }
+ }
}

0 comments on commit 29c27a7

Please sign in to comment.