通用的系统日志记录工具
日志框架基于用java8+springboot2.0.6编写
目前仅支持在springboot环境下
- 添加maven依赖 (目前未同步到中央仓库,在sonatype仓库中)
<dependency>
<groupId>io.github.jiaozi789.tool</groupId>
<artifactId>cherry-tool-log</artifactId>
<version>0.0.1-RELEASE</version>
</dependency>
-
main方法类上添加启用日志注解
@EnableRecordLog
-
有日志需求的service实现类方法上添加
@RecordLog(value = "'前置通知'+#h")
public String hello(String h) {
return ("hello" + h);
}
默认日志使用控制台输出,目前支持三种方式
@RecordLog的value值是个数组,可以多条日志,value使用ognl表达式 \#参数名,也可以使用\#参数对象.属性
支持默认的一些参数:
默認可以使用的常量是
- \#date 表示格式化的yyyy-MM-dd日期,表示当前插入日志时间点
- \#time 表示格式化的 HH24:mi:ss時間。
- \#datetime表示格式化的 yyyy-MM-dd HH24:mi:ss日期。
- \#curClass 當前的類名。
- \#curMethodName 當前的方法名稱。
- \#visitTime表示调用该业务逻辑的时间,格式化的 yyyy-MM-dd HH24:mi:ss 只有AFTER触发才能使用该变量。
- \#excuteTime 執行時間,只有AFTER触发才能使用该变量,否则为0 单位是ns。
- \#returnValue表示方法的返回值 ,只有AFTER触发才能使用该变量 否则为空。
- \#exception表示方法的异常信息 ,只有AFTER触发才能使用该变量 否则为空。
- \#uuid 产生一个随机的uuid字符串。
默认使用控制台输出,该项是可配置的,修改application.yml:
record:
log:
storageType: file
filePath: rr.log
修改application.yml
record:
log:
storageType: database
spring:
datasource:
url: jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driverClassName: com.mysql.jdbc.Driver
注意springboot程序必须构建一个DataSource,可以使用data-jpa或者使用其他连接池构建。 在service添加日志表信息:
@RecordLog(value = {"'user registriong userName:'+#userName+',password:'+#password","#datetime"})
@RecordTable(table = @Table(name="my_log"), id = "id", columns = {
@Column(name = "content"),
@Column(name = "createTime")
},generationType=GenerationType.IDENTITY)
public boolean login(String userName,String password) {
if("admin".equals(userName) && "password".equals(password)) {
return true;
}
return false;
}
RecordTable注解定义的列必须和RecordLog的value数组顺序一致 主键类型(AUTO,IDENTIFY,UUID): - identity表示columns所有列和value对应,uuid也不需要指定值:
@RecordLog(value = {"'用户注册 用户名:'+#userName+',密码:'+#password","#datetime"})
@RecordTable(table = @Table(name="my_log"), id = "id", columns = {
@Column(name = "content"),
@Column(name = "createTime")
},generationType=GenerationType.IDENTITY)
public boolean login(String userName,String password) {
- auto是程序指定主键 第0列就是主键的参数
@RecordLog(value = {"#user.id","'用户注册 用户名:'+#user.userName+',密码:'+#user.password","#datetime"})
@RecordTable(table = @Table(name="my_log"), id = "id", columns = {
@Column(name = "content"),
@Column(name = "createTime")
},generationType=GenerationType.AUTO)
public boolean login(User user)
如果觉得使用@RecordTable每个日志都需要定义注解,可以自己实现DataSourceStorage接口,注册到springbean的容器。 此时就不需要使用@RecordTable注解。
@Bean
public DataSourceStorage dss(){
return XXXDataSourceStorage();
}