tomcat 日志 java.util.logging.Logger使用 (一)

时间:2021-03-16 19:50:04


1:java sdk 自带的logger配置文件位于 sdk home目录下:

jdk1.6.0_25\jre\lib\logging.properties

2:启用该日志配置有两种方式

1)用java -Djava.util.logging.config.file=myfile属性

java -Djava.util.logging.config.file=myfile  JavaClass
2)在程序中初始化LogManager ,该方式也适用于基于Tomcat等web项目

LogManager logManager = LogManager.getLogManager();
InputStream in;
try {
in = new FileInputStream(LoggerUtils.class.getResource("/").getPath()
+ "conf/logging.properties");
logManager.readConfiguration(in);
} catch (Exception e) {
e.printStackTrace();
}

logger的主要配置信息如下:

handlers 用户配置log的处理方式,主要有java.util.logging.ConsoleHandler 用于往控制台打印信息

还有一个java.util.logging.FileHandler用于往文件中写数据

handlers= java.util.logging.ConsoleHandler
# To also add the FileHandler, use the following line instead.#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
java.util.logging.FileHandler.pattern = %h/java%u.log  为生成的输出文件名称指定一个模式。
模式由包括以下特殊组件的字符串组成,则运行时要替换这些组件:
  • "/" 本地路径名分隔符
  • "%t" 系统临时目录
  • "%h" "user.home" 系统属性的值  (win系统会在c:/users/username目录下)
  • "%g" 区分循环日志的生成号
  • "%u" 解决冲突的惟一号码
  • "%%" 转换为单个百分数符号"%"  

1):该属性可以写绝对路径 如c:/xxxxx/java%u.log 但是不能写c:/java%u.log即四个盘的根目录不能写

2):绝对路径如果不存在的话会报错  

Can't load log handler "java.util.logging.FileHandler"
java.io.IOException: Couldn't get lock for c:/java%u.log
java.io.IOException: Couldn't get lock for c:/java%u.log
at java.util.logging.FileHandler.openFiles(FileHandler.java:372)
at java.util.logging.FileHandler.<init>(FileHandler.java:208)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at java.util.logging.LogManager$3.run(LogManager.java:358)
at java.security.AccessController.doPrivileged(Native Method)
at java.util.logging.LogManager.loadLoggerHandlers(LogManager.java:344)
at java.util.logging.LogManager.initializeGlobalHandlers(LogManager.java:909)
at java.util.logging.LogManager.access$900(LogManager.java:128)

解决办法:

/**
* 初始化Logger相关的配置 主要包括,读取指定指定目录下的logger.properties文件
*/
public static void init() {
logManager = LogManager.getLogManager();
// 重新读取配置文件
// ClassLoaderLogManager logManager = new ClassLoaderLogManager();
InputStream inStream = null;
try {
inStream = new FileInputStream(Log.class.getResource("/").getPath()
+ "conf/logging.properties");
logManager.readConfiguration(inStream);

String logPath = logManager
.getProperty("java.util.logging.FileHandler.pattern");
// 如果不是以%开头,则手动创建file文件
// 否则会报如下:java.io.IOException: Couldn't get lock for c:/xx/java%u.log类似的错误
if (logPath.startsWith("%") == false) {
File logFile = new File(logPath);
// 注意不能指定c:/log.file 即win下四个硬盘的根目录下不能存放
if (logFile.getParentFile().exists() == false) {
logFile.getParentFile().mkdirs();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inStream != null) {
inStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

附上logging.properties的全文:

############################################################
# Default Logging Configuration File
#
# You can use a different file by specifying a filename
# with the java.util.logging.config.file system property.
# For example java -Djava.util.logging.config.file=myfile
############################################################

############################################################
# Global properties
############################################################

# "handlers" specifies a comma separated list of log Handler
# classes. These handlers will be installed during VM startup.
# Note that these classes must be on the system classpath.
# By default we only configure a ConsoleHandler, which will only
# show messages at the INFO and above levels.
handlers= java.util.logging.ConsoleHandler

# To also add the FileHandler, use the following line instead.
#handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler

# Default global logging level.
# This specifies which kinds of events are logged across
# all loggers. For any given facility this global level
# can be overriden by a facility specific level
# Note that the ConsoleHandler also has a separate level
# setting to limit messages printed to the console.
.level= WARNING

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter

# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = WARNING
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter


############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
com.xyz.foo.level = SEVERE