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
分享到:
相关推荐
详细介绍了java.util.logging.Logger的用法和结构,对如果扩展Logger起到抛砖引玉的作用!尊重劳动成果,亲下载了要给个评价!
Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException:java.lang.OutOfMemoryError),内附解决方案!
tomcat默认使用Java.util.logging记录日志,默认只记录tomcat的日志,不记录应用的日志。tomcat支持采用log4j进行日志记录,配置方法如下: 1.更新最新的tomcat_juli.jar包到./bin/目录下; 2.拷贝log4j.jar,lo4j....
Java日志实现。对java.util.logging.Logger的封装,在前人(原文件来自网络)的类上完善了文件的控制,使得日志文件可以重入。
1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...
本文通过对数据压缩算法的简要介绍,然后以详细的示例演示了利用java.util.zip包实现数据的压缩与解压,并扩展到在网络传输方面如何应用java.util.zip包现数据压缩与解压
java.util.ConcurrentModificationException 异常问题详解1
Exception in thread “main“ java.util.InputMismatchException
java.util.Date与java.sql.Date互转及字符串转换为日期时间格式.docx
java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1) java.util.concurrent系列文章(1)
java并发工具包 java.util.concurrent中文版-带书签版
详细介绍java.util.Date和java.sql.Date相互转换的多种方法总结,希望对大家有帮助
这是我在编写struts2中遇到的问题,整理出来,包括截图,希望可以帮到大家
java.util包
使用java.util.timer实现的简单定时任务,在实现简单一次性定时任务时,使用java.util.timer非常的简单易用,适合没有接触过quartz的新手急用。
发现问题 早上起来报错误,Jenkins打包到tomcat服务器,死活启动不起来,一些定时任务也没跑成功。 报错如下: org.apache.catalina.startup.ContextConfig.beforeStart ... at java.util.zip.ZipFile.(ZipFi
世界范围内的时区列表。由 java.util.TimeZone 类导出
java.util包源码,pdf版,方便打印
java.util.pdf
java API 在包java.util中有一个GregorianCalendar类,使用它可以得到一年内某个月的日历.zip