日志文件的管理

博客展示了Java代码,包含JBWriteLog类和TestLog类。JBWriteLog类用于写日志,构造函数设置配置文件,writeLog方法执行写日志操作;TestLog类的main方法创建JBWriteLog实例并调用writeLog方法写入多条日志,涉及异常处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import org.apache.log4j.*;

/**
 * @author 
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class JBWriteLog {
 
  Logger log = Logger.getLogger("writelog.JBWriteLog");
  Logger logE = Logger.getLogger("writelog.JBWriteLog");
  //文件路径斜扛
  char xg = java.io.File.separatorChar;
  /**
   * 构造函数:并设置配置文件
   * @param PropertyFile:配置文件路径
   */
  public JBWriteLog(String PropertyFile) throws Exception{
    PropertyConfigurator.configure(PropertyFile);
  }
  /**
   * 写日志操作
   * @param logInfo:日志一个条目的内容
   * @throws Exception
   */
  public void writeLog(String logInfo) throws Exception{
    try {
      //引入普通信息日志配置文件
      //PropertyConfigurator.configure("."+xg+"properties"+xg+propertiesFileName);

      log.info(logInfo);
    }
    catch (Exception e) {

      e.printStackTrace();
      throw e;
    }
 
}

}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * @author 
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */
public class TestLog {
 
 /**
  * Constructor
  */
    public TestLog() {
    }


    public static void main(String[] args) {
       try{
          JBWriteLog wl = new JBWriteLog("/properties/log4j.properties");
          wl.writeLog("1");
          wl.writeLog("2");
          wl.writeLog("3");
          wl.writeLog("4");
          }catch(Exception e)
           {
           e.printStackTrace();
           }

      }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值