Home  >  Article  >  Java  >  FAQ about logging mechanism in Java functions?

FAQ about logging mechanism in Java functions?

WBOY
WBOYOriginal
2024-05-04 14:33:02713browse

Answers to common logging questions in Java functions: Register a logger: Use Logger.getLogger(class name) to register. Set the log level: set through logger.setLevel(Level), for example, Level.FINE means outputting FINE level and above information. Use placeholders and parameters: Use new Object[] {parameter 1, parameter 2} placeholders and parameters to pass information. Contains exception information: Use logger.log(Level.SEVERE, "Exception message", e) to log exception information. Output to file: Create a FileHandler and add it to the logger, and set the file handler level.

Java 函数中日志记录机制的常见问题解答?

FAQ about logging mechanism in Java functions

Question 1: How to register a logger?

Code:

import java.util.logging.Logger;

public class LoggingExample {

    private static final Logger logger = Logger.getLogger(LoggingExample.class.getName());

    public static void main(String[] args) {
        // 注册日志记录器
        logger.info("日志消息");
    }
}

Question 2: How to set the log level?

Code:

import java.util.logging.Level;
import java.util.logging.Logger;

public class LoggingExample {

    private static final Logger logger = Logger.getLogger(LoggingExample.class.getName());

    public static void main(String[] args) {
        // 设置日志级别为 FINE
        logger.setLevel(Level.FINE);

        // 输出 FINE 级别及以上的消息
        logger.info("日志消息");
        logger.fine("详细日志消息");
    }
}

Question 3: How to use placeholders and parameters to pass information?

Code:

import java.util.logging.Logger;

public class LoggingExample {

    private static final Logger logger = Logger.getLogger(LoggingExample.class.getName());

    public static void main(String[] args) {
        // 使用占位符和参数传递信息
        logger.info("订单 {0} 已被创建,金额为 {1}", new Object[] {12345, 100.0});
    }
}

Question 4: How to include exception information in log messages?

Code:

import java.util.logging.Level;
import java.util.logging.Logger;

public class LoggingExample {

    private static final Logger logger = Logger.getLogger(LoggingExample.class.getName());

    public static void main(String[] args) {
        try {
            // 发生异常
            throw new Exception("异常发生");
        } catch (Exception e) {
            // 记录异常信息
            logger.log(Level.SEVERE, "异常发生", e);
        }
    }
}

Question 5: How to output the log to a file?

Code:

  1. Create Log File Processor
import java.io.File;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LoggingExample {

    private static final Logger logger = Logger.getLogger(LoggingExample.class.getName());

    public static void main(String[] args) throws Exception {
        // 创建日志文件处理器
        FileHandler fileHandler = new FileHandler("my.log");

        // 将日志文件处理器添加到日志记录器
        logger.addHandler(fileHandler);
    }
}
  1. Set file processor level
// 设置文件处理器的日志级别
fileHandler.setLevel(Level.INFO);

The above is the detailed content of FAQ about logging mechanism in Java functions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn