English 中文(简体)
如何在奥索尔进行 Java伐木,并存档?
原标题:How to do Java logging on console and also to file?
  • 时间:2012-05-18 23:13:41
  •  标签:
  • java
  • logging
public class MyUtil {

    public static boolean showLogging;
    public static boolean saveLogging;

public static void log(Object s) {
        if (showLogging) {
            // show logging on console
        }
        if (saveLogging) {
            // save logging to file and append logging old file when it is created
        }
}
}

This is my current idea. I heard of log4j but it s too complicate for me.

因此,我只想简单地在奥索尔印刷伐木,并用用户配置书写。

最佳回答

采用伐木框架,例如JDK的伐木框架,并将这一框架汇入这两个地方。 他们为你解决这些问题。

由于像原木4j这样的框架十分普遍,大多数人都实现了这一标准,并使用相同的伐木框架,因此所有东西都走到同一位置。 (即,他们使用已经使用标志4j的第三方图书馆。)

Here is something you might want to read to get a handle on how these frameworks work (most are similar): http://logging.apache.org/log4j/1.2/manual.html

本文件中列举一些真实世界实例的缩略语

问题回答

使用日志

第一种定义是 log4j.xml,它把ole和文档质都定义为:

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j= http://jakarta.apache.org/log4j/ >
    <appender name="CA" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
        </layout>
    </appender>
    <appender name="Daily" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="logs/sample.log" />
        <param name="DatePattern" value=" . yyyy-MM-dd-HH-mm" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%d [%t] - %m%n%n%n" />
        </layout>
    </appender>
    <root>
        <appender-ref ref="CA" />
        <appender-ref ref="Daily" />
    </root>
</log4j:configuration>

将该xml的档案保存在年级。

A sample code is:

4. 一揽子计划:

进口 org.apache.log4j.*;

public class HelloWorld 
{
    static final Logger logger = Logger.getLogger(HelloWorld.class);

    public static void main(String[] args) throws InterruptedException
    {

        logger.info("I am going to log to file as well as console");

    }

}




相关问题
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 ...

热门标签