Coder Social home page Coder Social logo

daodaoliang / aliyunlog Goto Github PK

View Code? Open in Web Editor NEW

This project forked from xnx3/aliyunlog

0.0 1.0 0.0 11.68 MB

不想吧日志信息频繁写入数据库中,可用阿里云日志服务,将日志保存到云。全程5~20分钟内完成快速接入!

License: Apache License 2.0

Java 100.00%

aliyunlog's Introduction

aliyunLog 前言简介

程序中需时刻记录日志,同时,又不想频繁将日志写入数据库堆积,可采用阿里云日志服务。
记录日志后可设置几天或几周有效期,超过有效期后自动转为文件存储起来并不会丢失。同时还可以将其导入MaxCompute进行大数据分析!
整理所得,将日志服务快速接入项目中使用,全程5~20分钟内接入完成接入,实现日志的基本操作:添加跟获取。

1 准备工作

阿里云日志服务控制台: https://sls.console.aliyun.com 增加一个项目(Project),然后再在其中增加一个日志库(logStore)
具体步骤可参考:http://www.guanleiming.com/1726.html

2 快速上手,2分钟内跑通项目

2.1 创建项目(Java Project)

可选 a. 将 j2seSource文件夹内的项目 直接导入到Eclipse中进行调试
可选 b. 其他编辑器,或自己手动创建项目,需导入 /j2se/lib/ 文件夹中的jar包

2.2 修改调试类,调试运行

/j2se/src/com/xnx3/aliyunLog/SimpleExample.java 修改以下静态工厂中代码,填写你步骤1中阿里云的相关参数

static{
    //这里填上你的阿里云日志服务相关的参数信息
    actionLog = new AliyunLogUtil("endpoint...", "accessKeyId...", "accessKeySecret...", "project...", "logstore...");
}

修改完这五个参数后,直接运行,即可看到效果。此时,日志的写入、读取已操作完毕!

3 加入Web项目使用

3.1 准备工作:将步骤2.1中所用的jar包加入到你的web项目中; 查看示例:/j2ee
3.2 写入日志,可参考步骤2.2中, SimpleExample 类的 simpleAdd() 方法
3.2 读取保存的日志列表并在页面上显示

3.2.1 控制器Controller中,增加列表页入口

public static AliyunLogUtil actionLog;
static{
    //这里填上你的阿里云日志服务相关的参数信息
    actionLog = new AliyunLogUtil("endpoint...", "accessKeyId...", "accessKeySecret...", "project...", "logstore...");
}
    
@RequestMapping("list")
public String list(HttpServletRequest request, Model model) throws LogException{
    AliyunLogPageUtil log = new AliyunLogPageUtil(actionLog);
    JSONArray jsonArray = log.list("", "", true, 10, request);
    model.addAttribute("list", jsonArray);
    model.addAttribute("page", log.getPage());
    return "simple/list";
}

3.2.2 视图view中,增加两个页面(直接从 /j2ee/WebRoot/WEB-INF/view/复制、simple/list.jsp)

  1. list.jsp (列表页,将数据列出展示),来自 /j2ee/WebRoot/WEB-INF/view/include/page.jsp
  2. page.jsp (底部的分页功能,显示上一页、下一页、首页、尾页等跳转)来自 /j2ee/WebRoot/WEB-INF/view/simple/list.jsp

    添加完毕后,重启项目,访问控制器中增加的list方法,看看效果(看一下list.jsp中include标签引用page.jsp的路径,别路径错了)
    至此,日志服务集成到项目中完毕!

4 扩展

4.1 jsp页面中增加将时间戳转化为时间描述的tld标签

在 步骤3.2 中,列表页调出数据,显示的日志存储时间 ${log.logtime} 是10位时间戳,可将其转化为能阅读的时间显示。
引入jar包 xnx3_tld.jar(点击下载) | xnx3_tld使用说明

5 预览效果

aliyunlog's People

Contributors

xnx3 avatar

Watchers

 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.