English 中文(简体)
使用已配置对log4j 的日志跟踪类内日志
原标题:Tracking the class inside logs with log4j configured
  • 时间:2012-05-24 16:40:52
  •  标签:
  • java
  • log4j

我有一个问题 可以说我已经发展了一个简单的班级...

    class Simple
    {
public static void main(String args[])
    {
    System.out.println("I am a good bouy");
    }
    }

现在,在我的应用程序的前一级, 请告诉我还有50个其它类也执行过, 我配置了Log4j 来追踪日志记录, 现在在日志中, 我只想知道我的上一级何时执行, 那么我应该在这个类中输入什么, 这样我就可以追踪日志, 并开始知道, 此时我的上一级被执行过. it is log.info (“ 侧边简单类 ” ) ;

问题回答

在此示例有用之前, 遗漏所有其它您需要解析的事项, 您只需要修改日志配置, 并在您打印日志消息时打印此类和行 。 @ info: whatsthis

我不想说是因为它已经过时了,但RTFM是这里最好的方法。这个页面将告诉大家,你最需要的就是开始:

http://logbook.apache.org/log4j/1.2/manual.html

您只需要为您的日志输入一个特定的转换 Patter 配置选项, 每次登录信件时, 它会记录类名甚至行信息 。 以下是该页面的一个例子 :

// Import log4j classes.
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

// log4j.appender.console.layout.ConversionPattern=%d{yyyyMMdd HH.mm.ss} %-5p %C.%M(%L): %m%n

class Simple
{
    static Logger logger = Logger.getLogger(Simple.class);

    protected static void doIt()
    {
        logger.info("oh yea, we re doing it!");
        logger.error("  f-you! joe Boy, you are done!");
        logger.fatal("and the world went to bed");
    }

    public static void main(String[] args)
    {
        // BasicConfigurator replaced with PropertyConfigurator.
        PropertyConfigurator.configure(args[0]);

        logger.info("Entering application.");
        doIt();
        logger.info("Exiting application.");
    }
}

建造和运行时产生以下结果:

14:39:56:--> java -classpath log4j-1.2.15.jar:. Simple log4j.properties 
20120623 14.41.17 INFO  Simple.main(17): Entering application.
20120623 14.41.17 INFO  Simple.doIt(24): oh yea, we re doing it!
20120623 14.41.17 ERROR Simple.doIt(25):   f-you! joe Boy, you are done!
20120623 14.41.17 FATAL Simple.doIt(26): and the world went to bed
20120623 14.41.17 INFO  Simple.main(19): Exiting application.

当您使用像这样的转换模式时 :% d{yyyyyyyyyyyMMMDdd HH.mm.ss}% -5p% C.% M(% L):% m%n

以下是更多具体细节:

1. get a copy of log4j jar and put it in directory
2. create Simple.java in directory
3. create a file log4j.properties in same directory, put this in file:

log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{yyyyMMdd HH.mm.ss} %-5p %C.%M(%L): %m%n

4. compile Simple.java with: javac -classpath log4j-1.2.15.jar:. Simple.java
5. run it with this: java -classpath log4j-1.2.15.jar:. Simple log4j.properties

我们过去这样做是为了追踪一个应用程序何时失败。 (如果我能理解你的问题正确的话) 我们设置了某种类型的记录,这样我们就可以跟踪它,看看它是如何失败的,以及何时由于一系列事件而失败的。就像审计线索一样。我们设置了类似的东西,比如以下的东西。

class Simple
{
    public static void main(String args[])
    {
        log.info("Entering " + this.getClass().getName());
        System.out.println("I am a good bouy");
        log.info("Exiting " + this.getClass().getName());
    }
}

当然,您也可以将方法名称也放进去。 具体取决于您的班级和方法是如何执行的 。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签