Giter Club home page Giter Club logo

cherry-tool-log's Introduction

cherry-tool-log

通用的系统日志记录工具 日志框架基于用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);
}

默认日志使用控制台输出,目前支持三种方式

1. 控制台输出(默认)

@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字符串。

2. 文件方式输出

默认使用控制台输出,该项是可配置的,修改application.yml:

   record: 
     log: 
       storageType: file
       filePath: rr.log

3. 数据库方式输出

修改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();
}

cherry-tool-log's People

Contributors

jiaozi789 avatar lzeqian avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.