Log output to file in Java

Introduction

Java material that occurs on a regular basis. It was decided to output a certain information as a log, but as a result of considering whether to output it to a database or a file, I decided to output a log to a file, so a memo when considering in advance. The premise is Linux.

Benefits of file output

The viewpoints are completely different, but enumerate as you can think of

--Since the storage area overhead is smaller than that of the database, it does not consume space. --When the log becomes large, it is necessary to consider dividing the data, but in the case of a database, it is troublesome to implement the process of creating a table, but if it is a file, the program only creates a new file. Easy --When you refer to the log, you cannot see it unless you log in and hit SQL if it is a database, but if it is a file, you can easily refer to it with more / less. --In the case of a database, it is necessary to additionally consider the effect on the backup of the database (processing time, etc.), but in the case of a file, only the backup of the entire system needs to be considered.

Disadvantages of file output

--If the exclusive processing is not performed properly, the file may be corrupted. --When searching, it is necessary to devise to improve performance because there is no index. --When performing a search, you need to create your own search logic because SQL cannot be used.

Source

So I made a program that outputs a one-line log consisting of time and text to a file. The file is generated monthly. It seems easy to use the synchronized method as exclusive control, so I chose this method. Of course, it is a major premise that you do not operate the log file other than this method.

LogWriter.java


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
import java.io.*;
public class LogWriterNew{

    public static SimpleDateFormat sdf = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");

    public static synchronized void writeLog(String text){
        Calendar calendar = Calendar.getInstance();

        String OUTPUT_DIR= "out";

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        Date date = calendar.getTime();

        String yearStr = String.format("%04d", year);
        String monthStr = String.format("%02d", month);

        //Log output
        String file_name = OUTPUT_DIR + File.separator + yearStr + "_" + monthStr + ".log";
        File file = new File(file_name);
        FileWriter fw = null;
        String line = sdf.format(date) + "," +  text;
        try{
            fw = new FileWriter(file, true);
            fw.write(line + "\n");
        }catch(IOException e){
            e.printStackTrace();
        }finally {
            if(fw != null) {
                try {
                    fw.close();
                }catch(Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] argv){

        for(int i=0; i<1000;i++){
            System.out.println(i);
            writeLog(i + "aaa");
        }
    }
}

output

Like this.

2020/05/23 08:25:02,0aaa
2020/05/23 08:25:02,1aaa
2020/05/23 08:25:02,2aaa
2020/05/23 08:25:02,3aaa
2020/05/23 08:25:02,4aaa
2020/05/23 08:25:02,5aaa
2020/05/23 08:25:02,6aaa
2020/05/23 08:25:02,7aaa
2020/05/23 08:25:02,8aaa
2020/05/23 08:25:02,9aaa
2020/05/23 08:25:02,10aaa
・ ・ ・ ・

Processing time (reference)

On a small PC, we got a throughput of ``` 850 cases / sec` ``.

Postscript (2020/5/24)

Since the character code cannot be specified with FileWriter, I tried to specify the character code using OutputStreamWriter.

 //Log output method
    public static synchronized boolean writeLog(String fileName, String date, String[] items){

        for(int i=0; i<items.length; i++){
            items[i] = escape(items[i]);
        }
        String line = date + "," + String.join(",", items);

        File file = new File(fileName);
        PrintWriter pw   = null;
        try {
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), "Windows-31j")));
            pw.println(line);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                try {
                    pw.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                    return false;
                }
            }
        }

in conclusion

Next time, I will verify whether exclusive control is really done by this syncronized.

Recommended Posts

Log output to file in Java
Gzip-compress byte array in Java and output to file
Sample to unzip gz file in Java
About Java log output
I tried to output multiplication table in Java
Enable log output to both file and console using log4j in Eclipse.
Output javadoc to word file
Output XML tree to file
Java standard log output sample
Ubuntu unable to log in
How to input / output IBM mainframe files in Java?
How to convert a file to a byte array in Java
Read Java properties file in C #
Multithreaded to fit in [Java] template
How to learn JAVA in 7 days
Unzip the zip file in Java
Mixed Western calendar output in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
How to use classes in Java?
How to name variables in Java
About file copy processing in Java
Try to implement Yubaba in Java
How to concatenate strings in java
Put the date (e.g. yyyy-MM-dd) in the log file output by logback
Sample to read and write LibreOffice Calc fods file in Java 2021
How to get the length of an audio file in java
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
Try to solve Project Euler in Java
Easy to make Slack Bot in Java
Java reference to understand in the figure
How to output Jetty log to any directory
Output Spring Boot log in json format
Output Notes document as XML document in Java
Output HTTP header of google-http-client to log
How to add jar file in ScalaIDE
How to do base conversion in Java
[Java] How to output and write files!
How to output Java string to console screen
Output Date in Java in ISO 8601 extended format
Convert SVG files to PNG files in Java
How to implement coding conventions in Java
How to embed Janus Graph in Java
Read xlsx file in Java with Selenium
How to get the date in java
Output of the book "Introduction to Java"
Add footnotes to Word documents in Java
[Java] Color the standard output to the terminal
[Java Bronze] 5 problems to keep in mind
Java to C and C to Java in Android Studio
Add SameSite attribute to cookie in Java
File output bean as JSON in spring
[Java] output, variables
The story of forgetting to close a file in Java and failing
To create a Zip file while grouping database search results in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
Changes in Java 11
Rock-paper-scissors in Java
How to record JFR (Java Flight Recorder) and output a dump file
java file creation