Java Logger:创建后缀为转码+ .log的文件 [英] Java Logger: Create file with rotation number + .log as suffix

查看:226
本文介绍了Java Logger:创建后缀为转码+ .log的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用java.util.logging包中的Java Logger.这是我当前创建记录器的方式:

I am using the Java Logger in the java.util.logging package. This is how I create the logger currently:

FileHandler fileHandler = new FileHandler(filePath, 5242880, 5, true);
fileHandler.setFormatter(new java.util.logging.Formatter() {
  @Override
  public String format(LogRecord logRecord) {
    if(logRecord.getLevel() == Level.INFO) {
      return "[INFO  " + createDateTimeLog() + "]  " + logRecord.getMessage() + "\r\n";
    } else if(logRecord.getLevel() == Level.WARNING) {
      return "[WARN  " + createDateTimeLog() + "]  " + logRecord.getMessage() + "\r\n";
    } else if(logRecord.getLevel() == Level.SEVERE) {
      return "[ERROR " + createDateTimeLog() + "]  " + logRecord.getMessage() + "\r\n";
    } else {
      return "[OTHER " + createDateTimeLog() + "]  " + logRecord.getMessage() + "\r\n";
    }
  }
  });
logger.addHandler(fileHandler)

现在,当我的记录器记录日志时,它将创建一个扩展名为.0,.1,.2(等)的文件.我希望它说.0.log,.1.log(etc).我找不到可以设置的位置.任何想法/帮助都会很棒.

Now when my logger logs, it creates a file with the extention .0,.1,.2 (etc). I would prefer for it to say .0.log, .1.log (etc). I cannot find where I can set this. Any ideas / help would be great.

推荐答案

构造fileHandler对象时,修改filePath以使用模式.创建一个使用%g组件的模式.该组件将在运行时用世代号替换,以区分旋转的日志.

When you construct your fileHandler object, modify filePath to use a pattern. Create a pattern that makes use of the %g component. This component will be replaced at runtime with the generation number to distinguish rotated logs.

示例
要将日志文件放置在格式为%TEMP%/mylog.1.log的系统临时目录中,请使用以下模式:

Example
To place log file in the system temp directory with form %TEMP%/mylog.1.log, use the following pattern:

`"%t/mylog.%g.log"` 

这篇关于Java Logger:创建后缀为转码+ .log的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆