Show detailed error in Logger when running Java on server

Symptoms

When "Run on server [^ 1]", when using Logger to output to the console, the" Configuration "level and below were not displayed.

Conclusion

--Set the output destination to a file. I gave up printing to the console. --Set not only setLevel (Level.ALL) of FileHandler but alsosetLevel (Level.ALL) of Logger.

environment

Eclipse 2019-03 (4.11.0) Tomcat 9 Java 11 macOS 10.13.6

comment

It seems that there are some configuration files to output to the console. Article [1] I was able to solve it, but since I used it by overriding Formatter, it is troublesome to use the configuration file. .. So I gave up outputting to the console and output to a file.

Be careful when specifying the log file with a relative path [2].

It seems that the default value of setLevel () in Logger is Level.INFO (information). Let's set it properly.

Source code

Log.java


public class Log {
	private static final String DEBUG_FILE = "log/test.log";
	private static final Level DEBUG_LEVEL = Level.FINER;
	public static final Logger logger = Logger.getLogger("Test");

	public Log() throws SecurityException, IOException {
		
		logger.setLevel(Level.ALL);

		FileHandler debugHandler = new FileHandler(DEBUG_FILE, APPEND);
		debugHandler.setLevel(DEBUG_LEVEL);
		debugHandler.setFormatter(new MyFormatter());
		logger.addHandler(debugHandler);

		logger.setUseParentHandlers(false);

		test();
	}

	private static void test() {
		File file = new File(DEBUG_FILE);
        if (file.exists()) {
            System.out.println("logfile: " + file.getAbsolutePath());
        } else {
            System.out.println("logfile is not exist");
        }
		logger.finest("log test: FINEST");
		logger.finer("log test: FINER");
		logger.fine("log test: FINE");
		logger.config("log test: CONFIG");
		logger.info("log test: INFO");
		logger.warning("log test: WARNING");
		logger.severe("log test: SEVERE");
	}
}

By the way, the MyFormatter class is a self-made class that inherits java.util.logging.Formatter.

How to use

new Log();

Initialize with and where you want to output the log

Log.logger.fine("Log message");

I do.

reference

[1] Tomcat, Eclipse and logs https://qiita.com/utisam/items/e725ccff4062f3078ad9 [2] Relative path when running Tomcat on Eclipse https://qiita.com/milmilk/private/99102afbb678b8e82c4c [3] [Introduction to Java] How to use Logger (explains log level and output destination settings) https://www.sejuku.net/blog/61048 [4] How to use java.util.logging https://qiita.com/Qui/items/40077ce9e33738dd3914

[^ 1]: Eclipse + Tomcat app https://searchman.info/java_eclipse/1100.html

Recommended Posts

Show detailed error in Logger when running Java on server
Resolve CreateProcess error = 206 when running Java in a Windows environment
Error when running git push heroku master on Heroku
Error in bundle install when running rails new
What I learned when building a server in Java
Error when playing with java
An error occurs when codedeploy-agent is installed in Ubuntu Server 20.04
Symbol not found error when new class in another Java file
Remedy for "A server is already running." Error when running rails s
Avoid character code error in java when using VScode extension RUN-CODE
How to solve the unknown error when using slf4j in Java
Investigation method when the CPU of the server running java is heavy
Error when npm install on Windows 7
Try implementing GraphQL server in Java
<java> When "EXCEPTION_ACCESS_VIOLATION" appears in awt
Error when deploying EC2 on CircleCI
Try running Selenuim 3.141.59 in eclipse (java)
Notes on signal control in Java
Avoid Java encoding errors that occur when running FitGen.exe on Japanese Windows
When seeking multiple in a Java array
I tried running Docker on Windows Server 2019
Kick ShellScript on the server from Java
Error in implementation when implementing Spring validation
Relative path when running tomcat on eclipse
Get history from Zabbix server in Java
Building Java Web Applications on Windows Server 2016
Build Web Application Server (Java) on VPS
Place Java project using database on the server created in VPS so far
407 error when trying to access an HTTPS site in Java via an authenticated proxy
When using a list in Java, java.awt.List comes out and an error occurs
Let's create a TODO application in Java 12 Processing when a request comes in with an unused HttpMethod ・ Processing when an error occurs in the server