Adhering to a certain kind of practice I decided today not to Logback, but to Java logging, now summarize the results of the research:
Log level
There are seven log levels, Severe,warning,info,fine,finer,finest,config, seven of which can be used as method names, and enumerations in the enumeration class level. The use method is simple, similar to Logger.info (String msg) in the form of use, but next there is a comparison pit: The default can only print Info,warning,severe these three levels of the log, if you want to use a lower level of the log, For example, when you look at the information in debug, complex configuration is necessary.
If you simply output the log to a file without using the console, it is easier to use the Setlevel method directly. However, if you want to use the console, be aware that the log can be displayed in the console not only depending on whether it is printed at this level, but also on whether the level of logs in the global logger configuration is allowed to appear in the console. Therefore, to view low-level logs in the console, you need to manually overwrite the Loggermanager configuration class.
Load configuration file
Java8 previous global profile in Jre/lib, moved from JAVA9 to jre/conf, but I highly recommend that you change the configuration here, I recommend that you load it separately, only for your app to take effect. Here's how springboot loads the global log profile, and I put the custom configuration file in the/resources/logging.properties
@Component class logconfig { @PostConstruct pub Lic void setlogmanager () {Logmanager Logmanager = Logmanager . getlogmanager (); try {Logmanager. readconfiguration (new classpathresource ( "logging.properties" ). getinputstream ()); } catch (Exception e) {//add your convenient way to display the error. } }}
Here I use the spring-provided Classpathresource class, which is very handy, it can point directly to the resources default directory, and provides a variety of scenarios such as converting to a file/io stream to make it easier for your class to read. Another Logmanager is a singleton, and once you change the config global log, your custom configuration will be loaded. Don't throw up the groove Why should I use Java's own log class in Springboot, because the project is a combination of multiple projects. There are many ways to read this without Classpathresource classes, such as file manipulation, no longer described.
Configuration file
After that, we can write the configuration file in the corresponding directory:
#handlerhandlers= Java.util.logging.ConsoleHandler#全局日志等级. level= INFO#输出日志文件名格式Java.util.logging.FileHandler.pattern =%h/java%u.log#日志文件长度限制Java.util.logging.FileHandler.limit = 50000#日志数目Java.util.logging.FileHandler.count = 1#锁的数量Java.util.logging.FileHandler.maxLocks = 100#日志默认形式Java.util.logging.FileHandler.formatter = Java.util.logging.XMLFormatter#控制台显示的日志等级Java.util.logging.ConsoleHandler.level = FINE#选择日志输出格式Java.util.logging.ConsoleHandler.formatter = Java.util.logging.SimpleFormatter#对格式进行自定义java.util.logging.simpleformatter.format=[%1$TF%1$tt] [%4$-7s]%5$s%n#对特殊类的等级特殊说明Com.changhe.utils.logutils.level=fine
For example here, I did not set handler, set the global display info level, my debug with logger display to fine level (method is to use the class name as the name of the logger hierarchy to fine, reference code), and allow the console output fine level messages, and customize the log output format, there is about the format definition of the wording, you can go to Java formatter class, which is often used to define the time format, and more complex.
A logger acquisition method of a singleton pattern
Finally provide me with the A Hungry man mode of the log class Singleton, considering the log to record the class name, this method is not really suitable, but because I wrote this logger only responsible for the custom output, so you can use this convenient and reduce memory consumption practices:
import java.util.logging.*;publicclass LogUtils { privatestaticnewLogUtils(); publicstaticgetLogUtils() { return logUtils; } private Logger logger; privateLogUtils() { logger = Logger.getLogger(LogUtils.class.getName()); } publicgetLogger() { return logger; }}
Use of java.util.logging