java日志可以出现在控制台中也可以保存在文件中。可以通过控制java日志的输出流来控制日志的输出。可以用java中的System.out.println,也可以用log4j。log4j的配置文件默认在src目录下,优先读取log4j.xml,如果没有则读取src下的log4j.properties。具体配置文件格式可以百度“log4j”,查百科。
“System.out.println + log4j”可以将System.out.println输出的内容以log4j的格式输出到控制台或文件中。(可以将一条线程所有类的日志输出按程序调用顺序输出) 。
需要用System.setOut()方法重定向System.out.println输出的位置。
1.写一个类继承outputStream。重写flush方法。
package Text;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.IOException;
import org.apache.log4j.Logger;
public class ConsoleOutputStream extends OutputStream
{
private ByteArrayOutputStream outputStream = new ByteArrayOutputStream(256);
// 获取log4j.xml配置中的“SystemOut"、"SystemErr"的输出格式
private Logger m_log1 = Logger.getLogger("SystemOut");
private Logger m_log2 = Logger.getLogger("SystemErr");
String ps;
public ConsoleOutputStream(String ps )
{
this.ps = ps ;
}
public void write(int b)
{
outputStream.write(b);
}
public void write(byte b[]) throws IOException {
outputStream.write(b);
}
public void write(byte b[], int off, int len) throws IOException {
outputStream.write(b, off, len);
}
//重写方法,将写入输出流中日志内容按log4j的输出格式和输出方向输出。
public void flush() throws IOException
{
try
{
if( ps != null && ps.equals("out"))
{
m_log1.info(outputStream.toString("GBK"));
}
else if( ps != null && ps.equals("err"))
{
m_log2.info(outputStream.toString("GBK"));
}
outputStream.reset();
}
catch (Exception e)
{}
}
}
2.写一个类继承 PrintStream,那需要重写相关方法。
package Text;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Hashtable;
public class SystemLogHandler extends PrintStream {
// ----------------------------------------------------------- Constructors
/**
* Construct the handler to capture the output of the given steam.
*/
public SystemLogHandler(PrintStream wrapped) {
super(wrapped);
this.wrapped = wrapped;
}
// ----------------------------------------------------- Instance Variables
/**
* Wrapped PrintStream.
*/
protected PrintStream wrapped = null;
/**
* Thread <-> PrintStream associations.
*/
protected static Hashtable streams = new Hashtable();
/**
* Thread <-> ByteArrayOutputStream associations.
*/
protected static Hashtable data = new Hashtable();
// --------------------------------------------------------- Public Methods
public PrintStream getWrapped() {
return wrapped;
}
/**
* Start capturing thread's output.
*/
public static void setThread() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
data.put(Thread.currentThread(), baos);
streams.put(Thread.currentThread(), ps);
}
/**
* Stop capturing thread's output and return captured data as a String.
*/
public static String unsetThread() {
ByteArrayOutputStream baos =
(ByteArrayOutputStream) data.get(Thread.currentThread());
if (baos == null) {
return null;
}
streams.remove(Thread.currentThread());
data.remove(Thread.currentThread());
return baos.toString();
}
// ------------------------------------------------------ Protected Methods
/**
* Find PrintStream to which the output must be written to.
*/
protected PrintStream findStream() {
PrintStream ps = (PrintStream) streams.get(Thread.currentThread());
if (ps == null) {
ps = wrapped;
}
return ps;
}
// ---------------------------------------------------- PrintStream Methods
public void flush() {
findStream().flush();
}
public void close() {
findStream().close();
}
public boolean checkError() {
return findStream().checkError();
}
protected void setError() {
//findStream().setError();
}
public void write(int b) {
findStream().write(b);
}
public void write(byte[] b)
throws IOException {
findStream().write(b);
}
public void write(byte[] buf, int off, int len) {
findStream().write(buf, off, len);
}
public void print(boolean b) {
findStream().print(b);
}
public void print(char c) {
findStream().print(c);
}
public void print(int i) {
findStream().print(i);
}
public void print(long l) {
findStream().print(l);
}
public void print(float f) {
findStream().print(f);
}
public void print(double d) {
findStream().print(d);
}
public void print(char[] s) {
findStream().print(s);
}
public void print(String s) {
findStream().print(s);
}
public void print(Object obj) {
findStream().print(obj);
}
public void println() {
findStream().println();
findStream().flush();
}
public void println(boolean x) {
findStream().println(x);
findStream().flush();
}
public void println(char x) {
findStream().println(x);
findStream().flush();
}
public void println(int x) {
findStream().println(x);
findStream().flush();
}
public void println(long x) {
findStream().println(x);
findStream().flush();
}
public void println(float x) {
findStream().println(x);
findStream().flush();
}
public void println(double x) {
findStream().println(x);
findStream().flush();
}
public void println(char[] x) {
findStream().println(x);
findStream().flush();
}
public void println(String x) {
findStream().println(x);
findStream().flush();
}
public void println(Object x) {
findStream().println(x);
findStream().flush();
}
}
3.写一个测试方法
private void log(){
System.setOut(new SystemLogHandler(new PrintStream(new YBTConsoleOutputStream("out"))));
}
配置文件例
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<!-- define some appenders here -->
<appender name="ccc" class="org.apache.log4j.FileAppender">
<param name="File" value="ccc.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %m"/>
</layout>
</appender>
<appender name="SystemOut" class="org.apache.log4j.FileAppender">
<param name="File" value="SystemOut.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %x - %m"/>
</layout>
</appender>
<appender name="SystemErr" class="org.apache.log4j.FileAppender">
<param name="File" value="SystemErr.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d - %x - %m"/>
</layout>
</appender>
<!-- define logger here -->
<category name="SystemOut" additivity="false">
<priority value="info"/>
<appender-ref ref="SystemOut"/>
</category>
<category name="SystemErr" additivity="false">
<priority value="info"/>
<appender-ref ref="SystemErr"/>
</category>
<root>
<priority value ="info" />
<appender-ref ref="ccc" />
</root>
</log4j:configuration>
这样在这个线程下和log()方法下的所有System.out.println的输出都会按log4j的配置输出。