The log4j can be configured both programmatically and externally using special configuration files. External configuration is most preferred, because
to take effect it doesn’t require change in application code, recompilation, or redeployment. Configuration files can be XML files or Java property files that can be created and edited using any text editor or XML editor, respectively.

1. Programmatically configure log4j

This example is from this site, the code is messy, so why not make it clean.

package Test;
 
import org.apache.log4j.*;
 
public class TestLog {
 
/*
* get a static logger instance with name TestLog
*/

static Logger myLogger = Logger.getLogger(TestLog.class.getName());
Appender myAppender;
SimpleLayout myLayout;
 
/* Constructor */
public TestLog() {
 
/*
* Set logger priority level programmatically. Though this is better done externally
*/

myLogger.setLevel(Level.ALL);
 
/*
* Instantiate a layout and an appender, assign layout to appender
* programmatically
*/

myLayout = new SimpleLayout();
myAppender = new ConsoleAppender(myLayout); // Appender is Interface
 
/* Assign appender to the logger programmatically */
myLogger.addAppender(myAppender);
 
} // end constructor
 
public void do_something(int a, float b) {
 
/*
* This log request enabled and log statement logged, since INFO = INFO
*/

myLogger.info("The values of parameters passed to method do_something are: "+ a + ", " + b);
 
/* this log request is not enabled, since DEBUG < INFO */ myLogger.debug("Operation performed successfully"); Object x=null; if (x == null) { /* * this log request is enabled and log statement logged, since ERROR * > INFO
*/

myLogger.error("Value of X is null");
 
}
} // end do_something()
public static void main(String []args){
new TestLog().do_something(1, 3);
}
} // end class MyClass

Here is the snapshot of output:

2. Configure Log4j externally

Create a file named “log4j.properties” in the “src” directory of your project.

# Define the root logger with appender file
log = /home/ryan/Desktop/log4j
log4j.rootLogger = DEBUG, FILE # Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out # Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n

Here is the java code.

package Test;
import org.apache.log4j.Logger;
public class TestLogger {
static Logger myLogger = Logger.getLogger(TestLog.class.getName());
 
public static void main(String[] args) {
myLogger.info("testing");
myLogger.warn("warning testing");
myLogger.error("this is an error");
}
 
}

The log will be printed on the log.out file in /home/ryan/Desktop/log4j directory.

An Entry Example of Log4j的更多相关文章

  1. lucene构建restful风格的简单搜索引擎服务

    来自于本人博客: lucene构建restful风格的简单搜索引擎服务 本人的博客如今也要改成使用lucene进行全文检索的功能,因此在这里把代码贴出来与大家分享 一,文件夹结构: 二,配置文件: 总 ...

  2. Jetty启动报Error scanning entry META-INF/versions/9/org/apache/logging/log4j/util/ProcessIdUtil.class

    近日在项目中集成Elasticsearch后,Jetty启动报错. 错误日志如下: Suppressed: |java.lang.RuntimeException: Error scanning en ...

  3. log4j 文档

    log4j中文文档  中文详细教程 log4j中文文档   这篇文章描述了Log4j的API.独一无二的特色和设计原理.Log4j是一个聚集了许多作者劳动成果的开源软件项目.它允许开发人眼以任意的粒度 ...

  4. Log4J基础详解及示例大全

    去年这个时候,为做软件工程的大作业就详细学过Log4J的用法了,时隔一年想要在新的项目中好好使用一下的时候,发现几乎全忘了,悲催啊-- 再上网查资料,总是不能找到一篇符合我的口味,拿来就能轻松上手,方 ...

  5. Log4j的ConversionPattern无缝适配到Logback

    为了能将log4j的ConversionPattern无缝应用到logback上来,需要对两个Conversion做适配,具体可以参考:Log4j 与 Logback的ConversionPatter ...

  6. log4j使用教程详解(怎么使用log4j2)

    1. 去官方下载log4j 2,导入jar包,基本上你只需要导入下面两个jar包就可以了(xx是乱七八糟的版本号): log4j-core-xx.jar log4j-api-xx.jar 2. 导入到 ...

  7. Log4j 2使用教程

    Log4j 2的好处就不和大家说了,如果你搜了2,说明你对他已经有一定的了解,并且想用它,所以这里直接就上手了. 1. 去官方下载log4j 2,导入jar包,基本上你只需要导入下面两个jar包就可以 ...

  8. log4j 总结 精华

    去年这个时候,为做软件工程的大作业就详细学过Log4J的用法了,时隔一年想要在新的项目中好好使用一下的时候,发现几乎全忘了,悲催啊…… 再上网查资料,总是不能找到一篇符合我的口味,拿来就能轻松上手,方 ...

  9. log4j.properties文件配置--官方文档

    Default Initialization Procedure The log4j library does not make any assumptions about its environme ...

  10. Log4j 2.0 使用说明

      原文地址:http://blog.csdn.net/welcome000yy/article/details/7962447 Log4j 2.0 使用说明(1) 之HelloWorld 最近刚接触 ...

随机推荐

  1. Java Swing Loading转圈的进度提示框

    Java Swing Loading转圈的进度提示框 具体只需要两个类 AnimatedPanel.java InfiniteProgressPanel.java 前因:我们开发的web应用,有个奇葩 ...

  2. js玩儿爬虫

    前言 提到爬虫可能大多都会想到python,其实爬虫的实现并不限制任何语言. 下面我们就使用js来实现,后端为express,前端为vue3. 实现功能 话不多说,先看结果: 这是项目链接:https ...

  3. 【转】ElasticSearch报错FORBIDDEN/12/index read-only / allow delete (api) ,read_only_allow_delete 设置 windows

    仅供自己记录使用,原文链接:ElasticSearch报错FORBIDDEN/12/index read-only / allow delete (api)_sinat_22387459的博客-CSD ...

  4. Wordpress 建立公司网站

    1. 先安装好wordpress wordpress 6.4.2-php8.1-fpm-alpine php8.1 Login to wordpress http://www.hei-ya.com/w ...

  5. 鸿蒙系统(HarmonyOS)全局弹窗实现

    全局弹窗相对于自定义弹窗有以下优点: 封装更彻底,一行代码就能调用 跟组件耦合度低,只需要传入组件的UIContext对象,不需要跟自定义弹窗一样需要在组件内部实例化CustomDialogContr ...

  6. 消毒 url 和 html (url encode and sanitizer html )

    更新: 2020-06-24 FromRoute vs FromQuery decode FromRoute 是不会 auto decode 的, query string 就会 这个是微软默认的设置 ...

  7. Figma 学习笔记 – Frame

    Frame = <div> Frame 就类似 HTML 中的 div, 它和形状 rectangle 特性上蛮相识的, 但是使用场景其实差很多, 所以不要搞错哦. (除了图片很少会用到 ...

  8. Spring —— IoC入门案例

    IoC入门案例   思路分析:     1.管理什么?(Service与Dao)     2.如何将被管理的对象告知IoC容器?(配置)     3.被管理的对象交给IoC容器,如何获取到IoC容器? ...

  9. 反问面试官3个ThreadLocal的问题

    ThreadLocal,一个Java人面试绕不开的话题,我也很奇怪为什么那些面试官很喜欢问这个,也不知道他们自己有没有搞清楚. 接下来,我想先说说ThreadLocal的用法和使用场景,然后反问面试官 ...

  10. SqlEs-像使用数据库一样使用Elasticsearch

    SqlEs SqlEs是Elasticsearch的客户端JDBC驱动程序,支持采用sql语法操作Elasticsearch.SqlEs构建在RestHighLevelClient,屏蔽了RestHi ...