Persistent log LogPersist class

Foreword:

        The project has done part of it, to have the function of persistent log. The purpose is to facilitate the construction of the library in the future. The project background presets functions such as access records and login records, but this place has not been written in the first phase, so it is necessary to do some persistent information operations.

        Because these data will be built in the future, so the style can't be written casually. Therefore, a tool class is encapsulated to facilitate the persistence of these log information or data before the database is built;

        Of course, this class can also be used to write data to the file in order, it also supports non-sequential writing, and also supports writing in a custom format;

Above code:

package souqu.life.util;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


import com.alibaba.fastjson.JSON;


/**
 * Log data Persistence class
 * @author Jia Hengfei
 * @remark The persistence operation class of log information. For log storage operations, some common storage formats are built in, and users are also allowed to customize the storage mode
 */
public class LogPersist {
private static LogPersist operation; //Singleton operation tool class
private LogPersist (){}
/**
* Get the log operation object
* @return LogPersist
*/
public static LogPersist getOperation(){
if (operation==null) {
operation = new LogPersist();
}
return operation;
} /** * Get the predefined log template class



* @return Log (this class is an inner class, and an instance can only be obtained through this method)
*/
public Log log(){
return new Log();
} /** * Use a custom style to store log information * @ param format specification scheme (replace with "?" for each msg position in the scheme) * Example: '[?],?,---:?'--->'[2018-4-17], hello, ---:here I am! ' * @param msg info */ public void log(Format format,String... msg){ String chars = format.logsFormat(); for (String string : msg) { chars = chars.replaceFirst("?", string ); } tofiles(format.gainPath(), chars); } /** * Log template class * @author Jia Hengfei * @@remark This class is final and cannot be overridden or inherited. This class provides some predetermined solutions */ public final class Log{ /** * Save the log in csv format






 
















*/
public void csv(Path path,String... msg){
String chars = "\n";//Data initializes a newline
for (String str : msg) {
chars = chars+str+",";
}
tofiles (path.gainPath(), chars.substring(0,chars.length()-1));//Remove the last comma
}
/**
* Save the log in json format (each record is an object)
*/
public <T> void json(Path path,T t){
String json = "";
        try {
            json = JSON.toJSONString(t);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(!json. isEmpty()){ tofiles(path.gainPath(), json+",\n");         } } /** * Save log in wildcard format */
       





public void wildcard(String wildcard,Path path,String... msg){
String chars = "\n";//Data initializes a newline
for (String str : msg) {
chars = chars+str+wildcard;
}
tofiles (path.gainPath(), chars.substring(0,chars.length()-1));//Remove the last wildcard
}
/**
* Save the log in plain text format, no line breaks and no spaces
*/
public void txt (Path path,String... msg){
String chars = "";
for (String string : msg) {
chars = chars + string;
}
tofiles(path.gainPath(), chars);
}
} /** * log Information storage path * @author Jia Hengfei */ public interface Path{ String gainPath(); } /** * Information storage template * @author Jia Hengfei












*/
public interface Format extends Path{
String logsFormat();
} /** * Default path * @param fileName filename * @return returns a path */ public String path(String fileName){ return "C://logs/ "+fileName+".txt"; } /** * Append content to the specified file * @param filePath file path (if the file exists, append the content; if the file does not exist, create the file) * @param msg appended content */ private void tofiles(String filePath,String msg) { FileWriter fw = null; try { // append content if file exists; create file if file does not exist File f = new File(filePath); fw = new FileWriter(f, true); } catch (IOException e) { e.printStackTrace(); }
























PrintWriter pw = new PrintWriter(fw);
pw.println(msg);
pw.flush();
try {
fw.flush();
pw.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
        Four template data are provided by default, namely csv table format, json format, wildcard format and plain text mode. These four modes can actually meet most of the needs. If you need to customize some styles, you can implement the Format interface on the page, and call the log method to pass in the Format interface;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325763429&siteId=291194637