我正在使用java.util.logging。我想要2条日志-一个用于时间测量,另一个用于所有其他事情。问题是计时器记录器仍然记录所有内容,如何告诉它仅记录发送到logTime方法的内容?
import configurations.ConfigReader;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.*;
import configurations.Time;
import main.Main;
/**
* Created by giladba on 27/01/2016.
*/
public class LogManager {
private static final String LOGS_PATH = (String) ConfigReader.get(ConfigReader.Field.LOGS);
private static Logger globalLogger = Logger.getLogger(Main.class.getName());
private static Logger timingLogger = Logger.getLogger(Main.class.getName());
static {
initLoggers(globalLogger, (String) ConfigReader.get(ConfigReader.Field.ST_VIEW));
initLoggers(timingLogger, ConfigReader.get(ConfigReader.Field.ST_VIEW) + "_timing");
timingLogger.setUseParentHandlers(false);
}
public synchronized static void log(String message) {
globalLogger.info(message);
}
public synchronized static void logTime(String action, long time1, long time2) {
timingLogger.info(Time.getTimeNowFormatted() + action + "\t\t : " + (time2-time1) + " ms");
}
/**
* Initializes the logger.
* @throws SecurityException
* @throws IOException
*/
private static void initLoggers(Logger curLog, String name) throws SecurityException {
FileHandler procHandler = null;
try {
File file = new File(LOGS_PATH);
if(!file.exists())
file.mkdirs();
procHandler = new FileHandler(LOGS_PATH + name + "_log.out", 2048000, 20, true);
} catch (IOException e) {
e.printStackTrace();
}
procHandler.setFormatter(new MyFormatter());
procHandler.setLevel(Level.INFO);
curLog.addHandler(procHandler);
curLog.setUseParentHandlers(true);
curLog.info("\n");
curLog.info("--------" + (new SimpleDateFormat("dd-MM-yy_HH:mm:ss")).format(new Date()) + "--------");
}
/**
* simple naked formatter for the log files.
* @author jeremieg
*
*/
static class MyFormatter extends Formatter {
@Override
public String format(LogRecord arg0) {
return arg0.getMessage() + "\n";
}
}
}
private static Logger globalLogger = Logger.getLogger(Main.class.getName());
private static Logger timingLogger = Logger.getLogger(Main.class.getName());
那是相同的记录器,结果,您的配置变得混乱。
您必须使用两个不同的名称Logger.getLogger
才能获得两个不同的记录器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句