java中logger关闭log_Java Logger.log方法代码示例

import org.apache.log4j.Logger; //导入方法依赖的package包/类

@Test

public void testLayout() throws IOException {

configureSource();

props.put("log4j.appender.out2.layout", "org.apache.log4j.PatternLayout");

props.put("log4j.appender.out2.layout.ConversionPattern",

"%-5p [%t]: %m%n");

PropertyConfigurator.configure(props);

Logger logger = LogManager.getLogger(TestLog4jAppender.class);

Thread.currentThread().setName("Log4jAppenderTest");

for (int count = 0; count <= 100; count++) {

/*

* Log4j internally defines levels as multiples of 10000. So if we

* create levels directly using count, the level will be set as the

* default.

*/

int level = ((count % 5) + 1) * 10000;

String msg = "This is log message number" + String.valueOf(count);

logger.log(Level.toLevel(level), msg);

Transaction transaction = ch.getTransaction();

transaction.begin();

Event event = ch.take();

Assert.assertNotNull(event);

StringBuilder builder = new StringBuilder();

builder.append("[").append("Log4jAppenderTest").append("]: ")

.append(msg);

//INFO seems to insert an extra space, so lets split the string.

String eventBody = new String(event.getBody(), "UTF-8");

String eventLevel = eventBody.split("\\s+")[0];

Assert.assertEquals(Level.toLevel(level).toString(), eventLevel);

Assert.assertEquals(

new String(event.getBody(), "UTF8").trim()

.substring(eventLevel.length()).trim(), builder.toString());

Map hdrs = event.getHeaders();

Assert.assertNotNull(hdrs.get(Log4jAvroHeaders.TIMESTAMP.toString()));

Assert.assertEquals(Level.toLevel(level),

Level.toLevel(Integer.parseInt(hdrs.get(Log4jAvroHeaders.LOG_LEVEL

.toString()))));

Assert.assertEquals(logger.getName(),

hdrs.get(Log4jAvroHeaders.LOGGER_NAME.toString()));

Assert.assertEquals("UTF8",

hdrs.get(Log4jAvroHeaders.MESSAGE_ENCODING.toString()));

transaction.commit();

transaction.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值