JDK自带的Logger简单使用--输出信息到文本
public class WebLogger {private static Logger logger = Logger.getLogger(WebLogger.class.getName());static{try {FileHandler fileHandler = null;if("linux".equalsIgnoreCase(Config.getOSNa
·
/**
* 日志记录功能
* @author WJL
* @date 2014-1-22
* @email wjl@zving.com
*/
public class WebLogger {
private static Logger logger = Logger.getLogger(WebLogger.class.getName());
static{
try {
FileHandler fileHandler = null;
if("linux".equalsIgnoreCase(Config.getOSName())){
fileHandler = new FileHandler("/opt/log_"+System.currentTimeMillis()+".log");
}else{
fileHandler = new FileHandler("D:\\log_"+System.currentTimeMillis()+".log");
}
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
fileHandler.setFormatter(new Formatter() {
@Override
public String format(LogRecord arg0) {
return String.format("%-8s",arg0.getLevel().getLocalizedName())+sdf.format(new Date(arg0.getMillis()) )+" : "+ arg0.getMessage()+"\n";
}
});
logger.addHandler(fileHandler);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void showWarning(String message){
logger.log(Level.WARNING, message);
}
public static void showInfo(String message){
logger.info(message);
}
public static void main(String[] args) {
WebLogger.showInfo("Hello World");
WebLogger.showWarning("Hello 你妹");
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)