First step: Add Pom file dependencies
<!--log4j--- <dependency> <groupId>org.springframework.boot</groupId> < Artifactid>spring-boot-starter-log4j</artifactid> <version>1.3.8.RELEASE</version> </dependency> <dependency> <groupId>commons-lang</groupId> < artifactid>commons-lang</artifactid> <version>2.6</version> </dependency>
Step Two: Create a new Log4j.xml file under the Resources directory
Log4j.xml:
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE log4j:configuration SYSTEM "Http://toolkit.alibaba-inc.com/dtd/log4j/log4j.dtd" ><log4j: Configuration xmlns:log4j= "http://jakarta.apache.org/log4j/" > <appender name= "Console-appender"class= "Org.apache.log4j.ConsoleAppender" > <param name= "Encoding" value= "UTF-8"/> <param name= "Thresh Old "value=" Debug "></param> <layoutclass= "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" value= "%d%-5p%-32t-%m%n"/> </layout> </appender> <appender name= "Default-appender"class= "Org.apache.log4j.DailyRollingFileAppender" > <param name= "File" value= "./logs/default.log"/> <p Aram name= "Append" value= "true"/> <param name= "encoding" value= "UTF-8"/> <param name= "threshold" value= "Debug"/> <layoutclass= "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" value= "%d%-5p%-32t-%m%n"/> </layout> </appender> <appender name= "Error-appender"class= "Org.apache.log4j.DailyRollingFileAppender" > <param name= "File" value= "./logs/error.log"/> <par Am Name= "append" value= "true"/> <param name= "encoding" value= "UTF-8"/> <param name= "threshold" V alue= "Error"/> <layoutclass= "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" value= "%d%-5p%-32t-%m%n"/> </layout> </appender> <appender name= "Hospital-web"class= "Org.apache.log4j.DailyRollingFileAppender" > <param name= "File" value= "./logs/web.log"/> <para M name= "append" value= "true"/> <param name= "encoding" value= "UTF-8"/> <param name= "threshold" value= "Debug"/> <layoutclass= "Org.apache.log4j.PatternLayout" > <param name= "Conversionpattern" value= "%d%-5p%-32t-%m%n"/> </layout> </appender> <logger name= "Yxm.zyf.love" additivity= "false" > <level val ue= "Debug"/> <appender-ref ref= "Hospital-web"/> <appender-ref ref= "Error-appender"/> &L t;/logger> <!--<logger name= "Yxm.zyf.love" additivity= "false" > <level value= "Debug"/> <appender-ref ref= "Console-appender"/> <appender-ref ref= "Error-appender"/> <appender-ref ref= "Default-appender"/> </logger> <root> <level value= "Debug"/> <app Ender-ref ref= "Console-appender"/> <appender-ref ref= "Error-appender"/> <appender-ref ref= "DEF Ault-appender "/> </root></log4j:configuration>
The third step: give you a tool class reference, encapsulated 4 levels of log
ImportJava.text.MessageFormat;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportOrg.apache.log4j.Logger;/** * @authorZhangyufeng **/ Public classLoggerutil {Private Static FinalString trace_marker = "[traceid:{0}]"; /*** Generate trace template * *@paramTemplate *@paramparameters *@return */ Public Staticstring GetMessage (string template, Object ... parameters) {returnMessageformat.format (Trace_marker, Gettraceid ()) +Messageformat.format (template, parameters); } /*** <p> * Generate Debug level log * </p> * <p> * Generate debug level log in log template with parameters as {Number according to log template with parameters and parameter collection } represents a log point to be replaced with a variable, such as A={0}, which represents a parameter in the parameter collection with index 0 replaced by {0} * </p> *@paramLogger * Logger quote *@paramTemplate * Log templates with parameters *@paramParameters * Parameter Collection*/ Public Static voidDebug (Logger Logger, String template, Object ... parameters) {if(logger.isdebugenabled ()) {Logger.debug (GetMessage (template, parameters)); } } /*** <p> * Generate Debug level log * </p> * <p> * Generate info level log in log template with parameters as {number} based on log template and parameter collection with parameters Represents a log point to be replaced with a variable, such as A={0}, which represents a parameter in the parameter collection with index 0 replaced by {0} * </p> *@paramLogger * Logger quote *@paramTemplate * Log templates with parameters *@paramParameters * Parameter Collection*/ Public Static voidinfo (Logger Logger, String template, Object ... parameters) {if(logger.isinfoenabled ()) {Logger.info (GetMessage (template, parameters)); } } /*** <p> * Generate Debug level log * </p> * <p> * Generate warn level log in log template with parameters as {number} based on log template and parameter collection with parameters Represents a log point to be replaced with a variable, such as A={0}, which represents a parameter in the parameter collection with index 0 replaced by {0} * </p> *@paramLogger * Logger quote *@paramTemplate * Log templates with parameters *@paramParameters * Parameter Collection*/ Public Static voidwarn (Logger Logger, String template, Object ... parameters) {Logger.warn (GetMessage (template, parameters)); } /*** <p> * Generate Debug level log * </p> * <p> * Generate error level log based on log template with parameters and parameter collection * Log template with parameters {Number } represents a log point to be replaced with a variable, such as A={0}, which represents a parameter in the parameter collection with index 0 replaced by {0} * </p> *@paramLogger * Logger quote *@paramTemplate * Log templates with parameters *@paramParameters * Parameter Collection*/ Public Static voiderror (Logger Logger, String template, Object ... parameters) {logger.error (GetMessage (template, parameters)); } /*** <p> * Exception Log * </p> * <p> * Based on log template with parameters and parameter collection, generate Warn level log * with parameters in log template with { The number} represents a log point to be replaced with a variable, such as A={0}, which represents the substitution of {0} with the parameter in the parameter collection for index 0 * </p> *@paramLogger * Logger quote *@paramTemplate * Log templates with parameters *@paramParameters * Parameter Collection*/ Public Static voidwarn (throwable E, Logger Logger, String template, Object ... parameters) {Logger.warn (getMessage (template, par ameters), E); } /*** <p> * Exception Log * </p> * <p> * Generate the error level log in the log template with parameters according to the log template and parameter collection with parameters to {Number} indicates a log point to be replaced with a variable, such as A={0}, which represents the substitution of {0} with a parameter of index 0 in the parameter collection * </p> *@paramLogger * Logger quote *@paramTemplate * Log templates with parameters *@paramParameters * Parameter Collection*/ Public Static voiderror (Throwable e, Logger Logger, String template, Object ... parameters) {logger.error (getMessage (template, p arameters), E); } /*** Traceid * *@return */ Private StaticString Gettraceid () {SimpleDateFormat SDF=NewSimpleDateFormat ("Yyyymmddhhmmss");//Formatting ObjectsString tempd = Sdf.format (NewDate ()); returnTempd+ ((int) (Math.random () * 900) + 100); }}
Fourth: Create a Log Object
ImportJava.util.Map;Importjavax.servlet.http.HttpServletRequest;ImportOrg.apache.log4j.Logger;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.web.bind.annotation.RequestBody;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportOrg.springframework.web.bind.annotation.RestController;ImportCom.alibaba.fastjson.JSON;ImportYxm.zyf.love.domian.RoomDO;ImportYxm.zyf.love.mapper.RoomMapper;ImportYxm.zyf.love.utils.LoggerUtil;ImportYxm.zyf.love.utils.StringUtil;ImportYxm.zyf.love.utils.URLencord;Importyxm.zyf.love.vo.payVo; @RestController Public classHospitalpay {Private Final StaticLogger Logger = Logger.getlogger (hospitalpay.class);//initialize a log object to a class@AutowiredPrivateRoommapper Roommapper; @ResponseBody @RequestMapping (Value= "Pay") Public voidPay (httpservletrequest req, @RequestBody String a) {System.out.println (Req.getattribute ("Industry")); System.out.println (Req.getparameter ("Industry")); Map<string, object> map = Stringutil.transstringtomap (Urlencord.urldecoderstring (a), "&", "="); for(String key:map.keySet ()) {System.out.println ("Key:" + key + ", Value:" +Map.get (key)); } System.out.println (map); System.out.println (Json.parseobject (json.tojsonstring (map)); Payvo Payvo=json.parseobject (json.tojsonstring (map), Payvo.class); System.out.println (PAYVO); } @ResponseBody @RequestMapping (value= "the") PublicRoomdo () {Roomdo Roomdo= Roommapper.selectbyprimarykey (1); Loggerutil.info (Logger,"Ginseng roomdo:{0}", Roomdo);//Print log at info levelSystem.out.println (ROOMDO); returnRoomdo; }}
Fifth step: Run the Print log
Generates 3 log files, only 3 files are configured, and you need to add them yourself
Print log information:
Springboot Add log4j Log configuration log4j.xml generate log file