java日志功能用法总结

1.通过系统属性指定配置文件:
System.setProperty("java.util.logging.config.file", "./config/logging.properties");
2.在配置文件中定义日志输出信息:
handlers=java.util.logging.ConsoleHandler,java.util.logging.FileHandler
#,java.util.logging.FileHandler
# Set the logging level of the root logger.
# Levels from lowest to highest are
# FINEST, FINER, FINE, CONFIG, INFO, WARNING and SEVERE.
# The default level for all loggers and handlers is INFO.
#.level=SEVERE
# 默认只有最高级别的日志才输出
.level=SEVERE

# Specify logging levels for specific namespaces.

#com.doublethree.dpg.level=FINEST

# Configure the ConsoleHandler.
# ConsoleHandler uses java.util.logging.SimpleFormatter by default. 
# Even though the root logger has the same level as this,
# the next line is still needed because we're configuring a handler,
# not a logger, and handlers don't inherit properties from the root logger.
#为具体包定义详细输出
com.doublethree.gateway.level=FINEST
#Handler输出所有来自Logger的信息;最后的输出级别为Logger和Handler取最小
java.util.logging.ConsoleHandler.level=ALL

# Configure the FileHandler.
# FileHandler uses java.util.logging.XMLFormatter by default. 
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=ALL

# The following special tokens can be used in the pattern property
# which specifies the location and name of the log file.
#   / - standard path separator
#   %t - system temporary directory
#   %h - value of the user.home system property
#   %g - generation number for rotating logs
#   %u - unique number to avoid conflicts
# FileHandler writes to %h/demo0.log by default.
java.util.logging.FileHandler.pattern=%h/demo%u.log
 
3.代码引用输出:
Logger logger = Logger.getLogger(LoggingDemo.class.getName());
System.out.println(new File(".").getAbsolutePath());
logger.finest("finest");
logger.finer("finer");
logger.fine("fine");
logger.config("config");
logger.info("info");
logger.warning("warning");
logger.severe("severe");
 
 

你可能感兴趣的:(log)