Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get java Logger output to file by default [duplicate]

Netbeans thoughtfully sprinkles Logger.getLogger(this.getClass().getName()).log(Level. [...] statements into catch blocks. Now I would like to point them all to a file (and to console).

Every logging tutorial and such only me tells how to get a specific logger to output into a file, but I assume there is a better way than fixing every automatically generated logging statement? Setting a handler for some sort of root logger or something?

like image 857
Bloodboiler Avatar asked Apr 15 '09 13:04

Bloodboiler


People also ask

Why logger is printing twice in Java?

It's called additivity. Disabling additivity to prevent duplicate logging: In some cases, logging output is executed twice to a log destination. This is a side effect of the Log4j additivity which is active for each logger by default.

Does Java have a default logging mechanism?

Java is the programming language that comes with the logging approach. It provides a Logging API that was introduced in Java 1.4 version. It provides the ability to capture the log file.

How do I save a log in Java?

setFormatter(new Formatter() { @Override public String format(LogRecord record) { SimpleDateFormat logTime = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss"); Calendar cal = new GregorianCalendar(); cal. setTimeInMillis(record. getMillis()); return record. getLevel() + logTime.

Why is system out Println better than logger?

If you are running a Java program in Linux or any UNIX-based system, Log4j or SLF4j or any other logging framework offers a lot more features, flexibility, and improvement on message quality, which is not possible using the System. out. println() statement. I strongly discourage using System.


2 Answers

I just add the following at startup

Handler handler = new FileHandler("test.log", LOG_SIZE, LOG_ROTATION_COUNT);
Logger.getLogger("").addHandler(handler);

You can specify your own values for LOG_SIZE and LOG_ROTATION_COUNT

You may need adjust the logging level to suit.

like image 164
Miles D Avatar answered Oct 18 '22 05:10

Miles D


You have to define where the log is writting in the logger configuration file. For example, if you use log4j, a log4j.xml (or log4j.properties) file will contain such information.

For example, here is a simple log4j.xml file that logs directly into a file (my-app.log) and in the console:

<?xml version="1.0" encoding="UTF-8"?>
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="rolling" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="my-app.log" />
        <param name="DatePattern" value=".yyyy-MM-dd" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%C] [IP=%X{ipAddress}] [user=%X{user}] %m%n" />
        </layout>
    </appender>

    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d{yyyy-MM-dd HH:mm:ss} %-5p [%C] [user=%X{user}] %m%n" />
        </layout>
    </appender>

    <root>
        <priority value="info" />
        <appender-ref ref="console" />
        <appender-ref ref="rolling" />
    </root>

</log4j:configuration>
like image 31
Romain Linsolas Avatar answered Oct 18 '22 04:10

Romain Linsolas