public class ServerWithLog4j { final static int SERVER_PORT = 8001 ; // this server's port /* add for log4j: class Logger is the central class in the log4j package. we can do most logging operations by Logger except configuration. getLogger(...): retrieve a logger by name, if not then create for it. */ static final Logger logger = (Logger) Logger.getInstance(ServerWithLog4j.class); /** * main method * @param args */ public static void main ( String args[]) { String clientRequest = null ; BufferedReader reader = null ; PrintWriter writer = null ; ServerSocket server = null ; Socket socket = null ; InputStream in = null ; OutputStream out = null ; /* add for log4j: class BasicConfigurator can quickly configure the package. print the information to console. */ PropertyConfigurator.configure ( "ServerWithLog4j.properties" ) ; // add for log4j: set the level logger.setLevel ( ( Level ) Level.DEBUG ) ; try{ server = new ServerSocket ( SERVER_PORT ) ; // add for log4j: log a message with the info level logger.info ( "ServerSocket before accept: " + server ) ; // add for log4j: log a message with the info level logger.info ( "Java server with log4j, on-line!" ) ; // wait for client's connection socket = server.accept() ; // add for log4j: log a message with the info level logger.info ( "ServerSocket after accept: " + server ) ; in = socket.getInputStream() ; out = socket.getOutputStream() ; } catch ( IOException e ) { // add for log4j: log a message with the error level logger.error ( "Server constructor IOException: " + e ) ; System.exit ( 0 ) ; } reader = new BufferedReader ( new InputStreamReader ( in ) ) ; writer = new PrintWriter ( new OutputStreamWriter ( out ), true ) ; // send welcome string to client writer.println ( "Java server with log4j, " + new Date () ) ; while ( true ) { try { // read from client clientRequest = reader.readLine () ; // add for log4j: log a message with the debug level logger.debug ( "Client says: " + clientRequest ) ; if ( clientRequest.startsWith ( "HELP" ) ) { // add for log4j: log a message with the debug level logger.debug ( "OK!" ) ; writer.println ( "Vocabulary: HELP QUIT" ) ; } else { if ( clientRequest.startsWith ( "QUIT" ) ) { // add for log4j: log a message with the debug level logger.debug ( "OK!" ) ; System.exit ( 0 ) ; } else { // add for log4j: log a message with the warn level logger.warn ( "Command '" + clientRequest + "' not understood." ) ; writer.println ( "Command '" + clientRequest + "' not understood." ) ; } } } catch ( IOException e ) { // add for log4j: log a message with the error level logger.error( "IOException in Server " + e ) ; System.exit ( 0 ) ; } } } }
PropertyConfigurator.config("C:\log4j.properties");
Logger log=Logger.getLogger(A.class);
然后配置下log4j.properties这个文件 这个文件是设置你输出到哪里和级别的
log4j.rootLogger=DEBUG,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
提供个比较简单的给你 至于工作原理 上网查 一下
http://www.javaresearch.org/article/59840.htm
1 下载log4j的包,把jar文件放在你的classpath下
2 配置log4j.properties或者log4j.xml,放在classpath下
3 在程序中使用log4j来出log吧
final static int SERVER_PORT = 8001 ; // this server's port /*
add for log4j: class Logger is the central class in the log4j package.
we can do most logging operations by Logger except configuration.
getLogger(...): retrieve a logger by name, if not then create for it.
*/
static final Logger logger = (Logger) Logger.getInstance(ServerWithLog4j.class); /**
* main method
* @param args
*/
public static void main ( String args[]) {
String clientRequest = null ;
BufferedReader reader = null ;
PrintWriter writer = null ;
ServerSocket server = null ;
Socket socket = null ;
InputStream in = null ;
OutputStream out = null ; /*
add for log4j: class BasicConfigurator can quickly configure the package.
print the information to console.
*/
PropertyConfigurator.configure ( "ServerWithLog4j.properties" ) ; // add for log4j: set the level
logger.setLevel ( ( Level ) Level.DEBUG ) ; try{
server = new ServerSocket ( SERVER_PORT ) ;
// add for log4j: log a message with the info level
logger.info ( "ServerSocket before accept: " + server ) ;
// add for log4j: log a message with the info level
logger.info ( "Java server with log4j, on-line!" ) ;
// wait for client's connection
socket = server.accept() ;
// add for log4j: log a message with the info level
logger.info ( "ServerSocket after accept: " + server ) ; in = socket.getInputStream() ;
out = socket.getOutputStream() ;
} catch ( IOException e ) {
// add for log4j: log a message with the error level
logger.error ( "Server constructor IOException: " + e ) ;
System.exit ( 0 ) ;
}
reader = new BufferedReader ( new InputStreamReader ( in ) ) ;
writer = new PrintWriter ( new OutputStreamWriter ( out ), true ) ; // send welcome string to client
writer.println ( "Java server with log4j, " + new Date () ) ; while ( true ) {
try {
// read from client
clientRequest = reader.readLine () ;
// add for log4j: log a message with the debug level
logger.debug ( "Client says: " + clientRequest ) ;
if ( clientRequest.startsWith ( "HELP" ) ) {
// add for log4j: log a message with the debug level
logger.debug ( "OK!" ) ;
writer.println ( "Vocabulary: HELP QUIT" ) ;
}
else {
if ( clientRequest.startsWith ( "QUIT" ) ) {
// add for log4j: log a message with the debug level
logger.debug ( "OK!" ) ;
System.exit ( 0 ) ;
}
else {
// add for log4j: log a message with the warn level
logger.warn ( "Command '" + clientRequest + "' not understood." ) ;
writer.println ( "Command '" + clientRequest + "' not understood." ) ;
}
}
} catch ( IOException e ) {
// add for log4j: log a message with the error level
logger.error( "IOException in Server " + e ) ;
System.exit ( 0 ) ;
}
}
}
}
它总在报错啊! 详细代码如下:
package myexcel;import jxl.*;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import jxl.write.Number;
import jxl.write.Boolean;
import java.io.*;
import java.util.*;
import myBean.*;
import java.io.File;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;
public class MyExcel {
/**
* 读取Excel
* @param filePath
*/
static final Logger logger = (Logger) Logger.getLogger(MyExcel.class);
public ArrayList readExcel(String filePath)
{
ArrayList _myExA=new ArrayList();
PropertyConfigurator.config("D:\\project\\mytree\\WebRoot\\log4j.properties");
通过程序配置
通过Property文件配置 :PropertyConfigurator.configure ( "配置文件名字.properties" ) ;
通过XML文件配置设置输出级别:logger.setLevel ( ( Level ) Level.DEBUG ) ; 写需要输出的信息: logger.info ( "ServerSocket before accept: " + server ) ;
http://zooo.51.net/heavyz_cs/notebook/log4j.html
http://www.javaresearch.org/article/59840.htm
http://www.solol.org/technologic/java/j-log4j/#resource