追加写入日志提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
基于springboot框架的一个日志追加功能 前言
一般我们记录日志的时候都会吧新出现的日志追加到原本的日志文本中,也就是我们常说的日志追加,现在这里有两种方法追加日志:一个是:利用hutool插件追加,一个是:我们自己写一个工具类方法来追加日志。格式是追加日志时如果有原有的日志文件信息就在文件内原有的信息下一行(“n”,注意使用的系统)追加新的日志信息,如果没有就新建一个文件并追加日志进文件。
一、准备
- springboot框架
- hutool依赖(糊涂)
cn.hutool
hutool-all
5.7.7
二、具体示例
1. 写工具类(用hutool插件的不用写,直接跳过这一步)
代码如下(示例):
package com.example.testwar.until;
import java.io.*;
public class TestTo {
public static void content(String file, String info) {
FileWriter fileWriter = null;
try {
//如果文件存在,则追加内容;如果文件不存在,就创建文件
File f = new File(file);
fileWriter = new FileWriter(f, true);
} catch (IOException e) {
e.printStackTrace();
}
assert fileWriter != null;
PrintWriter pw = new PrintWriter(fileWriter);
pw.println(info);
pw.flush();
try {
fileWriter.flush();
pw.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void content2(String file, String info) {
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)));
bufferedWriter.write(info + "rn");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
assert bufferedWriter != null;
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2. 用hutool插件的示例
代码如下(示例):
//用糊涂插件追加的日志
package com.example.testwar.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.util.StrUtil;
import com.example.testwar.until.TestTo;
public class testLog {
public static void main(String[] args) {
// 本地根路径用user.dir , 不加的话就在classes下面
String path = "D:\logs\";
TestTo testTo = new TestTo();
// 模拟数据
String s2 = "n" + "无言独上西楼,月如钩。寂寞梧桐深院锁清秋。剪不断,理还乱,是离愁。别是一般滋味在心头。" +
"n" + "寒蝉凄切,对长亭晚,骤雨初歇。都门帐饮无绪,留恋处,兰舟催发。执手相看泪眼,竟无语凝噎。念去去,千里烟波,暮霭沉沉楚天阔。n" +
" 多情自古伤离别,更那堪,冷落清秋节!今宵酒醒何处?杨柳岸,晓风残月。此去经年,应是良辰好景虚设。便纵有千种风情,更与何人说?";
String paths = StrUtil.format(path + "tmp/{}.log", "日志追加"); //文件路径及名称
try {
// appendUtf8String: 将String写入文件,UTF-8编码追加模式
FileUtil.appendUtf8String(s2 + "n", paths); // 自动换行
} catch (IORuntimeException e) {
e.printStackTrace();
}
}
}
3. 自己写工具类的示例
代码如下(示例):
//自己写一个日志追加的工具类(有用hutool建议用hutool)
package com.example.testwar.controller;
import com.example.testwar.until.TestTo;
public class testLog02 {
public static void main(String[] args) {
String path = "D:\logs\";
//模拟数据
String s2 = "n" + "无言独上西楼,月如钩。寂寞梧桐深院锁清秋。剪不断,理还乱,是离愁。别是一般滋味在心头。" +
"n" + "寒蝉凄切,对长亭晚,骤雨初歇。都门帐饮无绪,留恋处,兰舟催发。执手相看泪眼,竟无语凝噎。念去去,千里烟波,暮霭沉沉楚天阔。n" +
" 多情自古伤离别,更那堪,冷落清秋节!今宵酒醒何处?杨柳岸,晓风残月。此去经年,应是良辰好景虚设。便纵有千种风情,更与何人说?";
TestTo.addContent2(path + "日志1.log", s2);
}
}
这里生成的地址是在D盘上的,如果是直接写的话(/logs/日志.log)是生成在项目的根路径上吧,可以自己试一下.
4. 注意注意这里的换行符,不同系统换行符不一样:
Windows: rn Mac: r Linux: n
项目中的日志信息可能是json格式:
// 一般项目会传一个对象,如果有就转一下 TaskLogDto taskLogDto = JSON.parseObject(JSON.toJSONString(req.getData()), TaskLogDto.class);5. 方法选择
现在hutool插件我选择用的是appendUtf8String,但是插件不止这一个方法,还有覆盖这些的,可以点进去看具体的使用方法,下面粘一小部分出来如下:
public static File writeUtf8String(String content, String path) throws IORuntimeException {
return writeString(content, path, CharsetUtil.CHARSET_UTF_8);
}
public static File writeUtf8String(String content, File file) throws IORuntimeException {
return writeString(content, file, CharsetUtil.CHARSET_UTF_8);
}
public static File writeString(String content, String path, String charset) throws IORuntimeException {
return writeString(content, touch(path), charset);
}
public static File writeString(String content, String path, Charset charset) throws IORuntimeException {
return writeString(content, touch(path), charset);
}
public static File writeString(String content, File file, String charset) throws IORuntimeException {
return FileWriter.create(file, CharsetUtil.charset(charset)).write(content);
}
public static File writeString(String content, File file, Charset charset) throws IORuntimeException {
return FileWriter.create(file, charset).write(content);
}
public static File appendUtf8String(String content, String path) throws IORuntimeException {
return appendString(content, path, CharsetUtil.CHARSET_UTF_8);
}
public static File appendString(String content, String path, String charset) throws IORuntimeException {
return appendString(content, touch(path), charset);
}
public static File appendString(String content, String path, Charset charset) throws IORuntimeException {
return appendString(content, touch(path), charset);
}
public static File appendUtf8String(String content, File file) throws IORuntimeException {
return appendString(content, file, CharsetUtil.CHARSET_UTF_8);
}
public static File appendString(String content, File file, String charset) throws IORuntimeException {
return FileWriter.create(file, CharsetUtil.charset(charset)).append(content);
}
public static File appendString(String content, File file, Charset charset) throws IORuntimeException {
return FileWriter.create(file, charset).append(content);
}
总结
这里有一个问题,就是输出为日志时没有限制每一行的字符数量的大小,就是不会自动换行的意思,这个还我还解决不了,有那位大佬看到能帮忙解一下惑吗?万分感谢。



