Skip to content

Commit

Permalink
Moving the basic logging project under RI core tree.
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@2085 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
drallen committed Mar 18, 2009
1 parent e20cc46 commit 5bd78e6
Show file tree
Hide file tree
Showing 10 changed files with 846 additions and 0 deletions.
37 changes: 37 additions & 0 deletions logging/pom.xml
@@ -0,0 +1,37 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>webbeans-parent</artifactId>
<groupId>org.jboss.webbeans</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>org.jboss.webbeans</groupId>
<artifactId>webbeans-logging</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>Web Beans Logging</name>
<dependencies>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
<classifier>jdk15</classifier>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>

</dependencies>

<build>
<defaultGoal>install</defaultGoal>
</build>

</project>
156 changes: 156 additions & 0 deletions logging/src/main/java/org/jboss/webbeans/log/JDKProvider.java
@@ -0,0 +1,156 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.jboss.webbeans.log;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Gavin King
*
*/
class JDKProvider implements LogProvider
{
private final Logger logger;
private final boolean isWrapped;

JDKProvider(String category, boolean wrapped)
{
this.logger = Logger.getLogger(category);
this.isWrapped = wrapped;
}

private void log(Level level, Object object, Throwable ex)
{

if (logger.isLoggable(level))
{
Throwable dummyException = new Throwable();
StackTraceElement locations[] = dummyException.getStackTrace();
String className = "unknown";
String methodName = "unknown";
int depth = isWrapped ? 3 : 2;
if (locations != null && locations.length > depth)
{
StackTraceElement caller = locations[depth];
className = caller.getClassName();
methodName = caller.getMethodName();
}
if (ex == null)
{
logger.logp(level, className, methodName, String.valueOf(object));
}
else
{
logger.logp(level, className, methodName, String.valueOf(object), ex);
}
}

}

public void debug(Object object, Throwable t)
{
log(Level.FINE, object, t);
}

public void debug(Object object)
{
log(Level.FINE, object, null);
}

public void error(Object object, Throwable t)
{
log(Level.SEVERE, object, t);
}

public void error(Object object)
{
log(Level.SEVERE, object, null);
}

public void fatal(Object object, Throwable t)
{
log(Level.SEVERE, object, t);
}

public void fatal(Object object)
{
log(Level.SEVERE, object, null);
}

public void info(Object object, Throwable t)
{
log(Level.INFO, object, t);
}

public void info(Object object)
{
log(Level.INFO, object, null);
}

public boolean isDebugEnabled()
{
return logger.isLoggable(Level.FINE);
}

public boolean isErrorEnabled()
{
return logger.isLoggable(Level.SEVERE);
}

public boolean isFatalEnabled()
{
return logger.isLoggable(Level.SEVERE);
}

public boolean isInfoEnabled()
{
return logger.isLoggable(Level.INFO);
}

public boolean isTraceEnabled()
{
return logger.isLoggable(Level.FINER);
}

public boolean isWarnEnabled()
{
return logger.isLoggable(Level.WARNING);
}

public void trace(Object object, Throwable t)
{
log(Level.FINER, object, t);
}

public void trace(Object object)
{
log(Level.FINER, object, null);
}

public void warn(Object object, Throwable t)
{
log(Level.WARNING, object, t);
}

public void warn(Object object)
{
log(Level.WARNING, object, null);
}

}
102 changes: 102 additions & 0 deletions logging/src/main/java/org/jboss/webbeans/log/Log.java
@@ -0,0 +1,102 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* 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.jboss.webbeans.log;

/**
* <p>A <code>Log</code> object is used by RI classes to log messages.
* They will be logged in any environment that has setup a logging service
* such as Log4J or the standard JDK logging facilities. In fact, this
* logging interface is very similar to the other facilities with the
* difference that logging methods also take any number of optional
* parameters beyond the message object for later interpolation
* into the message.</p>
*
* <p>The idea of using interpolation parameters is very important for
* performance reasons in production code. Normally developers write
* logging messages similar to this one:
* <pre>
* log.debug("Started processing of " + obj1 + " with action " + obj2);
* </pre>
* The problem that arises at runtime in production systems, is that DEBUG
* level logging may not be enabled. And even if this logging is going to
* be a no-op call, Java must still build the string dynamically that is the
* argument to the call. It is the building of this string that can be quite
* time consuming. The more complex the objects being concatenated are, the
* worse the time penalty incurred is. And this time is completely wasted
* since the string may never be used.</p>
*
* <p>Normally to combat the problem of making this call and building the
* string dynamically, the developer uses a conditional statement to invoke
* the call only if the corresponding logging level is enabled. So the above
* call may end up looking like:
* <pre>
* if (log.isDebugEnabled())
* {
* log.debug("Started processing of " + obj1 + " with action " + obj2);
* }
* </pre>
* Ideally, this structure should always be used to avoid the cost of building the
* dynamic string (concatenation) and making the unnecessary call. The only
* drawback to this is that code can become less readable. In some cases, there
* might be more code devoted to logging than the actual behavior required by a
* method.</p>
*
* <p>A cleaner way to do the logging is to use this interface where simple
* objects without any concatenation are passed as arguments. Albeit the call
* itself may still be a no-op when the logging level is not enabled, this is
* still much smaller than the concatenation process with dynamic strings. Each
* of the methods defined here will first check to see if the logging level is enabled,
* if that feature exists in the underlying logging system. If and only if that logging
* level is enabled, will the implementation proceed with concatenation of the strings
* and objects passed. So the above code using this interface becomes:
* <pre>
* log.debug("Started processing of {0} with action {1}, obj1, obj2);
* </pre>
* </p>
*
* <p>The interpolation of parameters into the message string are done using
* {@link java.text.MessageFormat}. See the documentation on that class for
* more ideas of interpolation possibilities. In the above code, <code>obj1</code>
* and <code>obj2</code> simply have their <code>toString()</code> methods invoked
* to produce a string which is then concatenated.</p>
*
* @author Gavin King
* @author David Allen
*
*/
public interface Log
{
public boolean isDebugEnabled();
public boolean isErrorEnabled();
public boolean isFatalEnabled();
public boolean isInfoEnabled();
public boolean isTraceEnabled();
public boolean isWarnEnabled();
public void trace(Object object, Object... params);
public void trace(Object object, Throwable t, Object... params);
public void debug(Object object, Object... params);
public void debug(Object object, Throwable t, Object... params);
public void info(Object object, Object... params);
public void info(Object object, Throwable t, Object... params);
public void warn(Object object, Object... params);
public void warn(Object object, Throwable t, Object... params);
public void error(Object object, Object... params);
public void error(Object object, Throwable t, Object... params);
public void fatal(Object object, Object... params);
public void fatal(Object object, Throwable t, Object... params);

}

0 comments on commit 5bd78e6

Please sign in to comment.