Java 自定义日志写入

/**
	 * 将信息写入到日志
	 * @param content
	 * @return
	 * @throws IOException
	 */
	public static boolean writeLog(String className,String ErrorInfo,String ErrorContent){
		String content=info(className, ErrorInfo, ErrorContent);
		File fileName = new File(System.getProperty("user.dir")+"/config/log.log");
		RandomAccessFile mm = null;
		boolean flag = false;
		FileOutputStream o = null;
		try {
			o = new FileOutputStream(fileName,true);
			o.write(content.getBytes("utf-8"));
			o.close();
			flag = true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (mm != null) {
				try {
					mm.close();	
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}
		return flag;
	}  

	/**
	 * 
	 * @param className 类名称
	 * @param ErrorInfo 错误信息
	 * @param ErrorContent 错误内容
	 * @return
	 */
	public static String info(String className,String ErrorInfo,String ErrorContent){
		 StringBuffer buffer=new StringBuffer();
		 Date date = new Date();   
         SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
         String d = sd.format(date)+"
";  
         buffer.append(d);
         buffer.append("错误类名为:").append(className).append("
");
         buffer.append("错误信息为:").append(ErrorInfo).append("
");
         buffer.append("错误内容为:").append(ErrorContent).append("
");
         buffer.append("
");
         return buffer.toString();
	}


 

原文地址:https://www.cnblogs.com/riasky/p/3473374.html