Java 如何获取控制台日志输出的信息? WriterAppender 可以实现? 配置文件如何配置

一:关于lo4j.properties 文件的配置

og4j.rootLogger=info,stdout,R,WriterAppender

# log to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%p] [%t] [%c:%L] %m%n

# log to file
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.Threshold=DEBUG
log4j.appender.R.File=${log4j.path}/iot.log
log4j.appender.R.DatePattern='_'yyyy-MM-dd
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern= %-d{yyyy-MM-dd HH:mm:ss} [%p]-[%c:%L] %m%n
#sql
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
#worklog
log4j.logger.work=Info.workItem
log4j.appender.workItem=org.apache.log4j.DailyRollingFileAppender
log4j.appender.workItem.File=${log4j.path}/work.log
log4j.additivity.work=false
log4j.appender.workItem.layout=org.apache.log4j.PatternLayout
log4j.appender.workItem.layout.ConversionPattern= %-d{yyyy-MM-dd HH:mm:ss} [%p]-[%c:%L] %m%n

##log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
log4j.rootCategory=WriterAppender
log4j.appender.WriterAppender=org.apache.log4j.WriterAppender
log4j.appender.WriterAppender.Threshold=debug
log4j.appender.WriterAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.WriterAppender.layout.ConversionPattern=%d{ISO8601} - %p - %m%n

二;Java代码获取控制台日志信息数据:

Logger root = Logger.getRootLogger();
PipedReader reader=null;
try {
    Appender appender = root.getAppender("WriterAppender");
    reader= new PipedReader();
    Writer writer = new PipedWriter(reader);
    ((WriterAppender) appender).setWriter(writer);
    Scanner scanner = new Scanner(reader);
    while (scanner.hasNext()this.session.getBasicRemote().sendText(scanner.nextLine()); }
   Log4JUtil.makeLogData();
} catch (Exception e) {

 }
最后你可以添加你自定义的代码了
 
  

你可能感兴趣的:(java)