spring mvc: log4j插件 log日志的输出
准备:
log插件:log4j
<!-- log日志插件 -->
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
访问地址:http://localhost:8080/gugua3/hello/log
包:json
需要在/WEB-INF/classes/文件夹下,新建log4j的配置
log4j.properties
# Root logger option
log4j.rootLogger=DEBUG, stdout, file # Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n # Redirect log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender #outputs to Tomcat home
log4j.appender.file.File=${catalina.home}/logs/myapp.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
那么生成的日志文件 位置为: apache-tomcat-7.0.82\logs\目录下(以你的tomcat目录为主)

配置文件如下:
<web-app>
<display-name>Archetype Created Web Application</display-name> <!--配置文件路径-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <!-- 字符过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 监听转发 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>json</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>json</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> </web-app>
applicationContext.xml自动导入bean
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 默认:注解映射支持 -->
<mvc:annotation-driven/>
<!-- 静态资源配置 -->
<mvc:resources location="/pages/**" mapping="/pages/"/> <!-- 自动扫描包名,controller -->
<context:component-scan base-package="json"/> </beans>
json-servlet.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean视图解析 -->
<!--
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean id="rssViewer" class="json.RSSFeedViewer"/>
--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean> </beans>
HelloController.java
package json; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.ModelMap;
import org.apache.log4j.Logger; @Controller
public class HelloController { private static final Logger LOGGER = Logger.getLogger(HelloController.class); @RequestMapping(value="/hello/log", method=RequestMethod.GET)
public String log(ModelMap model)
{
LOGGER.info("printHello started"); if(LOGGER.isDebugEnabled())
{
LOGGER.debug("Inside: printHello");
} //logs exception
LOGGER.error("Logging a sample exception", new Exception("Testing"));
//LOGGER.error("Logging a sample exception", new Exception("Testing")); model.addAttribute("message", "hello Spring mvc Framework");
LOGGER.info("pringHello ended"); return "hello_log";
}
}

spring mvc: log4j插件 log日志的输出的更多相关文章
- java log日志的输出。
在Spring框架中添加日志功能: pom.xml <dependency> <groupId>log4j</groupId> <artifactId> ...
- Spring Boot log4j多环境日志级别的控制
之前介绍了在<Spring boot中使用log4j>,仅通过log4j.properties对日志级别进行控制,对于需要多环境部署的环境不是很方便,可能我们在开发环境大部分模块需要采用D ...
- ActiveMQ学习总结(6)——ActiveMQ集成Spring和Log4j实现异步日志
我的团队和我正在创建一个由一组RESTful JSON服务组成的服务平台,该平台中的每个服务在平台中的作用就是分别提供一些独特的功能和/或数据.由于平台中产生的日志四散各处,所以我们想,要是能将这些日 ...
- 采用Spring AOP+Log4j记录项目日志
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6567672.html 项目日志记录是项目开发.运营必不可少的内容,有了它可以对系统有整体的把控,出现任何问题 ...
- Spring AOP+Log4j记录项目日志
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6567672.html 项目日志记录是项目开发.运营必不可少的内容,有了它可以对系统有整体的把控,出现任何问题 ...
- ios 将Log日志重定向输出到文件中保存
对于真机,日志没法保存,不好分析问题.所以有必要将日志保存到应用的Docunment目录下,并设置成共享文件,这样才能取出分析. 首先是日志输出,分为c的printf和标准的NSLog输出,print ...
- spring AOP知识点总结以及日志的输出
AOP的作用就是在基于OCP在不改变原有系统核心业务代码的基础上动态添加一些扩展功能.通常应用于日志的处理,事务处理,权限处理,缓存处理等等 首先,使用AOP需要添加的依赖有:spring-conte ...
- Spring Boot log4j实现把日志存入mongodb
准备工作 1.自定义appender的实现 log4j提供的输出器实现自Appender接口,要自定义appender输出到MongoDB,只需要继承AppenderSkeleton类,并实现几个方法 ...
- log4j配置单独日志文件输出
log4j.logger.batteryHistory=ERROR,BD log4j.appender.BD=org.apache.log4j.FileAppender log4j.appender. ...
随机推荐
- Visual Studio 起始页面关闭新闻等
[工具]->[选项]->[环境]->[启动] 将“下载内容的时间间隔”一项的勾选去掉,然后确定保存.这样,就大功告成啦
- Spring MVC学习(五)---ModelAndView没有明显申明name
看图不解释: 对于这种写法: new ModelAndView().addObject(XXX)
- PHP 基础篇 - PHP 正则官方文档汇总
一.PCRE 正则语法 下面是 PHP 的 PCRE 正则语法(模式语法)相关文档,详情请查阅相关链接: 简介 分隔符 元字符 转义序列(反斜线) Unicode字符属性 锚 句点 字符类(方括号) ...
- centos7命令3
查看监听的端口 netstat -lntp 检查端口被哪个进程占用 netstat -lnp|grep 8080 查看当前文件夹大小 du -sh 查看当前文件夹各目录大小 du -sh ./* 查看 ...
- 手机端1px细线公共类
手机端1px细线公共类 .borderBottom1px{ position: relative; } .borderBottom1px:after{ content: ""; p ...
- sgu 100 A+B 解题报告及测试数据
100.A+B time limit per test: 0.25 sec. memory limit per test: 65536 KB 题解:上手题,不解释. 直接上代码: #include & ...
- Hexo博客配置笔记
安装Hexo npm install hexo-cli -g cd /blog hexo init 安装next主题 git clone https://github.com/iissnan/hexo ...
- GIT使用—补丁与钩子
一.补丁 生成补丁 [root@localhost buding]# echo B > file;git add file;git commit -m "B" [master ...
- 设置 vadio 和checkbox是否选中
1.js方案 <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title>te ...
- ScriptToApp FAQ
Q: What should i do when the system alert is displayed ? A: To make the generated app work, you ne ...