Implement System Log Management Based on easyUI and implement logs using easyui
This article is based on
EasyUI + Knockout allows you to view and edit classic forms.
I. Preparations
1. Click here to download the relevant files and place the files in the corresponding folder of the ims project.
II. Introduction to relevant documents
1. log. jsp: System log Management Interface
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"> <% @ page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> View Code
2. log. js: log management list, function, and log view
Function viewModel () {var self = this; this. form = {logDate: ko. observable ()}; this. system = {size: {w: 6, h: 123}, url: rootPath + "/sys/log! GetSystemLog. do ", queryParams: ko. observable (), pagination: true}; this. refreshClick = function () {window. location. reload () ;}; this. searchClick = function () {this. system. queryParams (ko. toJS (this. form) ;}; this. clearClick = function () {this. form. logDate (""); this. searchClick () ;}}; var view = function (id) {com. dialog ({title: "view log", width: 700, height: 500, maximizable: true, html: "# log- View-template ", viewModel: function (w) {com. ajax ({type:" GET ", url: rootPath +"/sys/log! View. do? FileName = "+ id, success: function (d) {$ (" # logView ").html (d );}})}});};
View Code
3. logController. java
Package com. ims. web. controller. sys; import java. io. bufferedInputStream; import java. io. bufferedOutputStream; import java. io. file; import java. io. fileInputStream; import java. io. inputStream; import java. io. outputStream; import java. util. arrayList; import java. util. arrays; import java. util. hashMap; import java. util. list; import java. util. map; import org. apache. commons. lang3.StringUtils; import org. apache. log4j. fi LeAppender; import org. apache. log4j. logger; import org. springframework. stereotype. controller; import org. springframework. web. bind. annotation. requestMapping; import org. springframework. web. bind. annotation. requestParam; import org. springframework. web. servlet. modelAndView; import com. ims. common. dateUtil; import com. ims. common. fileUtil. fileAttr; import com. ims. common. fileUtil; @ Controller @ RequestMapping ("sys ") Public class LogController extends SysBaseController {@ RequestMapping ("log") public ModelAndView log () {ModelAndView view = new ModelAndView ("sys/log. jsp "); return view;} @ RequestMapping (" log! GetSystemLog ") public void getSystemLog (@ RequestParam Map <String, String> params) {FileAppender appender = (FileAppender) Logger. getRootLogger (). getAppender ("roll"); String filePath = appender. getFile (); String logFileName = filePath. substring (filePath. lastIndexOf ("/") + 1, filePath. length (); File folders = new File (filePath. substring (0, filePath. lastIndexOf ("/") + 1); String [] files = folders. list (); If (StringUtils. isNotBlank (params. get ("logDate") {Map <String, String> date = DateUtil. getStartEndDate (params. get ("logDate"); List <String> newFiles = new ArrayList <String> (); for (String fileName: files) {String temp = fileName; if (logFileName. equals (fileName) {temp = fileName + ". "+ DateUtil. getCurrDate ();} temp = temp. substring (logFileName. length () + 1, temp. length () + "00:00:00"; if (StringUtils. I SNotBlank (date. get ("startDate") {if (temp. compareTo (date. get ("startDate") <0) {continue ;}} if (StringUtils. isNotBlank (date. get ("endDate") {if (temp. compareTo (date. get ("endDate")> 0) {continue ;}} newFiles. add (fileName);} files = (String []) newFiles. toArray (new String [newFiles. size ()]);} Map <String, Object> result = new HashMap <String, Object> (); result. put ("total", files. length); Integer page = Integer. valueOf (params. get ("page"); Integer rows = Integer. valueOf (params. get ("rows"); files = Arrays. copyOfRange (files, (page-1) * rows, page * rows); List <FileAttr> fileList = new ArrayList <FileAttr> (); for (String fileName: files) {if (fileName! = Null) {FileAttr fileAttr = new FileAttr (); fileAttr. setId (fileName); fileAttr. setFileName (fileName); File file = new File (folders. getAbsolutePath () + "\" + fileName); fileAttr. setFileSize (file. length ()/1024 + "KB"); fileList. add (fileAttr) ;}} result. put ("rows", fileList); ajaxJson (result) ;}@ RequestMapping ("log! View ") public void view (String fileName) {FileAppender appender = (FileAppender) Logger. getRootLogger (). getAppender ("roll"); String filePath = appender. getFile (); File folders = new File (filePath. substring (0, filePath. lastIndexOf ("/") + 1); String log = FileUtil. readFileByLines (folders. getAbsolutePath () + "\" + fileName); ajaxJson (log) ;}@ RequestMapping ("log! Download ") public void download (String fileName) {try {FileAppender appender = (FileAppender) Logger. getRootLogger (). getAppender ("roll"); String filePath = appender. getFile (); File folders = new File (filePath. substring (0, filePath. lastIndexOf ("/") + 1); String path = folders. getAbsolutePath () + "\" + fileName; File file = new File (path); // path is the String filename = file spliced Based on the Log path and File name. getName (); // get the log file name InputStream FCM = new BufferedInputStream (new FileInputStream (path); byte [] buffer = new byte [FCM. available ()]; FCM. read (buffer); FCM. close (); response. reset (); // first remove the spaces in the file name, and then convert the encoding format to UTF-8 to ensure no garbled characters. This file name is used for the file name response automatically displayed in the download box of the browser. addHeader ("Content-Disposition", "attachment; filename =" + new String (filename. replaceAll ("",""). getBytes ("UTF-8"), "iso8859-1"); response. addHeader ("Content-Length", "" + file. length (); OutputStream OS = new BufferedOutputStream (response. getOutputStream (); response. setContentType ("application/octet-stream"); OS. write (buffer); // output file OS. flush (); OS. close () ;}catch (Exception e) {ajaxJson (STATUS_ERROR, "Log File Download failed ");}}}
View Code
III.
1. Access: http: // localhost: 8090/ims/sys/log. do, log management interface
2. Click to view