When "Run on server [^ 1]", when using Logger
to output to the console, the" Configuration "level and below were not displayed.
--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
.
Eclipse 2019-03 (4.11.0) Tomcat 9 Java 11 macOS 10.13.6
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.
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.
[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