Java standard log output sample

Introduction

This log output method is to output the log at the time of batch execution, and it is not recommended because there are log rote etc. in WEB application etc. It is better to use Log4j obediently.

However, in a system where Log4j cannot be used for some reason (I don't know if it is included in the first place), I was told "Logs can be System.out.println ()" and thought "Wait a minute", so I will summarize it. .. Ordinary people don't use it.

Log formatter


public class BatchLogFomatter extends SimpleFormatter {

       public String format(LogRecord logRecord) {
          final StringBuffer stringBuffer = new StringBuffer();

          //Date format
          //This batch wasn't supposed to be a thread-sensitive batch,
          //I'm worried about putting it in a class variable, so write it in format
          DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
       
          stringBuffer.append(dateFormat.format(new Date(logRecord.getMillis())));
          stringBuffer.append(" ");

          Level level = logRecord.getLevel();
          //For some reason, FINE and below are not displayed, so just set FINE and below for the time being.
          //The names are changed to CONF, INFO, WARN, ERROR.
          //Display settings (DEBUG)=Let's say CONF.
          if (level == Level.FINEST) {
             stringBuffer.append("FINEST");
          } else if (level == Level.FINER) {
             stringBuffer.append("FINER ");
          } else if (level == Level.FINE) {
             stringBuffer.append("FINE ");
          } else if (level == Level.CONFIG) {
             stringBuffer.append("CONF");
          } else if (level == Level.INFO) {
             stringBuffer.append("INFO ");
          } else if (level == Level.WARNING) {
             stringBuffer.append("WARN ");
          } else if (level == Level.SEVERE) {
             stringBuffer.append("ERROR ");
          }
          
          stringBuffer.append(" ");
          //The class name will be entered (I don't know what it is here)
          stringBuffer.append(logRecord.getLoggerName());
          stringBuffer.append(" - ");
          //Message is displayed
          stringBuffer.append(logRecord.getMessage());
          //Don't forget the line breaks.
          stringBuffer.append("\n");

          // 2017-10-05 18:52:57.592 INFO  hoge.FugaBatch -Start batch execution
          //I get a log like
          return stringBuffer.toString();
          
       }
}

Mounting side


public class LogTest {

	static final Logger logger = Logger.getLogger(LogTest.class.getName());
	
	static {
		ConsoleHandler handler = new ConsoleHandler();
		//Change log level
                //Even if it is ALL, the following FINE does not come out ...
		handler.setLevel(Level.ALL);
		handler.setFormatter(new BatchLogFomatter());
		logger.addHandler(handler);
		//If this is not included, the log will be duplicated
		logger.setUseParentHandlers(false);

	}
	
	public static void main (String[] args) {
		logger.info("Get out with INFO");
	}

}

At the end

Since it is a batch execution, the log file is always separated, so do not worry about the details I try to output only the log. Is there a better way to get a log?

Recommended Posts

Java standard log output sample
About Java log output
Request parameter log output sample Java & Spring MVC
Sample code for log output by Java + SLF4J + Logback
Log output to file in Java
[Java] output, variables
[Java] Generics sample
Java sample code 02
Java sample code 03
Selenium sample (Java)
Java GUI sample
Ruby standard output
Java standard logger
Java sample code 04
Java sample code 01
[Java] Color the standard output to the terminal
java standard functional interface
[Java] GlassFish 5 Troubleshooting Log
[Java] logback slf4j sample
Java (jdk1.8 or later) file input / output sample program
paiza skill check standard input / output summary [Java edition]
Java memo (standard class) substring
Java parallelization code sample collection
Java memo (standard class) length
Read standard input in Java
[Java] List OS-dependent standard libraries
Correct log output method on Android (standard Log class vs AnkoLogger)
Log Java NullPointerException stack trace
Selenium Sample Reservation Form (Java)
[Java] I want to test standard input & standard output with JUnit
Use Microsoft Graph with standard Java
[Java] Output by FormatStyle of DateTimeFormatter
[Java] Output multidimensional array / spreadsheet (AOJ⑥ spreadsheet)
Java 9 new features and sample code
A simple sample callback in Java
Output PDF and TIFF with Java 8
BloomFilter description and implementation sample (JAVA)
[Java] Date period duplication check sample
EXCEL file update sample with JAVA
Log output of WebServiceTemplate request / response
Spring Data JPA SQL log output
Sample vending machine made in Java
Sample code using Minio from Java