java logger

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

 

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

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

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

  1. java -Djava.util.logging.config.file=myfile  JavaClass  

2)在程序中初始化LogManager ,该方式也适用于基于Tomcat等web项目

 

  1. LogManager logManager = LogManager.getLogManager();  
  2.         InputStream in;  
  3.         try {  
  4.             in = new FileInputStream(LoggerUtils.class.getResource("/").getPath()  
  5.                     + "conf/logging.properties");  
  6.             logManager.readConfiguration(in);  
  7.         } catch (Exception e) {  
  8.             e.printStackTrace();  
  9.         }  

logger的主要配置信息如下:

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

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

  1. handlers= java.util.logging.ConsoleHandler  
  1. # To also add the FileHandler, use the following line instead.  
  2. #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):绝对路径如果不存在的话会报错  

 

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

解决办法:

 

  1. /** 
  2.      * 初始化Logger相关的配置 主要包括,读取指定指定目录下的logger.properties文件 
  3.      */  
  4.     public static void init() {  
  5.         logManager = LogManager.getLogManager();  
  6.         // 重新读取配置文件  
  7.         // ClassLoaderLogManager logManager = new ClassLoaderLogManager();  
  8.         InputStream inStream = null;  
  9.         try {  
  10.             inStream = new FileInputStream(Log.class.getResource("/").getPath()  
  11.                     + "conf/logging.properties");  
  12.             logManager.readConfiguration(inStream);  
  13.   
  14.             String logPath = logManager  
  15.                     .getProperty("java.util.logging.FileHandler.pattern");  
  16.             // 如果不是以%开头,则手动创建file文件  
  17.             // 否则会报如下:java.io.IOException: Couldn't get lock for c:/xx/java%u.log类似的错误  
  18.             if (logPath.startsWith("%") == false) {  
  19.                 File logFile = new File(logPath);  
  20.                 // 注意不能指定c:/log.file 即win下四个硬盘的根目录下不能存放  
  21.                 if (logFile.getParentFile().exists() == false) {  
  22.                     logFile.getParentFile().mkdirs();  
  23.                 }  
  24.             }  
  25.         } catch (Exception e) {  
  26.             e.printStackTrace();  
  27.         } finally {  
  28.             try {  
  29.                 if (inStream != null) {  
  30.                     inStream.close();  
  31.                 }  
  32.             } catch (IOException e) {  
  33.                 e.printStackTrace();  
  34.             }  
  35.         }  
  36.     }  

附上logging.properties的全文:

 
  1. ############################################################  
  2. #   Default Logging Configuration File  
  3. #  
  4. # You can use a different file by specifying a filename  
  5. # with the java.util.logging.config.file system property.    
  6. # For example java -Djava.util.logging.config.file=myfile  
  7. ############################################################  
  8.   
  9. ############################################################  
  10. #   Global properties  
  11. ############################################################  
  12.   
  13. # "handlers" specifies a comma separated list of log Handler   
  14. # classes.  These handlers will be installed during VM startup.  
  15. # Note that these classes must be on the system classpath.  
  16. # By default we only configure a ConsoleHandler, which will only  
  17. # show messages at the INFO and above levels.  
  18. handlersjava.util.logging.ConsoleHandler  
  19.   
  20. # To also add the FileHandler, use the following line instead.  
  21. #handlersjava.util.logging.FileHandler, java.util.logging.ConsoleHandler  
  22.   
  23. # Default global logging level.  
  24. # This specifies which kinds of events are logged across  
  25. # all loggers.  For any given facility this global level  
  26. # can be overriden by a facility specific level  
  27. # Note that the ConsoleHandler also has a separate level  
  28. # setting to limit messages printed to the console.  
  29. .levelWARNING   
  30.   
  31. ############################################################  
  32. # Handler specific properties.  
  33. # Describes specific configuration info for Handlers.  
  34. ############################################################  
  35.   
  36. # default file output is in user's home directory.  
  37. java.util.logging.FileHandler.pattern = %h/java%u.log  
  38. java.util.logging.FileHandler.limit = 50000  
  39. java.util.logging.FileHandler.count = 1  
  40. java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter  
  41.   
  42. # Limit the message that are printed on the console to INFO and above.  
  43. java.util.logging.ConsoleHandler.level = WARNING   
  44. java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter  
  45.   
  46.   
  47. ############################################################  
  48. # Facility specific properties.  
  49. # Provides extra control for each logger.  
  50. ############################################################  
  51.   
  52. # For example, set the com.xyz.foo logger to only log SEVERE  
  53. # messages:  
  54. com.xyz.foo.level = SEVERE  
 

 

猜你喜欢

转载自zeraw.iteye.com/blog/2329367