Disabling default console handler in Java Logger by codes
The open source packages usu. relies on log4j or Java Logger to print logs, by default the console handler is attached to the logger thus the logs will be printed to console. In certain cases we need to disable this behavior. Usu. it should be done via a propreties file, but for my special case, a logging properties file is not acceptable.
Stackoverflow suggests some solutions like this: http://stackoverflow.com/questions/2533227/how-can-i-disable-the-default-console-handler-while-using-the-java-logging-api
But unfortunately this won't work in my case (might be JDK implementation differences? My JDK is 1.7.0_71 ), after some debugging and tracing I finally find how to do this. Past the codes here for later reference as well as for somebody else (happy googler) who may need this:
{
/**
* Learn in the hard way:
*
* Via debug, we can get root logger using LogManager.getLogger, as rootLogger is the parent
* of all other logers, we can remove the handlers of root logger to avoid log messages be print
* to console
*
* Do not try to call Logger.getGlobal or Logger.getLogger("global"), it is but another child of rootLogger
*/
// Logger globalLogger = Logger.getLogger("global");
// Logger globalLogger = Logger.getGlobal();
Logger globalLogger = LogManager.getLogManager().getLogger("");
Handler[] handlers = globalLogger.getHandlers();
for (Handler handler : handlers) {
globalLogger.removeHandler(handler);
}
}
Disabling default console handler in Java Logger by codes的更多相关文章
- 关于Java Logger类的使用问题 - 内存不释放
原文地址:http://www.ihuxu.com/p/236.html 说明:这是一个利用Java Logger类的示例,完成简单的日志记录功能.代码中有部分类库没有说明,但不影响阅读. 本人技 ...
- 两种“新型”的javaweb后门(jspx和Java Logger)
利用这个可以突破st2下 强制jsp跳转login.jsp 利用jspx解决jsp后缀被限制拿shell - Hack Blog | 黑客博客http://www.hackblog.cn/post ...
- java Logger 的使用与配置
原文来自:http://blog.csdn.net/nash603/article/details/6749914 Logger所对应的属性文件在安装jdk目录下的jre/lib/logging.pr ...
- Java Logger(java日志)
目录 1. 简介2. 安装3. log4j基本概念3.1. Logger3.2. Appender3.2.1. 使用ConsoleAppender3.2.2. 使用FileAppender3.2.3. ...
- 我的日志文件java logger
操作读取日志文件, 1.使用默认的日志文件,并验证默认级别 public void originalConfig() { Logger logger = Logger.getLogger(Logger ...
- Java logger组件:slf4j, jcl, jul, log4j, logback, log4j2
先说结论 建议优先使用logback 或 log4j2.log4j2 不建议和 slf4j 配合使用,因为格式转换会浪费性能. 名词:jcl 和 jul 标题中的 jcl 是 apache Jakar ...
- 转:Java logger组件:slf4j, jcl, jul, log4j, logback, log4j2
先说结论 建议优先使用logback 或 log4j2.log4j2 不建议和 slf4j 配合使用,因为格式转换会浪费性能. 名词:jcl 和 jul 标题中的 jcl 是 apache Jakar ...
- Console Add Item –Java使用eBay API SDK刊登商品 详解
准备工作: 1. 运行Eclipse (或其他Java IDE) 2.创建一个ConsoleAddItem工程(项目) 选JDK 1.5.1.6.1.8等版本,已测试1.6.1.8版本. 3.下载JA ...
- java Logger 类
package org.rx.common; import org.slf4j.LoggerFactory; import java.util.Collections; import java.uti ...
随机推荐
- NashZhou的自我介绍
行业: 电子商务服务业,目前主要是淘宝开放平台,ISV 关键词: 电商,淘宝直通车,关键词广告,自动优化 当前目标: 广告算法 广告主自动优化 希望能在这里结识有共同爱好踏实上进的园友,共同学习,共同 ...
- Daily Scrum 12.16
今日完成任务: 完善了资源界面的UI设计:解决了PDF显示相同的问题:解决了下载时有时找不到文件的问题,因为上届建了upload和resource两个文件夹存文件,写代码时候写乱套了. 明日任务: 黎 ...
- Forwards on Weibo (30)
BFS,题意比较难懂,是求离query L层的总共人数 #include <stdio.h> #include <string.h> #include <iostream ...
- 在rails 4 中 使用 CSV 组件来 把csv文件导入到数据库
class HardWorker include Sidekiq::Worker require 'CSV' def perform(file_path) csv_text = File.read(f ...
- DBNull 与 求和方法
public int CountMoney() { if (!DBNull.Value.Equals(Eval("LawyerMoney")) && !DBNull ...
- new出对象的作用
new调用了构造函数以后 构造函数会返回一个对象的实例
- ajax基础一
AJAX AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. AJA ...
- MVC 单元测试
1.新建mvc项目 添加controller 添加action 或者方法 public ActionResult Index(string str) { ViewBag.Teststr = str; ...
- sql server 分布式查询 和 主从服务器搭建
1. 8K 对应的SQL语句限制 select * from openquery (recei 连接服务器名称 执行的sql 语句放在 SELECT @@SERVERNAME 在本地 ...
- 【CronExpression表达式详解和案例】
1. cron表达式格式: {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)} 2. cron表达式各占位符解释: {秒数} ==> 允许值范围: 0~59 ,不允许 ...