User: starksm
Date: 01/05/10 19:50:29
Added: src/main/org/jboss/logging/log4j CategoryStream.java
ConsoleAppender.java
Log:
Create a log4j specific subpackage for custom log4j classes.
Revision Changes Path
1.1 jboss/src/main/org/jboss/logging/log4j/CategoryStream.java
Index: CategoryStream.java
===================================================================
/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.logging.log4j;
import java.io.PrintStream;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
/** A subclass of PrintStream that redirects its output to a log4j Category.
This class is used to map PrintStream/PrintWriter oriented logging onto
the log4j Categories. Examples include capturing System.out/System.err writes.
@author [EMAIL PROTECTED]
@version $Revision: 1.1 $
*/
public class CategoryStream extends PrintStream
{
private Category category;
private Priority priority;
/** Redirect logging to the indicated category using Priority.INFO
*/
public CategoryStream(Category category)
{
this(category, Priority.INFO, System.out);
}
/** Redirect logging to the indicated category using the given
priority. The ps is simply passed to super but is not used.
*/
public CategoryStream(Category category, Priority priority, PrintStream ps)
{
super(ps);
this.category = category;
this.priority = priority;
}
public void println(String msg)
{
category.log(priority, msg);
}
public void println(Object msg)
{
category.log(priority, msg);
}
public void write(byte[] b, int off, int len)
{
// Remove the end of line chars
while( (b[len-1] == '\n' || b[len-1] == '\r') && len > off )
len --;
String msg = new String(b, off, len);
category.log(priority, msg);
}
}
1.1 jboss/src/main/org/jboss/logging/log4j/ConsoleAppender.java
Index: ConsoleAppender.java
===================================================================
/*
* JBoss, the OpenSource EJB server
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.logging.log4j;
import java.io.PrintStream;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Category;
import org.apache.log4j.Priority;
import org.apache.log4j.spi.LoggingEvent;
/** A log4j Appender implementation that writes to the System.out and
System.err console streams. It also installs PrintStreams for System.out
and System.err to route logging through those objects to the log4j
system via a category named Default.
@author [EMAIL PROTECTED]
@version $Revision: 1.1 $
*/
public class ConsoleAppender extends AppenderSkeleton
{
private Category category;
private PrintStream out;
private PrintStream err;
/** Creates new ConsoleAppender */
public ConsoleAppender()
{
out = System.out;
err = System.err;
}
public void activateOptions()
{
super.activateOptions();
category = Category.getInstance("Default");
System.setOut(new CategoryStream(category, Priority.INFO, out));
System.setErr(new CategoryStream(category, Priority.ERROR, err));
}
public boolean requiresLayout()
{
return true;
}
public void close()
{
if( out != null )
System.setOut(out);
out = null;
if( err != null )
System.setErr(err);
err = null;
}
protected void append(LoggingEvent event)
{
String msg = this.layout.format(event);
if( event.priority == Priority.ERROR )
out.print(msg);
else
err.print(msg);
}
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development