Spring工程 使用日志
Posted hzyhx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring工程 使用日志相关的知识,希望对你有一定的参考价值。
1.spring boot项目默认使用什么技术处理日志?
答:使用了log4j,使用log4j需要的配置:logback.xml配置文件和以下代码
1 private static final Logger log = LoggerFactory.getLogger(UserController.class);
1 log.debug("===============================用户信息:", user); 2 log.info("正常信息"); 3 log.error("错误");
完整代码:
1 package com.hzy.controller; 2 3 import java.util.Collections; 4 import java.util.LinkedHashMap; 5 import java.util.Map; 6 7 import javax.validation.Valid; 8 9 import org.slf4j.Logger; 10 import org.slf4j.LoggerFactory; 11 import org.springframework.beans.factory.annotation.Autowired; 12 import org.springframework.stereotype.Controller; 13 import org.springframework.ui.Model; 14 import org.springframework.validation.BindingResult; 15 import org.springframework.web.bind.annotation.GetMapping; 16 import org.springframework.web.bind.annotation.PathVariable; 17 import org.springframework.web.bind.annotation.PostMapping; 18 19 import com.hzy.dao.UserDao; 20 import com.hzy.model.User; 21 22 @Controller 23 public class UserController 24 25 private static final Logger log = LoggerFactory.getLogger(UserController.class); 26 27 final static Map<String, String> RADIO_GENDER = Collections.unmodifiableMap(new LinkedHashMap<String, String>() 28 private static final long serialVersionUID = 1L; 29 30 put("男", "男"); 31 put("女", "女"); 32 33 ); 34 35 final static Map<String, String> CHECK_COURSE = Collections.unmodifiableMap(new LinkedHashMap<String, String>() 36 private static final long serialVersionUID = 1L; 37 38 39 put("Java程序设计", "java"); 40 put("Spring", "spring"); 41 put("mysql", "mysql"); 42 put("html", "html"); 43 44 ); 45 46 final static Map<String, String> SELECT_HOMETOWN = Collections.unmodifiableMap(new LinkedHashMap<String, String>() 47 private static final long serialVersionUID = 1L; 48 49 put("上海", "上海"); 50 put("广东", "广东"); 51 put("广州", "广州"); 52 53 ); 54 55 final static Map<String, String> SELECT_HOBBIES = Collections.unmodifiableMap(new LinkedHashMap<String, String>() 56 private static final long serialVersionUID = 1L; 57 59 put("听歌", "听歌"); 60 put("玩游戏", "玩游戏"); 61 put("睡觉", "睡觉"); 62 put("看电视", "看电视"); 63 64 ); 65 66 @Autowired 67 private UserDao userDao; 68 69 /** 控制器的方法 **/ 70 71 @GetMapping("/") 72 public String index() 73 return "redirect:/allUser"; 74 75 76 @GetMapping("/allUser") 77 public String allUser(Model model) 78 model.addAttribute("users", userDao.getUsers()); 79 return "list-user"; 80 81 82 @GetMapping("/adduser") 83 public String showAddUserForm(Model model) 84 User user = new User(); 85 user.setId(userDao.getMaxId()); 86 model.addAttribute("user", user); 87 model.addAttribute("radioItems", RADIO_GENDER); 88 model.addAttribute("checkItems", CHECK_COURSE); 89 model.addAttribute("selectItems", SELECT_HOMETOWN); 90 model.addAttribute("multipleItems", SELECT_HOBBIES); 91 return "add-user"; 92 93 94 @PostMapping("/adduser") 95 public String addUser(@Valid User user, BindingResult bindingResult, Model model) 96 97 if (bindingResult.hasErrors()) 98 model.addAttribute("user", user); 99 model.addAttribute("radioItems", RADIO_GENDER); 100 model.addAttribute("checkItems", CHECK_COURSE); 101 model.addAttribute("selectItems", SELECT_HOMETOWN); 102 model.addAttribute("multipleItems", SELECT_HOBBIES); 103 return "add-user"; 104 105 106 userDao.getUsers().add(user); 107 108 System.out.println("-----------------------------------------------------"+user); 109 110 log.debug("===============================用户信息:", user); 111 log.info("正常信息"); 112 log.error("错误"); 113 114 return "redirect:/allUser"; 115 116 117 @GetMapping("/edit/id") // id是占位符 118 public String showUpdateForm(@PathVariable("id") long id, Model model) // @PathVariable 路径变量 119 User user = userDao.getUser(id); 120 model.addAttribute("user", user); 121 return "update-user"; 122 123 124 @PostMapping("/update/id") 125 public String updateUser(@PathVariable("id") long id, @Valid User user, BindingResult bindingResult) 126 if (bindingResult.hasErrors()) 127 return "update-user"; 128 129 userDao.update(user, id); 130 return "redirect:/allUser"; 131 132 133 @GetMapping("/delete/id") 134 public String deleteUser(@PathVariable("id") long id, Model model) 135 userDao.delete(id); 136 return "redirect:/allUser"; 137 138 139
2.日志配置文件logback.xml放在哪里?
答:放在src/main/resources目录下
以上是关于Spring工程 使用日志的主要内容,如果未能解决你的问题,请参考以下文章
springboot配置每天一个日志文件logback-spring.xml
springboot使用spring的aop功能实现操作日志功能