开发者

Java write record or message to log file problem

开发者 https://www.devze.com 2023-03-07 20:16 出处:网络
try { // Cr开发者_开发百科eate an appending file handler boolean append = true; FileHandler handler = new FileHandler(\"my.log\", append);
try {
    // Cr开发者_开发百科eate an appending file handler
    boolean append = true;
    FileHandler handler = new FileHandler("my.log", append);

    // Add to the desired logger
    Logger logger = Logger.getLogger("com.mycompany");
    logger.log(Level.INFO, "This is info message");
    logger.addHandler(handler);
} catch (IOException e) {
}

My problem is when i open my.log file i don't see the text "This is info message" but just

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
</log>

Can anyone point me out where is problem???


I think you should add handler before you log something.


You should set the appender before logging anything is logged

try {
    // Create an appending file handler
    boolean append = true;
    FileHandler handler = new FileHandler("my.log", append);

    // Add to the desired logger
    Logger logger = Logger.getLogger("com.mycompany");
    logger.addHandler(handler);

    logger.log(Level.INFO, "This is info message");
    } catch (IOException e) {
}


Quoting from the docs:

Each Logger has a "Level" associated with it.

Maybe the level configured for this logger is too high for INFO level messages to show up.

0

精彩评论

暂无评论...
验证码 换一张
取 消