0
点赞
收藏
分享

微信扫一扫

Log4j之LogFactor5简介

唯米天空 2023-05-24 阅读 14


[url]http://kin111.blog.51cto.com/738881/158612[/url]
Log4j中的LogFactor5是Log4j的Appender的一个实现。LogFactor5可以图形的形式表示log信息。并且可以对表示的Log信息进行筛选。适合于使用Log4j时查看log的调试。

本文简单介绍LogFactor5的使用方法。
本文示例使用的Log4j版本为:apache-log4j-1.2.15

1。运行LogFactor5的简单方法:
在有log4j-1.2.15.jar文件的目录下从命令行执行:
java -classpath .;log4j-1.2.15.jar org.apache.log4j.lf5.StartLogFactor5即可

表示出一个GUI图形界面。如下图
[img]http://kin111.blog.51cto.com/attachment/200905/200905161242543082026.jpg[/img]


从File菜单选OPEN菜单,从文件选择对话框中选择\apache-log4j-1.2.15\examples\lf5\OpeningLogFiles\sample.log文件后,GUI图形界面内容变化,即可表示出log内容。如下图
[img]http://kin111.blog.51cto.com/attachment/200905/200905161242543107996.jpg[/img]

2。生成可以被LogFactor5GUI图形界面表示的Log方法。
这需要使用Class LF5Appender。这个Class位于org.apache.log4j.lf5包中。
有很多方法可以调用到LF5Appender。
最简单的一种方法如下:
使用DefaultLF5Configurator.configure();
示例代码:

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package examples.lf5.InitUsingDefaultConfigurator;

import org.apache.log4j.Logger;
import org.apache.log4j.NDC;
import org.apache.log4j.lf5.DefaultLF5Configurator;

import java.io.IOException;

/**
 * This class is a simple example of how to configure the LogFactor5
 * logging window using the DefaultLF5Configurator.
 *
 * The DefaultLF5Configurator uses a default configuration file stored
 * in the log4j.jar in order to provide a default configuration for
 * the LF5Appender.
 *
 * @author Brent Sprecher
 */

// Contributed by ThoughtWorks Inc.

public class InitUsingDefaultConfigurator {
    //--------------------------------------------------------------------------
    //   Constants:
    //--------------------------------------------------------------------------

    //--------------------------------------------------------------------------
    //   Protected Variables:
    //--------------------------------------------------------------------------

    //--------------------------------------------------------------------------
    //   Private Variables:
    //--------------------------------------------------------------------------
    private static Logger logger =
            Logger.getLogger(InitUsingDefaultConfigurator.class);

    //--------------------------------------------------------------------------
    //   Constructors:
    //--------------------------------------------------------------------------

    //--------------------------------------------------------------------------
    //   Public Methods:
    //--------------------------------------------------------------------------

    public static void main(String[] args) throws IOException {
        // Configure the LF5Appender using the DefaultLF5Configurator.  This
        // will add the LF5Appender to the root of the Category tree.
        DefaultLF5Configurator.configure();

        // Add an NDC to demonstrate how NDC information is output.
        NDC.push("#23856");
        // Log some information.
        for (int i = 0; i < 10; i++) {
            logger.debug("Hello, my name is Homer Simpson.");
            logger.info("Mmmmmm .... Chocolate.");
            logger.warn("Mmm...forbidden donut.");
        }
        // Clean up NDC
        NDC.pop();
        NDC.remove();

        NDC.push("Another NDC");
        // Log some information.
        logger.fatal("Hello, my name is Bart Simpson.");
        logger.error("Hi diddly ho good neighbour.");
        // Clean up NDC
        NDC.pop();
        NDC.remove();

        // Call methods on both classes.
        InitUsingDefaultConfigurator.foo();
        InnerInitUsingDefaultConfigurator.foo();

        logger.info("Exiting InitUsingDefaultConfigurator.");

    }

    public static void foo() {
        logger.debug("Entered foo in InitUsingDefaultConfigurator class");

        NDC.push("#123456");
        logger.debug("Hello, my name is Marge Simpson.");
        logger.info("D'oh!! A deer! A female deer.");
        // Clean up NDC
        NDC.pop();
        NDC.remove();
    }

    //--------------------------------------------------------------------------
    //   Protected Methods:
    //--------------------------------------------------------------------------

    //--------------------------------------------------------------------------
    //   Private Methods:
    //--------------------------------------------------------------------------

    //--------------------------------------------------------------------------
    //   Nested Top-Level Classes or Interfaces:
    //--------------------------------------------------------------------------

    public static class InnerInitUsingDefaultConfigurator {
        static Logger logger =
                Logger.getLogger(InnerInitUsingDefaultConfigurator.class.getName());

        static void foo() throws IOException {
            // Configure the LF5Appender again. You can call
            // DefaultLF5Configurator.configure() as often as you want
            // without unexpected behavior.
            DefaultLF5Configurator.configure();

            logger.info("Entered foo in InnerInitUsingDefaultConfigurator class.");
        }
    }
}



编译:


javac -classpath .;log4j-1.2.15.jar InitUsingDefaultConfigurator.java

得到InitUsingDefaultConfigurator$InnerInitUsingDefaultConfigurator.class

和InitUsingDefaultConfigurator.class



执行:


java -classpath .;log4j-1.2.15.jar examples.lf5.InitUsingDefaultConfigurator.InitUsingDefaultConfigurator



会启动LogFactor5的图形界面,并把输出Log的内容表示到界面中。如下图:


[img]http://kin111.blog.51cto.com/attachment/200905/200905161242543142205.jpg[/img]



3,LogFactor5的Log的格式


LogFactor5的Log的格式是固定的。如下示例


中括号的规定的标记。



[slf5s.start]26 Jul 2001 15:54:44,673[slf5s.DATE]

DEBUG[slf5s.PRIORITY]

[slf5s.NDC]

main[slf5s.THREAD]

examples.InitUsingMultipleAppenders.InitUsingMultipleAppenders[slf5s.CATEGORY]

examples.InitUsingMultipleAppenders.InitUsingMultipleAppenders.main(InitUsingMultipleAppenders.java:102)[slf5s.LOCATION]

Hello, my name is Homer Simpson.[slf5s.MESSAGE]



4,LogFactor5的package简介



org.apache.log4j.lf5 主要是appender,configurator,level,封装的log记录类,和启动类等等.


org.apache.log4j.lf5.util 主要是LogFactor5的实用类,读资源,处理流,swing的一些共通处理


org.apache.log4j.lf5.viewer 主要是gui的主窗口,各个dialog,表示log用的table,tablemodel等类


org.apache.log4j.lf5.viewer.categoryexplorer 主要是主窗口左边的category流览tree表示swing组件的支持的类


org.apache.log4j.lf5.viewer.configure 主要是处理最近打开文件历史,配置保存读取类


举报

相关推荐

0 条评论