resin记录日志log4j配置

在resin的配置文件\resin-pro-3.1.8\conf\morden.conf加上下面这几行:

<stderr-log  path="${resin.home}/log/stderr.log" timestamp="[%m-%d %H:%M:%S.%s] " rollover-size="100mb" rollover-period="1W"   rollover-count="10" />
   
    <stdout-log  path="${resin.home}/log/stdout.log" timestamp="[%m-%d %H:%M:%S.%s] " rollover-size="100mb" rollover-period="1W" rollover-count="10" />
   
   <log name="" level="info" path="${resin.home}/log/jdk-out.log" timestamp="[%m-%d %H:%M:%S.%s] {%{thread}} " rollover-size="100mb" rollover-period="1W" rollover-count="10"/>

 

程序启动之后,在resion的日志文件夹resin-pro-3.1.8\log下面,会有相应的日志文件:

jdk-out.log

stderr.log

stdout.log

上面配置文件的第三个日志配置,有个level的属性可以配置日志输出级别

 

 

注意:一般maven构建项目,log4j.properties要放在类路径下面,如:

 

程序中记录日志直接用

private final Logger logger = LoggerFactory.getLogger(getClass());

来获取logger对象,调用info或者方法记录日志,如:

public class UserDaoListener implements DaoListener {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	GeliDao geliDao;
	
	@SuppressWarnings("rawtypes")
	@Override
	public Class getEntityType() {
		return User.class;
	}

	@Override
	public void onEvent(Event event, Object entity) {
		User user = (User) entity;
		switch (event) {
		case PostSelect:
			logger.info("passportId: {}, userId: {} nickname:{}", new Object[] {user.getPassportId(), user.getUserId(), user.getNickName()});
			if(user.getAccount()!=null){
				user.setNickName(user.getAccount().getNickName());
			}
			geliDao.setCache(CacheKey.PASSPORT_PREFIX +user.getPassportId(), geliDao.object2String(user));
			break;
		case PostUpdate:
			geliDao.deleteCache(CacheKey.PASSPORT_PREFIX +user.getPassportId());
			break;
		default:
			break;
		}
	}

}

 
在调用 LoggerFactory.getLogger(getClass())方法时,程序默认会在类路径下加载log4j.properties文件, 如果没有这个文件,虽然程序没有报错,但stderr.log日志输出异常:

[04-09 16:26:24.155] log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
[04-09 16:26:24.156] log4j:WARN Please initialize the log4j system properly.
[04-09 16:26:24.156] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
[04-09 16:27:10.299] log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
[04-09 16:27:10.299] log4j:WARN Please initialize the log4j system properly.
[04-09 16:27:10.299] log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
具体可以看http://logging.apache.org/log4j/1.2/faq.html#noconfig这里。

 

log4j.properties的内容如下

log4j.rootLogger=INFO, stdout
######################### logger ##############################

log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.conversionPattern = %d [%t] %-5p %c - %m%n

#log4j.logger.com.danga.MemCached.MemCachedClient=WARN

log4j.appender.env = org.apache.log4j.ConsoleAppender
log4j.appender.env.layout = org.apache.log4j.PatternLayout
log4j.appender.env.layout.conversionPattern = %d %m%n

log4j.additivity.org.gelivable.web.EnvFilter = false
log4j.logger.org.gelivable.web.EnvFilter = DEBUG, env

 

如果在resin配置文件配置了上面的日志输出文件,尽管log4j配置了log4j.appender.stdout = org.apache.log4j.ConsoleAppender,在resin启动的时候,控制台是没有输出相关日志信息,只在resin-pro-3.1.8\log日志文件输出。要想在控制台看到输出的日志信息,就要把上面resin配置文件的日志配置去掉,如去掉下面这两个日志配置:

<stderr-log  path="${resin.home}/log/stderr.log" timestamp="[%m-%d %H:%M:%S.%s] " rollover-size="100mb" rollover-period="1W"   rollover-count="10" />
    <stdout-log  path="${resin.home}/log/stdout.log" timestamp="[%m-%d %H:%M:%S.%s] " rollover-size="100mb" rollover-period="1W" rollover-count="10" />

 

 不能同时在日志文件和控制台看到输出日志,这个可能与resin服务器有关,具体原因暂时不明.

 

 

 

 

 

 

 

你可能感兴趣的:(log4j)