java log4j插件,spring mvc: log4j插件 log日志的输出

准备:

log插件:log4j

log4j

log4j

1.2.17

访问地址: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目录为主)

09068b615e3526ec16b91166abaef4ea.png

配置文件如下:

Archetype Created Web Application

contextConfigLocation

/WEB-INF/applicationContext.xml

encodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

UTF-8

encodingFilter

/*

org.springframework.web.context.ContextLoaderListener

json

org.springframework.web.servlet.DispatcherServlet

1

json

/

applicationContext.xml自动导入bean

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">

json-servlet.xml

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">

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";

}

}

2d1f2d2d587dd24641f6c29a7713bd65.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值