{"id":967,"date":"2010-10-02T00:16:00","date_gmt":"2010-10-02T00:16:00","guid":{"rendered":"http:\/\/enjoyasp.net\/?p=967"},"modified":"2010-10-02T00:16:00","modified_gmt":"2010-10-02T00:16:00","slug":"log4j%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/enjoyasp.net\/index.php\/2010\/10\/02\/log4j%e4%bd%bf%e7%94%a8\/","title":{"rendered":"log4j\u4f7f\u7528"},"content":{"rendered":"<p>1\uff0c\u5bfc\u5165\u5165log4j-1.2.8.jar<\/p>\n<p>2\uff0c\u5efa\u7acblog4j.properties\u6587\u4ef6<\/p>\n<p>log4j.rootLogger=warn, myConsole, myFile<br \/>\n\/\/appender\u4e3a\u8f93\u51fa\u7684\u76ee\u5730\u5730\uff0c\u63a7\u5236\u53f0\u6216\u6587\u4ef6<br \/>\nlog4j.appender.myConsole=org.apache.log4j.ConsoleAppender<br \/>\nlog4j.appender.myConsole.layout=org.apache.log4j.PatternLayout<\/p>\n<p># Pattern to output the caller&#8217;s file name and line number.<br \/>\nlog4j.appender.myConsole.layout.ConversionPattern= [QC] %p [%t] %C.%M(%L) | %m%n<\/p>\n<p>log4j.appender.myFile=org.apache.log4j.RollingFileAppender<br \/>\nlog4j.appender.myFile.File=E:\\\\log\\\\JavaApp.log<br \/>\nlog4j.appender.myFile.MaxFileSize= 1KB<\/p>\n<p># Keep one backup file<br \/>\nlog4j.appender.myFile.MaxBackupIndex=1<\/p>\n<p>log4j.appender.myFile.layout=org.apache.log4j.PatternLayout<br \/>\nlog4j.appender.myFiles.layout.ConversionPattern= [QC] %p [%t] %C.%M(%L) | %m%n<\/p>\n<p>#\u6307\u5b9acom.neusoft\u5305\u4e0b\u7684\u6240\u6709\u7c7b\u7684\u7b49\u7ea7\u4e3aDEBUG\u3002<br \/>\nlog4j.logger.com. neusoft =DEBUG<\/p>\n<p>\u89e3\u91ca\uff1a<br \/>\n\u2460\u3001\u914d\u7f6e\u6839Logger??????? log4j.rootLogger<br \/>\nLogger \u8d1f\u8d23\u5904\u7406\u65e5\u5fd7\u8bb0\u5f55\u7684\u5927\u90e8\u5206\u64cd\u4f5c\u3002<br \/>\n\u5176\u8bed\u6cd5\u4e3a\uff1a<br \/>\nlog4j.rootLogger = [ level ] , appenderName, appenderName, \u2026<br \/>\n\u5176\u4e2d\uff0clevel \u662f\u65e5\u5fd7\u8bb0\u5f55\u7684\u4f18\u5148\u7ea7\uff0c\u5206\u4e3aOFF\u3001FATAL\u3001ERROR\u3001WARN\u3001INFO\u3001DEBUG\u3001ALL\u6216\u8005\u81ea\u5b9a\u4e49\u7684\u7ea7\u522b\u3002Log4j\u5efa\u8bae\u53ea\u4f7f\u7528\u56db\u4e2a\u7ea7\u522b\uff0c\u4f18\u5148\u7ea7\u4ece\u9ad8\u5230\u4f4e\u5206\u522b\u662fERROR\u3001WARN\u3001INFO\u3001DEBUG\u3002\u901a\u8fc7\u5728\u8fd9\u91cc\u5b9a\u4e49\u7684\u7ea7\u522b\uff0c\u60a8\u53ef\u4ee5\u63a7\u5236\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u76f8\u5e94\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\u7684\u5f00\u5173\u3002\u6bd4\u5982\u5728\u8fd9\u91cc\u5b9a\u4e49\u4e86INFO\u7ea7\u522b\uff0c\u53ea\u6709\u7b49\u4e8e\u53ca\u9ad8\u4e8e\u8fd9\u4e2a\u7ea7\u522b\u7684\u624d\u8fdb\u884c\u5904\u7406\uff0c\u5219\u5e94\u7528\u7a0b\u5e8f\u4e2d\u6240\u6709DEBUG\u7ea7\u522b\u7684\u65e5\u5fd7\u4fe1\u606f\u5c06\u4e0d\u88ab\u6253\u5370\u51fa\u6765\u3002ALL:\u6253\u5370\u6240\u6709\u7684\u65e5\u5fd7\uff0cOFF\uff1a\u5173\u95ed\u6240\u6709\u7684\u65e5\u5fd7\u8f93\u51fa\u3002 appenderName\u5c31\u662f\u6307\u5b9a\u65e5\u5fd7\u4fe1\u606f\u8f93\u51fa\u5230\u54ea\u4e2a\u5730\u65b9\u3002\u53ef\u540c\u65f6\u6307\u5b9a\u591a\u4e2a\u8f93\u51fa\u76ee\u7684\u5730\u3002<\/p>\n<p>\u2461\u3001\u914d\u7f6e\u65e5\u5fd7\u4fe1\u606f\u8f93\u51fa\u76ee\u7684\u5730 Appender<br \/>\nAppender \u8d1f\u8d23\u63a7\u5236\u65e5\u5fd7\u8bb0\u5f55\u64cd\u4f5c\u7684\u8f93\u51fa\u3002<br \/>\n\u5176\u8bed\u6cd5\u4e3a\uff1a<br \/>\nlog4j.appender.appenderName =org.apache.log4j.ConsoleAppender<br \/>\n\u8fd9\u91cc\u7684appenderName\u4e3a\u5728\u2460\u91cc\u5b9a\u4e49\u7684\uff0c\u53ef\u4efb\u610f\u8d77\u540d\u3002<br \/>\n\u5176\u4e2d\uff0cLog4j\u63d0\u4f9b\u7684appender\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<br \/>\norg.apache.log4j.ConsoleAppender\uff08\u63a7\u5236\u53f0\uff09\uff0c<br \/>\norg.apache.log4j.FileAppender\uff08\u6587\u4ef6\uff09\uff0c<br \/>\norg.apache.log4j.DailyRollingFileAppender\uff08\u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff09\uff0c<br \/>\norg.apache.log4j.RollingFileAppender\uff08\u6587\u4ef6\u5927\u5c0f\u5230\u8fbe\u6307\u5b9a\u5c3a\u5bf8\u7684\u65f6\u5019\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\uff09\uff0c\u53ef\u901a\u8fc7log4j.appender.R.MaxFileSize=100KB\u8bbe\u7f6e\u6587\u4ef6\u5927\u5c0f\uff0c\u8fd8\u53ef\u901a\u8fc7 log4j.appender.R.MaxBackupIndex=1\u8bbe\u7f6e\u4e3a\u4fdd\u5b58\u4e00\u4e2a\u5907\u4efd\u6587\u4ef6\u3002<br \/>\norg.apache.log4j.WriterAppender\uff08\u5c06\u65e5\u5fd7\u4fe1\u606f\u4ee5\u6d41\u683c\u5f0f\u53d1\u9001\u5230\u4efb\u610f\u6307\u5b9a\u7684\u5730\u65b9\uff09<br \/>\n\u4f8b\u5982\uff1alog4j.appender.stdout=org.apache.log4j.ConsoleAppender<br \/>\n\u5b9a\u4e49\u4e00\u4e2a\u540d\u4e3astdout\u7684\u8f93\u51fa\u76ee\u7684\u5730\uff0cConsoleAppender\u4e3a\u63a7\u5236\u53f0\u3002<\/p>\n<p>\u2462\u3001\u914d\u7f6e\u65e5\u5fd7\u4fe1\u606f\u7684\u683c\u5f0f\uff08\u5e03\u5c40\uff09Layout<br \/>\nLayout \u8d1f\u8d23\u683c\u5f0f\u5316Appender\u7684\u8f93\u51fa\u3002<br \/>\n\u5176\u8bed\u6cd5\u4e3a\uff1a<br \/>\nlog4j.appender.myConsole.layout=org.apache.log4j.PatternLayout<br \/>\n\u5176\u4e2d\uff0cLog4j\u63d0\u4f9b\u7684layout\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a<br \/>\norg.apache.log4j.HTMLLayout\uff08\u4ee5HTML\u8868\u683c\u5f62\u5f0f\u5e03\u5c40\uff09\uff0c<br \/>\norg.apache.log4j.PatternLayout\uff08\u53ef\u4ee5\u7075\u6d3b\u5730\u6307\u5b9a\u5e03\u5c40\u6a21\u5f0f\uff09\uff0c<br \/>\norg.apache.log4j.SimpleLayout\uff08\u5305\u542b\u65e5\u5fd7\u4fe1\u606f\u7684\u7ea7\u522b\u548c\u4fe1\u606f\u5b57\u7b26\u4e32\uff09\uff0c<br \/>\norg.apache.log4j.TTCCLayout\uff08\u5305\u542b\u65e5\u5fd7\u4ea7\u751f\u7684\u65f6\u95f4\u3001\u7ebf\u7a0b\u3001\u7c7b\u522b\u7b49\u7b49\u4fe1\u606f\uff09<\/p>\n<p>\u683c\u5f0f\u5316\u65e5\u5fd7\u4fe1\u606f\uff08\u5f53\u5e03\u5c40\u4e3aPatternLayout\u65f6\u624d\u53ef\u683c\u5f0f\u5316\u65e5\u5fd7\u4fe1\u606f\uff09<br \/>\nlog4j.appender.myConsole.layout.ConversionPattern= [QC] %p [%t] %C.%M(%L) | %m%n<br \/>\nLog4J\u91c7\u7528\u7c7b\u4f3cC\u8bed\u8a00\u4e2d\u7684printf\u51fd\u6570\u7684\u6253\u5370\u683c\u5f0f\u683c\u5f0f\u5316\u65e5\u5fd7\u4fe1\u606f\uff0c\u6253\u5370\u53c2\u6570\u5982\u4e0b\uff1a<br \/>\n%m \u8f93\u51fa\u4ee3\u7801\u4e2d\u6307\u5b9a\u7684\u6d88\u606f? \uff08m:message\uff09<br \/>\n%p \u8f93\u51fa\u4f18\u5148\u7ea7\uff0c\u5373DEBUG\uff0cINFO\uff0cWARN\uff0cERROR\uff0cFATAL?? \uff08p: perioty\uff09<br \/>\n%r \u8f93\u51fa\u81ea\u5e94\u7528\u542f\u52a8\u5230\u8f93\u51fa\u8be5log\u4fe1\u606f\u8017\u8d39\u7684\u6beb\u79d2\u6570\uff08r:run\uff09<br \/>\n%c \u8f93\u51fa\u6240\u5c5e\u7684\u7c7b\u76ee\uff0c\u901a\u5e38\u5c31\u662f\u6240\u5728\u7c7b\u7684\u5168\u540d\uff08c:class\uff09<br \/>\n%t \u8f93\u51fa\u4ea7\u751f\u8be5\u65e5\u5fd7\u4e8b\u4ef6\u7684\u7ebf\u7a0b\u540d\uff0c,\u901a\u5e38\u662f\u6267\u884c\u7684\u65b9\u6cd5\u540d\uff08t:thread\uff09<br \/>\n%n \u8f93\u51fa\u4e00\u4e2a\u56de\u8f66\u6362\u884c\u7b26\uff0cWindows\u5e73\u53f0\u4e3a\u201crn\u201d\uff0cUnix\u5e73\u53f0\u4e3a\u201cn\u201d \uff08n:enter\uff09<br \/>\n%d \u8f93\u51fa\u65e5\u5fd7\u65f6\u95f4\u70b9\u7684\u65e5\u671f\u6216\u65f6\u95f4\uff0c\u9ed8\u8ba4\u683c\u5f0f\u4e3aISO8601\uff0c\u4e5f\u53ef\u4ee5\u5728\u5176\u540e\u6307\u5b9a\u683c\u5f0f\uff0c\u6bd4\u5982\uff1a%d{yyyy MMM dd HH:mm:ss,SSS}\uff0c\u8f93\u51fa\u7c7b\u4f3c\uff1a2002\u5e7410\u670818\u65e5 22\uff1a10\uff1a28\uff0c921\uff08d:daytime\uff09<br \/>\n%l \u8f93\u51fa\u65e5\u5fd7\u4e8b\u4ef6\u7684\u53d1\u751f\u4f4d\u7f6e\uff0c\u5305\u62ec\u7c7b\u76ee\u540d\u3001\u53d1\u751f\u7684\u7ebf\u7a0b\uff0c\u4ee5\u53ca\u5728\u4ee3\u7801\u4e2d\u7684\u884c\u6570\u3002\uff08l:location\uff09<\/p>\n<p>3. \u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528<\/p>\n<p>\/\/LogTest:\u5f53\u524d\u7c7b\uff0c\u6b64\u53e5\u662f\u7528\u6765\u7ed9Log\u63d0\u4f9b\u5f53\u524d\u64cd\u4f5c\u7684\u7c7b\u7684\u76f8\u5173\u4fe1\u606f\u3002<br \/>\nLogger logger = Logger.getLogger(LogTest.class.getName());?<\/p>\n<p>PropertyConfigurator.configure ( &#8220;E:\\\\log\\\\log4j.properties&#8221;);<br \/>\n\/\/\u8f93\u51fa\u4fe1\u606f\uff0c\u4f1a\u81ea\u52a8\u8f93\u51fa\u5230properties\u6587\u4ef6\u6307\u5b9a\u7684appender\u4e2d<br \/>\nlogger.debug(&#8220;Debug &#8230;&#8221;);<br \/>\nlogger.info(&#8220;Info &#8230;&#8221;);<br \/>\nlogger.warn(&#8220;Warndsf &#8230;&#8221;);<br \/>\nlogger.error(&#8220;Errordsfsd &#8230;&#8221;);<br \/>\nSystem.out.println( &#8220;*********&#8221; );<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\uff0c\u5bfc\u5165\u5165log4j-1.2.8.jar 2\uff0c\u5efa\u7acblog4j.properties\u6587\u4ef6 log4j.rootL [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28],"tags":[],"class_list":["post-967","post","type-post","status-publish","format-standard","hentry","category-ssh"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/967","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/comments?post=967"}],"version-history":[{"count":0,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/posts\/967\/revisions"}],"wp:attachment":[{"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/media?parent=967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/categories?post=967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enjoyasp.net\/index.php\/wp-json\/wp\/v2\/tags?post=967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}