Recently encountered a demand for testing, to use the program constantly to monitor whether the specified service components are running, one minute detection, if an exception is found, it is necessary to capture the exception information, and then recorded in the log for developers to troubleshoot.
But in the process of implementation, found a problem, the resulting log in the file, there is no clear row and column tag, very inconvenient operation, and the log is an append mode, in the case of classification, to find more difficult by classification. Like what:
A total of 1W log, info level of 8,000, error level of 500, warning level 1500, how to organize the data well, easy to find bugs, easy to filter it?
I used two sets of ordinal manipulation of these data, a set of ordinal is the log of each category, there is a number of their own, at the same time, there is a global log sequence number, in this case, the different business units generated by the log, as long as the push to my single process, can achieve global sorting records, local also has a sequence, and saved to the same file for easy centralized processing.
The implementation is as follows:
/** * */package com;/** * @author Administrator * */public class HelloWorld {/** * @param args */public static void main ( String[] args) {//TODO auto-generated method Stubmylog log1 = new MyLog (); MyLog log2 = new MyLog (); MyLog log3 = new MyLog (); MyLog log4 = new MyLog (); MyLog log5 = new MyLog () Log1.write (1, "Running"), Log1.write (2, "reopen"), Log2.write (1, "starting"), Log2.write (3, " Crashed "), Log2.write (1," Resume "), Log3.write (1," stoped "), Log4.write (1," Prompt "), Log5.write (2," Network Failed "); Log5.write (1, "Network Normal"), Log5.write (2, "Thread Crash"), Log5.write (3, "Connect Failed");}} Class MyLog {private static int nserialno = 0;//0 is the INFO level, 1 is warning level, 2 is the error level final int info_type = 0;final int Warnning_ TYPE = 1;final int error_type = 2;static int ninfono = 0;//local ordinal static int nwarnningno = 0;static int Nerrorno = 0;private int ngolobalno = 0;//Global ordinal private String STRMSG = "";//log message public void write (int type, String msg) {STRMSG = Msg;ngoloba LNo = (++nserialno);//0 is info level, 1 is warNing level, 2 is the error level switch (type) {case INFO_TYPE:System.out.println ("log[info][" + Ngolobalno + "]" + "[" + (++ninfono) + "]: "+ STRMSG"); Break;case WARNNING_TYPE:System.out.println ("log[warning][" + Ngolobalno + "]" + "[" + (++nwarnningno) + "]:" + STRMSG); Break;case ERROR_TYPE:System.out.println ("log[error][" + Ngolobalno + "]" + "[" + (++nerrorno) + "]:" + STRMSG); Break;default:system.out.println ("log[unknow][" + Ngolobalno + "]" + "[" + "-" + "]:" + STRMSG);
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The count policy when Java tests serialized data