﻿{"id":110,"date":"2016-03-14T21:33:58","date_gmt":"2016-03-14T13:33:58","guid":{"rendered":"http:\/\/www.chenweikang.top\/?p=110"},"modified":"2016-03-14T21:39:23","modified_gmt":"2016-03-14T13:39:23","slug":"%e5%9c%a8java%e9%a1%b9%e7%9b%ae%e4%b8%ad%e4%bd%bf%e7%94%a8log4j-%e8%ae%b0%e5%bd%95%e6%97%a5%e5%bf%97","status":"publish","type":"post","link":"https:\/\/www.chenweikang.top\/?p=110","title":{"rendered":"\u5728java\u9879\u76ee\u4e2d\u4f7f\u7528log4j \u8bb0\u5f55\u65e5\u5fd7"},"content":{"rendered":"<blockquote><p>Log4j\u662f<a href=\"http:\/\/baike.baidu.com\/subview\/28283\/5418753.htm\" target=\"_blank\">Apache<\/a>\u7684\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u901a\u8fc7\u4f7f\u7528Log4j\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u65e5\u5fd7\u4fe1\u606f\u8f93\u9001\u7684\u76ee\u7684\u5730\u662f<a href=\"http:\/\/baike.baidu.com\/view\/135167.htm\" target=\"_blank\">\u63a7\u5236\u53f0<\/a>\u3001\u6587\u4ef6\u3001<a href=\"http:\/\/baike.baidu.com\/view\/25309.htm\" target=\"_blank\">GUI<\/a>\u7ec4\u4ef6\uff0c\u751a\u81f3\u662f\u5957\u63a5\u53e3\u670d\u52a1\u5668\u3001<a href=\"http:\/\/baike.baidu.com\/subview\/378\/5113303.htm\" target=\"_blank\">NT<\/a>\u7684\u4e8b\u4ef6\u8bb0\u5f55\u5668\u3001<a href=\"http:\/\/baike.baidu.com\/view\/8095.htm\" target=\"_blank\">UNIX<\/a> <a href=\"http:\/\/baike.baidu.com\/view\/1614723.htm\" target=\"_blank\">Syslog<\/a><a href=\"http:\/\/baike.baidu.com\/view\/53123.htm\" target=\"_blank\">\u5b88\u62a4\u8fdb\u7a0b<\/a>\u7b49\uff1b\u6211\u4eec\u4e5f\u53ef\u4ee5\u63a7\u5236\u6bcf\u4e00\u6761\u65e5\u5fd7\u7684\u8f93\u51fa\u683c\u5f0f\uff1b\u901a\u8fc7\u5b9a\u4e49\u6bcf\u4e00\u6761\u65e5\u5fd7\u4fe1\u606f\u7684\u7ea7\u522b\uff0c\u6211\u4eec\u80fd\u591f\u66f4\u52a0\u7ec6\u81f4\u5730\u63a7\u5236\u65e5\u5fd7\u7684\u751f\u6210\u8fc7\u7a0b\u3002\u6700\u4ee4\u4eba\u611f\u5174\u8da3\u7684\u5c31\u662f\uff0c\u8fd9\u4e9b\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a<a href=\"http:\/\/baike.baidu.com\/view\/2117618.htm\" target=\"_blank\">\u914d\u7f6e\u6587\u4ef6<\/a>\u6765\u7075\u6d3b\u5730\u8fdb\u884c\u914d\u7f6e\uff0c\u800c\u4e0d\u9700\u8981\u4fee\u6539\u5e94\u7528\u7684\u4ee3\u7801\u3002<\/p>\n<p>log4j jar\u5305\u4e0b\u8f7d\u00a0<a href=\"http:\/\/logging.apache.org\/log4j\/1.2\/download.html\">apache\u5b98\u7f51\u4e0b\u8f7dlog4j<\/a><\/p><\/blockquote>\n<h2>a). \u65b0\u5efa\u4e00\u4e2ajava project .<\/h2>\n<h2>b).\u9879\u76ee\u4e2d\u65b0\u5efalib\u6587\u4ef6\u5939 \uff0c\u5c06\u00a0log4j-1.2.17.jar \u62f7\u8d1d\u5230lib\u4e0b \u7136\u540e Build Path \u5230\u9879\u76ee<\/h2>\n<h2>c). \u65b0\u5efalog4j.properties\uff0c\u7f6e\u4e8eproject\u6839\u76ee\u5f55\u4e0b\uff1b<\/h2>\n<blockquote><p>log4j.rootLogger=info, ServerDailyRollingFile, stdout<br \/>\nlog4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender<br \/>\nlog4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd<br \/>\nlog4j.appender.ServerDailyRollingFile.File=D:\/\/logs\/mylog.txt<br \/>\nlog4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout<br \/>\nlog4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n<br \/>\nlog4j.appender.ServerDailyRollingFile.Append=true<br \/>\nlog4j.appender.stdout=org.apache.log4j.ConsoleAppender<br \/>\nlog4j.appender.stdout.layout=org.apache.log4j.PatternLayout<br \/>\nlog4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c] %m%n<\/p><\/blockquote>\n<p><span style=\"color: #ff0000;\">\u6ce8\u610f\uff1a\u00a0log4j.appender.ServerDailyRollingFile.File=D:\/\/logs\/mylog.txt \u00a0\u6307\u5b9a\u8f93\u51fa\u65e5\u5fd7\u6587\u4ef6\u4f4d\u7f6e<\/span><\/p>\n<h2>d). \u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55 \u65b0\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u7c7b Main \u5728main\u65b9\u6cd5\u4e2d\u6d4b\u8bd5<\/h2>\n<blockquote style=\"padding-left: 30px;\"><p>public class Main {<\/p>\n<p>public static void main(String[] args) {<br \/>\nLogger logger = Logger.getLogger(Main.class);<br \/>\nSystem.out.println(\"test log4j.....\");<br \/>\nlogger.info(\"log4j\u8f93\u51fa\u65e5\u5fd7...\");<br \/>\n}}<\/p><\/blockquote>\n<p>\u67e5\u770bD:\/\/logs\/mylog.txt \u65e5\u5fd7\u6210\u529f\u8bb0\u5f55<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-111\" src=\"http:\/\/www.chenweikang.top\/wp-content\/uploads\/2016\/03\/QQ\u622a\u56fe20160314212342.png\" alt=\"QQ\u622a\u56fe20160314212342\" width=\"642\" height=\"321\" \/><\/p>\n<h2><strong>\u30102\u3011log4j \u683c\u5f0f\u8be6\u89e3<\/strong><\/h2>\n<p>&nbsp;<\/p>\n<h3><strong>\u00a0log4j.rootLogger=\u65e5\u5fd7\u7ea7\u522b\uff0cappender1, appender2, \u2026.<\/strong><\/h3>\n<ul>\n<li>\u65e5\u5fd7\u7ea7\u522b\uff1a<strong>ALL&lt;DEBUG&lt;INFO&lt;WARN&lt;ERROR&lt;FATAL&lt;OFF<\/strong>\uff0c\u4e0d\u533a\u5206\u5927\u5c0f\u5199<\/li>\n<li>\u6ce8\u610f\uff0c\u9700\u5728\u63a7\u5236\u53f0\u8f93\u5165\uff0c\u53ea\u9700\u5c06\u5176\u4e2d\u4e00\u4e2aappender\u5b9a\u4e49\u4e3a<strong>stdout<\/strong>\u5373\u53ef<\/li>\n<li>\u6ce8\u610f\uff0crootLogger<strong>\u9ed8\u8ba4\u662f\u5bf9\u6574\u4e2a\u5de5\u7a0b<\/strong>\u751f\u6548<\/li>\n<li>\u6ce8\u610f\uff0c\u5982\u679c<strong>\u53ea\u60f3\u5bf9\u67d0\u4e9b\u5305<\/strong>\u64cd\u4f5c\uff0c\u90a3\u4e48\uff1alog4j.logger.<strong>com.hutu<\/strong>=info, stdout\uff0c\u8868\u793a\u8be5\u65e5\u5fd7\u5bf9package com.hutu\u751f\u6548<\/li>\n<li>\u6ce8\u610f\uff0c\u8fd9\u6837\u505a\u53ef\u4ee5\u533a\u5206dev\/\u7ebf\u4e0a\uff0c\u4e5f\u53ef\u4ee5\u51cf\u5c0f\u6027\u80fd\u5f71\u54cd\uff1a<strong>if(log.isDebugEnabled())<\/strong>{log.debug();}<\/li>\n<\/ul>\n<h3><strong>\u00a0log4j.appender.appender1=org.apache.log4j.\u65e5\u5fd7\u8f93\u51fa\u5230\u54ea\u513f<\/strong><\/h3>\n<ul>\n<li><strong>ConsoleAppender<\/strong>\uff08\u63a7\u5236\u53f0\uff09<\/li>\n<li><strong>FileAppender<\/strong>\uff08\u6587\u4ef6\uff09<\/li>\n<li><strong>DailyRollingFileAppender<\/strong>\uff08\u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff09<\/li>\n<li><strong>RollingFileAppender<\/strong>\uff08\u6587\u4ef6\u5927\u5c0f\u5230\u8fbe\u6307\u5b9a\u5c3a\u5bf8\u65f6\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\uff09<\/li>\n<li><strong>WriteAppender<\/strong>\uff08\u5c06\u65e5\u5fd7\u4fe1\u606f\u4ee5\u6d41\u683c\u5f0f\u53d1\u9001\u5230\u4efb\u610f\u6307\u5b9a\u7684\u5730\u65b9\uff09<\/li>\n<li><strong>JDBCAppender<\/strong>\uff08\u5c06\u65e5\u5fd7\u4fe1\u606f\u4fdd\u5b58\u5230\u6570\u636e\u5e93\u4e2d\uff09<\/li>\n<\/ul>\n<h3><strong>\u00a0log4j.appender.appender1.File=\u6587\u4ef6\u76ee\u5f55\u53ca\u6587\u4ef6<\/strong><\/h3>\n<p><strong>${user.home}\/logs\/...<\/strong><\/p>\n<h3><strong>\u00a0log4j.appender.appender1.MaxFileSize=\u6700\u5927\u6587\u4ef6\u5927\u5c0f<\/strong><\/h3>\n<h3>\u00a0log4j.appender.appender1.MaxBackupIndex=\u5907\u4efd\u6587\u4ef6\u4e2a\u6570<\/h3>\n<ul>\n<li>\u5176\u4e2d\uff0cappender1\u662f\u5728\u7b2c\u4e00\u884c\u5b9a\u4e49\u8fc7\u7684\uff1b<\/li>\n<li>\u6587\u4ef6\u76ee\u5f55\u53ca\u6587\u4ef6\uff0c\u4f8b\u5982\uff0c\/home\/admin\/logs\/hutudan.log<\/li>\n<li>\u6700\u5927\u6587\u4ef6\u5927\u5c0f\uff0c\u4f8b\u5982\uff0c100KB<\/li>\n<li>\u5907\u4efd\u6587\u4ef6\u4e2a\u6570\uff0c\u4f8b\u5982\uff0c1<\/li>\n<\/ul>\n<h3><strong>\u00a0log4j.appender.ServerDailyRollingFile.DatePattern=\u65e5\u5fd7\u540e\u7f00\u683c\u5f0f<\/strong><\/h3>\n<ul>\n<li>\u4f8b\u5982\uff0c'.'yyyy-MM-dd<\/li>\n<\/ul>\n<h3><strong>\u00a0log4j.appender.appender1.layout=org.apache.log4j.\u65e5\u5fd7\u5e03\u5c40\u683c\u5f0f<\/strong><\/h3>\n<ul>\n<li>HTMLLayout\uff08\u4ee5HTML\u8868\u683c\u5f62\u5f0f\u5e03\u5c40\uff09<\/li>\n<li>SimpleLayout\uff08\u5305\u542b\u65e5\u5fd7\u4fe1\u606f\u7684\u7ea7\u522b\u548c\u4fe1\u606f\u5b57\u7b26\u4e32\uff09<\/li>\n<li>TTCCLayout\uff08\u5305\u542b\u65e5\u5fd7\u4ea7\u751f\u7684\u65f6\u95f4\uff0c\u6267\u884c\u7eea\uff0c\u7c7b\u522b\u7b49\u4fe1\u606f\uff09<\/li>\n<li>PatternLayout\uff08\u53ef\u4ee5\u7075\u6d3b\u7684\u6307\u5b9a\u5e03\u5c40\u683c\u5f0f\uff0c<strong>\u5e38\u7528<\/strong>\uff09<\/li>\n<\/ul>\n<h3><strong>\u00a0log4j.appender.appender1.layout.ConversionPattern=\u65e5\u5fd7\u8f93\u51fa\u683c\u5f0f<\/strong><\/h3>\n<ol>\n<li>\u4f8b\u5982\uff0c%d - %m%n\u6216%d{yyyy-MM-dd HH:mm:ss} %p [%c] %m%n<\/li>\n<li><strong>%c <\/strong>\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\u6240\u5c5e\u7684\u7c7b\u7684\u5168\u540d<\/li>\n<li><strong>%d<\/strong> \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{yyy-M-dd HH:mm:ss }\uff0c\u8f93\u51fa\u7c7b\u4f3c\uff1a2002-10-18- 22\uff1a10\uff1a28<\/li>\n<li><strong>%f<\/strong> \u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\u6240\u5c5e\u7684\u7c7b\u7684\u7c7b\u540d<\/li>\n<li><strong>%l<\/strong> \u8f93\u51fa\u65e5\u5fd7\u4e8b\u4ef6\u7684\u53d1\u751f\u4f4d\u7f6e\uff0c\u5373\u8f93\u51fa\u65e5\u5fd7\u4fe1\u606f\u7684\u8bed\u53e5\u5904\u4e8e\u5b83\u6240\u5728\u7684\u7c7b\u7684\u7b2c\u51e0\u884c<\/li>\n<li><strong>%m<\/strong> \u8f93\u51fa\u4ee3\u7801\u4e2d\u6307\u5b9a\u7684\u4fe1\u606f\uff0c\u5982log(message)\u4e2d\u7684message<\/li>\n<li><strong>%n<\/strong> \u8f93\u51fa\u4e00\u4e2a\u56de\u8f66\u6362\u884c\u7b26\uff0cWindows\u5e73\u53f0\u4e3a\u201crn\u201d\uff0cUnix\u5e73\u53f0\u4e3a\u201cn\u201d<\/li>\n<li><strong>%p<\/strong> \u8f93\u51fa\u4f18\u5148\u7ea7\uff0c\u5373DEBUG\uff0cINFO\uff0cWARN\uff0cERROR\uff0cFATAL\u3002\u5982\u679c\u662f\u8c03\u7528debug()\u8f93\u51fa\u7684\uff0c\u5219\u4e3aDEBUG\uff0c\u4f9d\u6b64\u7c7b\u63a8<\/li>\n<li><strong>%r<\/strong> \u8f93\u51fa\u81ea\u5e94\u7528\u542f\u52a8\u5230\u8f93\u51fa\u8be5\u65e5\u5fd7\u4fe1\u606f\u6240\u8017\u8d39\u7684\u6beb\u79d2\u6570<\/li>\n<li><strong>%t<\/strong> \u8f93\u51fa\u4ea7\u751f\u8be5\u65e5\u5fd7\u4e8b\u4ef6\u7684\u7ebf\u7a0b\u540d<\/li>\n<li>\u53ef\u53c2\u8003\uff1a<a href=\"http:\/\/blog.sina.com.cn\/s\/blog_4e4dd5570100qowy.html\">http:\/\/blog.sina.com.cn\/s\/blog_4e4dd5570100qowy.html<\/a><\/li>\n<\/ol>\n<h3><strong>\u00a0log4j.appender.ServerDailyRollingFile.Append=true<\/strong><\/h3>\n<ul>\n<li>\u4f8b\u5982\uff0c\u4e0d\u89e3\u91ca\uff0c\u8ffd\u52a0\u5f80\u540e\u5199\u4fbf\u662f<\/li>\n<\/ul>\n<h2><strong>\u00a0\u603b\u7ed3\u4e00\u4e0b\uff1a<\/strong><\/h2>\n<ul>\n<li>Logger\u7c7b\uff1a\u5b8c\u6210\u65e5\u5fd7\u8bb0\u5f55\uff0c\u8bbe\u7f6e\u65e5\u5fd7\u4fe1\u606f\u7ea7\u522b<\/li>\n<li>Appender\u7c7b\uff1a\u51b3\u5b9a\u65e5\u5fd7\u53bb\u5411\uff0c\u7ec8\u7aef\u3001DB\u3001\u786c\u76d8<\/li>\n<li>Layout\u7c7b\uff1a\u51b3\u5b9a\u65e5\u5fd7\u8f93\u51fa\u7684\u6837\u5f0f\uff0c\u4f8b\u5982\u5305\u542b\u5f53\u524d\u7ebf\u7a0b\u3001\u884c\u53f7\u3001\u65f6\u95f4<\/li>\n<\/ul>\n<p>\u8f6c\u8f7d\u8bf7\u6ce8\u660e\uff1a<a href=\"https:\/\/www.chenweikang.top\">\u5de6\u624b\u4ee3\u7801\u53f3\u624b\u8bd7<\/a> &raquo; <a href=\"https:\/\/www.chenweikang.top\/?p=110\">\u5728java\u9879\u76ee\u4e2d\u4f7f\u7528log4j \u8bb0\u5f55\u65e5\u5fd7<\/a><\/p><div class=\"__youshang\">\r\n            <div id=\"__youshang_popup\" class=\"wechat popup\" style=\"display: none;\">\r\n                <div class=\"head\">~\u8c22\u8c22\u6253\u8d4f~<\/div>\r\n                <div class=\"qrcode\"><div class=\"qrcode-li wechat\" ><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/wexin.png\" \/><\/div><div class=\"qrcode-li alipay\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-pay.png\" \/><\/div><div class=\"qrcode-li hongbao\" style=\"display:none;\"><img src=\"https:\/\/www.chenweikang.top\/wp-content\/uploads\/2019\/07\/ali-hongbao.png\" \/><\/div><\/div>\r\n                <ul class=\"platform\"><li class=\"icon-wechat active\" data-bg-color=\"#05af4e\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-alipay \" data-bg-color=\"#00a2ea\" data-thanks=\"~\u8c22\u8c22\u6253\u8d4f~\"><\/li><li class=\"icon-hongbao \" data-bg-color=\"#dd5746\" data-thanks=\"<p>\u626b\u7801\u9886\u7ea2\u5305<\/p><p style='margin-top: 24px;'>\uff08\u4f59\u989d\u5b9d\u652f\u4ed8\u65f6\u53ef\u62b5\u73b0\uff09<\/p>\"><\/li><\/ul>\r\n            <\/div>\r\n            <a href=\"javascript:void(0);\" id=\"__youshang_btn\">\u8d4f<\/a>\r\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>Log4j\u662fApache\u7684\u4e00\u4e2a\u5f00\u6e90\u9879\u76ee\uff0c\u901a\u8fc7\u4f7f\u7528Log4j\uff0c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u65e5\u5fd7\u4fe1\u606f\u8f93\u9001\u7684\u76ee\u7684\u5730\u662f\u63a7\u5236\u53f0\u3001\u6587\u4ef6\u3001G [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8,3],"tags":[20,31],"class_list":["post-110","post","type-post","status-publish","format-standard","hentry","category-java","category-note","tag-java","tag-log4j"],"_links":{"self":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/110","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=110"}],"version-history":[{"count":0,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=\/wp\/v2\/posts\/110\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chenweikang.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}