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性能调优实践【代码+JVM 性能提升70%】

    这是我第一次对系统进行调优,涉及代码和JVM层面的调优.如果你能看到最后的话,或许会对你日常的开发有帮助,可以避免像我一样,犯一些低级别的错误.本次调优的代码是埋点系统中的报表分析功能,小公司,开发结 ...

  2. 第一个鸿蒙程序Hello Word

    DevEco Studio介绍 HUAWEI DevEco Studio(以下简称DevEco Studio)是基于IntelliJ IDEA Community开源版本打造,面向华为终端全场景多设备 ...

  3. Google – Reviews

    前言 继上一篇 Facebook – Reviews (Graph API) 后, 这篇继续介绍另一个 Reviews 大平台 Google Reviews. 想通过 API 获取 Google Re ...

  4. RxJS 系列 – Join Creation Operators

    前言 我们一样从简单和常用的入手. 第一篇介绍了 Creation Operators 上一篇介绍了 Filter Operators 这一篇来到 Join Creation Operators. 参 ...

  5. JavaScript Library – Swiper

    前言 官网已经有很好的教程了, 这篇只是记入一些我用过的东西和冷门知识. 参考 官网安装 官网 Demo 安装 yarn add swiper JS import Swiper from 'swipe ...

  6. .NET 开源工业级移动端仓库管理系统

    前言 在工业生产中,定制化的软件对于每个环节都至关重要.对于仓库管理,推荐一款开源的仓库管理系统(WMS)解决方案. 这款基于.NET 框架开发的移动应用,提供了全面的仓库操作.订单处理.主数据管理. ...

  7. Oracle 到 MySQL 函数替换方案汇总

    常用函数和语法转换     NVL函数 Oracle语法: NVL(COUNT(*), 0) MySQL语法: IFNULL(COUNT(*), 0)   转字符串 Oracle语法: to_char ...

  8. [OI] 模拟退火

    模拟退火是一种适合求样本点较大的多峰函数极值的方法. 模拟退火有几个参数:初始温度(\(T_{0}\)),终止温度(\(T_{e}\))和降温参数 \(d\),具体地,模拟退火是让每次的当前温度 \( ...

  9. Nuke导出视频缺失 H.246格式 的解决办法

    同事在使用Nuke导出视频时报错,报错提示:缺失 H.246格式 后来经过我的研究发现,Quicktime Player 在标准安装时,默认不关联一些格式(具体是哪些格式不清楚) Quicktime ...

  10. TS体操类型学习记录

    Easy 1. Easy - 4 - Pick 从类型 T 中选出符合 K 的属性,构造一个新的类型 type MyPick<T, K extends keyof T> = { [key ...