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. electron-builder打包配置说明

    发现问题 通过vue建立的项目使用electron-builder打包(不会eletron打包vue项目的看这里)出exe后发现名字就直接是项目文件夹的名字,但此时想自定义汉字名称,通过尝试直接修改p ...

  2. Pointer Event Api-整合鼠标事件、触摸和触控笔事件

    Pointer Events API 是Hmtl5的事件规范之一,它主要目的是用来将鼠标(Mouse).触摸(touch)和触控笔(pen)三种事件整合为统一的API. Pointer Event P ...

  3. Angular 18+ 高级教程 – Angular CLI

    前言 这篇会列出我开发中常用的 command. 并给予一些简单的说明 Command Format 先了解一下几个简单的 command 格式: 缩写 shortform 这个是完整版 ng gen ...

  4. CSS & JS Effect – Blue Tick Avatar

    效果 难点 难题只有一个, 那就是如何把 blue tick image 定位当 avatar 的右下角. HTML <div class="avatar-wrapper"& ...

  5. RxJS 系列 – 实战练习

    前言 这篇主要是给一些简单例子, 从中体会 RxJS 在管理上的思路. Slide Down Effect with Dynamic Content 我在这篇 CSS & JS Effect ...

  6. P1543 [POI2004] SZP 题解

    P1543 [POI2004] SZP 题解 传送门. 题目简述 有 \(n\) 个人,每个人都会监视另一个人,要求选出尽可能多的同学,使得选出的每一名同学都必定会被监视到.且选出的同学不可再监视其他 ...

  7. PyCharm 的一些基本设置&&常用插件&&快捷键

    PyCharm一些基本设置   1.主题色彩   2.添加设置:Ctrl+鼠标滚轮上下调节字体大小           3. 中文语言包   4.翻译插件 5.快捷键

  8. 一,初始 MyBatis-Plus

    一,初始 MyBatis-Plus @ 目录 一,初始 MyBatis-Plus 1. MyBatis-Plus 的概述 2. 入门配置第一个 MyBatis-Plus 案例 3. 补充说明: 3.1 ...

  9. Windows自动更新hosts(bat脚本方式)

    为了解决无法打开 github 网页的问题,才有了这个自动更新hosts 的命令脚本 hosts 里的内容会每日更新,内容从这里拿 文件:https://raw.hellogithub.com/hos ...

  10. LeetCode 1316. Distinct Echo Substrings (RK哈希)

    题意: 给一个字符串 寻找字符串为(a+a)格式的子串有多少.a+a 格式字符串比如 abcabc, ee 等. 首先O(N^2)枚举子串,然后通过哈希在O(1)复杂度判断子串是否符合要求. RK哈希 ...