Appearance
JDK Logging
During the process of writing a program, you find that the program results are not as expected. What should you do? Of course, use System.out.println()
to print out some variables during the execution process, observe whether the results of each step are consistent with the code logic, and then modify the code in a targeted manner.
What should I do if the code is changed? Of course, delete the useless System.out.println()
statement.
What should I do if I change the code and problems arise? Plus System.out.println()
.
After doing this several times, everyone soon found that using System.out.println()
was very troublesome.
what to do?
The solution is to use logs.
So what is a log? Logging is Logging, and its purpose is to replace System.out.println()
.
Outputting logs instead of using System.out.println()
has the following benefits:
- You can set the output style to avoid writing
"ERROR: " + var
every time; - You can set the output level to prohibit certain levels of output. For example, only error logs are output;
- Can be redirected to a file so that the log can be viewed after the program has finished running;
- You can control the log level by package name and only output logs for certain packages;
- Can……
In short, there are many benefits.
How to use logs?
Because the Java standard library has a built-in log package java.util.logging
, we can use it directly. Let’s look at a simple example first:
java
import java.util.logging.Level;
import java.util.logging.Logger;
public class Hello {
public static void main(String[] args) {
Logger logger = Logger.getGlobal();
logger.info("start process...");
logger.warning("memory is running out...");
logger.fine("ignored.");
logger.severe("process will be terminated...");
}
}
Run the above code and get output similar to the following:
Mar 02, 2019 6:32:13 PM Hello main
INFO: start process...
Mar 02, 2019 6:32:13 PM Hello main
WARNING: memory is running out...
Mar 02, 2019 6:32:13 PM Hello main
SEVERE: process will be terminated...
From the comparison, it can be seen that the biggest benefit of using logs is that it automatically prints a lot of useful information such as time, calling class, calling method, etc.
After careful observation, we found that only 3 of the 4 logs were printed, and logger.fine()
did not print them. This is because the level of log output can be set. JDK Logging defines 7 log levels, from severe to normal:
- SEVERE
- WARNING
- INFO
- CONFIG
- FINE
- FINER
- FINEST
Because the default level is INFO, logs below the INFO level will not be printed. The advantage of using the log level is that by adjusting the level, you can block a lot of debugging-related log output.
Using the built-in logging in the Java standard library has the following limitations:
The logging system reads the configuration file and completes initialization when the JVM starts. Once the main()
method starts running, the configuration cannot be modified;
Configuration is not convenient and parameters need to be passed when the JVM is started. -Djava.util.logging.config.file=<config-file-name>
.
Therefore, the built-in Logging in the Java standard library is not very widely used. We will introduce the more convenient logging system later.
Practise
Use logger.severe()
to print exceptions:
java
import java.io.UnsupportedEncodingException;
import java.util.logging.Logger;
public class Main {
public static void main(String[] args) {
Logger logger = Logger.getLogger(Main.class.getName());
logger.info("Start process...");
try {
"".getBytes("invalidCharsetName");
} catch (UnsupportedEncodingException e) {
// TODO: Use logger.severe() to print exceptions
}
logger.info("Process end.");
}
}
Summary
The log is to replace System.out.println()
, it can define the format, redirect to a file, etc.;
Logs can be archived to facilitate problem tracking;
Logging can be categorized by levels, making it easy to turn certain levels on or off;
The log can be adjusted according to the configuration file without modifying the code;
The Java standard library provides java.util.logging
to implement the logging function.