From 230f999a61b12b65ca648f567db49abb995e6c38 Mon Sep 17 00:00:00 2001
From: liulhdarks <313241942@qq.com>
Date: Thu, 27 Mar 2014 20:49:04 +0800
Subject: [PATCH] 1.Add some logger's interface 2.Add LevelMatchFilter 3.Modify
logger thread delay sleep number. 4.Update README.MD
---
README.md | 16 +-
bin/darks/log/DefaultLogger.class | Bin 4644 -> 4439 bytes
bin/darks/log/InvalidLogger.class | Bin 709 -> 685 bytes
bin/darks/log/Logger.class | Bin 3953 -> 4051 bytes
bin/darks/log/LoggerFactory.class | Bin 2906 -> 2975 bytes
bin/darks/log/LoggerThread.class | Bin 2024 -> 2024 bytes
bin/darks/log/filter/LevelRangeFilter.class | Bin 1188 -> 1214 bytes
src/darks/log/DefaultLogger.java | 20 +-
src/darks/log/InvalidLogger.java | 2 +-
src/darks/log/Logger.java | 17 +-
src/darks/log/LoggerFactory.java | 194 ++++++++++----------
src/darks/log/LoggerThread.java | 168 ++++++++---------
src/darks/log/filter/LevelMatchFilter.java | 108 +++++++++++
src/darks/log/filter/LevelRangeFilter.java | 188 +++++++++----------
src/darks/log/filter/RegexMatchFilter.java | 4 +
15 files changed, 423 insertions(+), 294 deletions(-)
create mode 100644 src/darks/log/filter/LevelMatchFilter.java
diff --git a/README.md b/README.md
index f6b3351..f627761 100644
--- a/README.md
+++ b/README.md
@@ -44,7 +44,7 @@ Example:
static Logger log = Logger.getLogger("darks.logs.test.TestLogger");
static Logger log = Logger.getLogger("TestLogger");
-After define Logger variable, you can call info, debug, warn, error and so on to output message for specify level.
+After define Logger variable, you can call info, debug, warn, error and so on to output message for specify level.
Example:
log.debug("This is the darks logs hello world."); @@ -76,7 +76,7 @@ Example:
### Config Appenders Darks logs has realized some frequently used appenders. Such as ConsoleAppender, AndroidAppender, FileAppender and so on. If you have configure multiple appenders, it will output log message to each appender when every message comes. All of appenders inherit from Appender
-If you need to configure class full name such as "darks.log.appender.impl.ConsoleAppender", you could just configure ConsoleAppender. If you configure class full name, it will load class directly. Or if you configure class simple name, it will find class in package darks.log.appender.impl, darks.log.filter, darks.log.layout, darks.log.pattern, darks.log.externs and so on. +If you need to configure class full name such as "darks.log.appender.impl.ConsoleAppender", you could just configure ConsoleAppender. If you configure class full name, it will load class directly. Or if you configure class simple name, it will find class in package darks.log.appender.impl, darks.log.filter, darks.log.layout, darks.log.pattern, darks.log.externs and so on.
Example:logd.root=info,console @@ -134,6 +134,18 @@ Appender is the base class of all appenders. Appender can configure layout, filt logd.appender.console.filter.accept=true+ LevelMatchFilter:Level match filter will output log which level has been contained by levels.
+ Example: ++ logd.appender.console=ConsoleAppender + logd.appender.console.layout=PatternLayout + logd.appender.console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} - %m%n + logd.appender.console.filter=LevelMatchFilter + #It will only output the message which level is debug or info. + logd.appender.console.filter.levels=debug,info + logd.appender.console.filter.accept=true ++ Custom filter:public class CustomFilter extends LoggerFilter diff --git a/bin/darks/log/DefaultLogger.class b/bin/darks/log/DefaultLogger.class index 54d8a70ec6d8114d96feeb9c02f7cbcd08066966..1782a5c63e2655a1d2bc591fd2d8f147078ce7bb 100644 GIT binary patch delta 375 zcmYk0O-lk{5Xb+sYlZ8kyBFzDl41~b2>J-2h=(pwNFEBstrldLQuBNCKpqObbm%ID zkdz;wYmipj`}^BRh{lyd9++q5Kl7j8?1dX4<@Bdk2QZEAiqdv3d9aLziY7_~IhDx} zyk;(9qzkK2E0xX~88@bIrJ{vdLSH00vk+5QxmI!0vGg13g;X|27&LQnYsKy{-K%&I zn@W7>O~o@_2p-c~Gd9fpoNea`lsq_7ny&dG2tGb228kcwn>fU170JmXoJ&YlSduWz zQE}Sa@S=nfuKnF{gfYey8?l33UPibOWi8~otfDP~x}VCTr3L8L5%2!Q{f;O<;z=y( zLFx`x#FjoT^R41U55!+PXxTw~`3Gtkz%gU$J$UUOui+gZ3{N;N@^Sjfsf4dzj8j=T delta 585 zcmYk2Pe@cz6vlsd-hDI8^Xa^x#Ly&Vf*HjLrA2=>DOwEJ1c6DjiII8Lgv^km>!E$S z2bEdWLUbn#g;9%&BBG*gYGwaR?XSBCB0F!)3hq7McMs<~=R0?%c|2-8{B`3d(7|%q znh%?brdY3c*{m<^PR9>bafDf$InFAKOg5_s4iyT+t`L=0)Z~iY?!d^go_uCt(9MR~ z#tWO5>{poGJzahKEXlmKdE@JY?#YqNU{O(>D<<EB@_>~E3R+pZjh=}h60+@oG4WqQTTxCmYl67s t~Z z!@Kky;px`v%mnD)r0-6$QQ|GX`d_{*jW*LtLYgILub@GLyjrj%o L`<-QarQ7FZ(pD#b`oW)pwGs*Abx%JlxH$1Oq}xsG3b|U6!Rx`h`3&nfo2F5 z;)L9|9G`Fhfl)2`=M?^Io$_`#b$Wi9SKgFUtERk&)2QBh38z`VTT5Q^lg0vz-f5>v ztrp()?m00-zSLWG8k3*OGJJK%8bNqKh)43Rk1K1`icxU>uHo=S%&UrJ0~HbaRSE)p F`wen?gt!0z diff --git a/bin/darks/log/InvalidLogger.class b/bin/darks/log/InvalidLogger.class index ee7f53dd2eb68c294730e5e160fe82e5c1c6de7c..93efa203757e9ab2c3f8a5f50ec0ba4b6f4ff1b0 100644 GIT binary patch delta 77 zcmX@gx|Wse)W2Q(7#J9g7-TkbxiB&+O!jA#o1Dj}KY117MOIUw4AbNWCMiWR1|}ez Zk%5PS7f6Fx5)6_+l953Q$YWxV1^~&?44?o2 delta 106 zcmZ3>dX$yx)W2Q(7#J9g800o`xiB&+PWES%V~m=d%c#%IR+OJ#lA6NE0OZeNysTso wRL2a&j2nRj69X5JWMg0ik|qq?KvIE$d2&CKs4!3ggA{`_Pz57{3|OTs0O_0&3jhEB diff --git a/bin/darks/log/Logger.class b/bin/darks/log/Logger.class index c723a409d91e881f17062895ea2ae0fd9598e9bb..3090d230733ca02039f91b59c3b7b52a8eac7edc 100644 GIT binary patch delta 172 zcmew;cUhk6)W2Q(7#J9g7 >rHSf4jNwInD%zr-g$Jw3IEkwHX5 z( uB5KXWV8+nN!_daiKAD@{j8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e6216271b2228179246e..532f5add0df980be4942468c4897638660a65d6b 100644 GIT binary patch delta 19 bcmaFC|AK$Qenys!2X3)4vTUBs=F12GQ*;M9 delta 19 bcmaFC|AK$Qen!TP2X3)4GH#yD=F12GQ$7bY diff --git a/bin/darks/log/filter/LevelRangeFilter.class b/bin/darks/log/filter/LevelRangeFilter.class index 00d60252cc6898fd9378042f076aa61d9a939714..1c4e25c03894901e9bce6109336ae0930edd6377 100644 GIT binary patch delta 401 zcmZvXJ4*vm5QV?HPqVB^AZ}ukH4h&VjJmcWh=?MB+Gt^;AQ%J<2nv>3`2*yZVrLm4 zf~2(6|Ke}3aPA5g!CRbrI5X$W%yZ^F(=VwwgJLYi#WSoJ;^PI3Q}HS5x+>dMV!fi8 z^?(`;pSmJ ?ryJ5_Qs1kl?yGaJMmwac8`js|0~R)r%)L?gAdoNm z#C3Y@0j1q}qYjvOByVUW^vM*ho>*)~1uJ-hYh+m`&jt~jwAo^TZPB3d8&6KE<|Rxq zEoVgHoMF|I`7MzH?5z4G_T>b{EU{pawku)anoasbtaPfEihKISPKt%sKRJ*x>@&iF gzI94VuB>7Fi~r_D!+D2#*-47gaNd#R9ZO#82TfKn8UO$Q delta 370 zcmZ9Gy-EX75QV?lU$d?zXv}|1R^y6B*jG@*MwAHJXeSs14FnXl^brJaBZ!@4iWMZK zt?%M9SU4NOBF)@0=N`T@pSibOT9(;>A|; 6C(^d#EIA=E-#ynqJFK20Hi zaei}sn+%4R$wB|FKL}~jHjfeoS$ZYJ$1|y1c@4TsGHvVo;T{VcXm!Ud9&T-ZP#sf$ zp^(W) entry : cfg.getCategories().entrySet()) - { - String key = entry.getKey(); - if (tag.startsWith(key)) - { - if (maxKey == null || maxKey.length() < key.length()) - { - maxKey = key; - match = entry.getValue(); - } - } - } - return match; - } + /** + * Find category object from config object by tag string. + * + * @param cfg Configuration object + * @param tag Tag string + * @return Category object + */ + private static Category getCategory(LoggerConfig cfg, String tag) + { + Category category = cfg.getCategory(tag); + if (category == null) + { + category = deepFindCategory(cfg, tag); + } + if (category == null) + { + return cfg.getRoot(); + } + return category; + } + + /** + * The depth of search category by tag string + * + * @param cfg Configuration object + * @param tag Tag string + * @return Category object + */ + private static Category deepFindCategory(LoggerConfig cfg, String tag) + { + Category match = null; + String maxKey = null; + for (Entry entry : cfg.getCategories().entrySet()) + { + String key = entry.getKey(); + if (tag.startsWith(key)) + { + if (maxKey == null || maxKey.length() < key.length()) + { + maxKey = key; + match = entry.getValue(); + } + } + } + return match; + } } diff --git a/src/darks/log/LoggerThread.java b/src/darks/log/LoggerThread.java index 1ce7475..133dc5a 100644 --- a/src/darks/log/LoggerThread.java +++ b/src/darks/log/LoggerThread.java @@ -1,84 +1,84 @@ -/** - * - *Copyright 2014 The Darks Logs Project (Liu lihua) - * - *Licensed under the Apache License, Version 2.0 (the "License"); - *you may not use this file except in compliance with the License. - *You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, software - *distributed under the License is distributed on an "AS IS" BASIS, - *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - *See the License for the specific language governing permissions and - *limitations under the License. - */ - -package darks.log; - -import java.util.Queue; -import java.util.concurrent.ConcurrentLinkedQueue; - -import darks.log.appender.Appender; -import darks.log.kernel.Kernel; - -/** - * Logger thread is used to process async appenders. - * - * LoggerThread.java - * @version 1.0.0 - * @author Liu lihua - * 2014-3-21 - */ -public class LoggerThread extends Thread -{ - - private static Queue holders = new ConcurrentLinkedQueue (); - - private static final int SLEEP_DELAY_COUNT = 256; - - public LoggerThread() - { - } - - /** - * {@inheritDoc} - */ - @Override - public void run() - { - try - { - while (!isInterrupted()) - { - Thread.sleep(100); - LoggerHolder holder = null; - int count = 0; - while ((holder = holders.poll()) != null) - { - for (Appender appender : holder.getAppenders()) - { - appender.doAppend(holder.getMsg()); - } - count++; - if (count % SLEEP_DELAY_COUNT == 0) - { - Thread.sleep(100); - } - } - } - } - catch (Exception e) - { - Kernel.logError("Logger thread shutdown. Cause " + e.getMessage(), e); - } - } - - public static Queue getHolders() - { - return holders; - } - - -} +/** + * + *Copyright 2014 The Darks Logs Project (Liu lihua) + * + *Licensed under the Apache License, Version 2.0 (the "License"); + *you may not use this file except in compliance with the License. + *You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + *Unless required by applicable law or agreed to in writing, software + *distributed under the License is distributed on an "AS IS" BASIS, + *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + *See the License for the specific language governing permissions and + *limitations under the License. + */ + +package darks.log; + +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; + +import darks.log.appender.Appender; +import darks.log.kernel.Kernel; + +/** + * Logger thread is used to process async appenders. + * + * LoggerThread.java + * @version 1.0.0 + * @author Liu lihua + * 2014-3-21 + */ +public class LoggerThread extends Thread +{ + + private static Queue holders = new ConcurrentLinkedQueue (); + + private static final int SLEEP_DELAY_COUNT = 1024; + + public LoggerThread() + { + } + + /** + * {@inheritDoc} + */ + @Override + public void run() + { + try + { + while (!isInterrupted()) + { + Thread.sleep(100); + LoggerHolder holder = null; + int count = 0; + while ((holder = holders.poll()) != null) + { + for (Appender appender : holder.getAppenders()) + { + appender.doAppend(holder.getMsg()); + } + count++; + if (count % SLEEP_DELAY_COUNT == 0) + { + Thread.sleep(100); + } + } + } + } + catch (Exception e) + { + Kernel.logError("Logger thread shutdown. Cause " + e.getMessage(), e); + } + } + + public static Queue getHolders() + { + return holders; + } + + +} diff --git a/src/darks/log/filter/LevelMatchFilter.java b/src/darks/log/filter/LevelMatchFilter.java new file mode 100644 index 0000000..4de5641 --- /dev/null +++ b/src/darks/log/filter/LevelMatchFilter.java @@ -0,0 +1,108 @@ +/** + * + *Copyright 2014 The Darks Logs Project (Liu lihua) + * + *Licensed under the Apache License, Version 2.0 (the "License"); + *you may not use this file except in compliance with the License. + *You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + *Unless required by applicable law or agreed to in writing, software + *distributed under the License is distributed on an "AS IS" BASIS, + *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + *See the License for the specific language governing permissions and + *limitations under the License. + */ + +package darks.log.filter; + +import java.util.LinkedList; +import java.util.List; + +import darks.log.Level; +import darks.log.LogMessage; + +/** + * Level match filter will output log which level has been contained by levels. + *
+ * Example: + *+ * logd.appender.console=ConsoleAppender + * logd.appender.console.layout=PatternLayout + * logd.appender.console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} - %m%n + * logd.appender.console.filter=LevelMatchFilter + * #It will only output the message which level is debug or info. + * logd.appender.console.filter.levels=debug,info + * logd.appender.console.filter.accept=true + *+ * LevelMatchFilter.java + * + * @version 1.0.0 + * @author Liu lihua + */ +public class LevelMatchFilter extends LoggerFilter +{ + + /** + * Target levels. + */ + private Level[] levels; + + public LevelMatchFilter() + { + } + + /** + * {@inheritDoc} + */ + @Override + public int decide(LogMessage msg) + { + if (levels == null || levels.length == 0) + { + return LoggerFilter.ALLOW; + } + for (Level level : levels) + { + if (level.equals(msg.getLevel())) + { + return LoggerFilter.ALLOW; + } + } + return LoggerFilter.DENY; + } + + public Level[] getLevels() + { + return levels; + } + + public void setLevels(String strLevel) + { + if (strLevel == null || "".equals(strLevel.trim())) + { + return; + } + strLevel = strLevel.trim(); + String[] args = strLevel.split(","); + if (args.length == 0) + { + return; + } + int len = args.length; + Listlist = new LinkedList (); + for (int i = 0; i < len; i++) + { + Level lv = Level.getLevel(args[i].trim()); + if (lv != null) + { + list.add(lv); + } + } + levels = new Level[list.size()]; + list.toArray(levels); + } + + +} diff --git a/src/darks/log/filter/LevelRangeFilter.java b/src/darks/log/filter/LevelRangeFilter.java index dc420b4..44806c9 100644 --- a/src/darks/log/filter/LevelRangeFilter.java +++ b/src/darks/log/filter/LevelRangeFilter.java @@ -1,92 +1,96 @@ -/** - * - *Copyright 2014 The Darks Logs Project (Liu lihua) - * - *Licensed under the Apache License, Version 2.0 (the "License"); - *you may not use this file except in compliance with the License. - *You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - *Unless required by applicable law or agreed to in writing, software - *distributed under the License is distributed on an "AS IS" BASIS, - *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - *See the License for the specific language governing permissions and - *limitations under the License. - */ - -package darks.log.filter; - -import darks.log.Level; -import darks.log.LogMessage; - -/** - * Level range filter will output log which level between levelMin and levelMax. - *
- * Example: - *- * logd.appender.console=ConsoleAppender - * logd.appender.console.layout=PatternLayout - * logd.appender.console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} - %m%n - * logd.appender.console.filter=LevelRangeFilter - * logd.appender.console.filter.levelMin=debug - * logd.appender.console.filter.levelMax=info - * logd.appender.console.filter.accept=true - *- * LevelRangeFilter.java - * - * @version 1.0.0 - * @author Liu lihua - */ -public class LevelRangeFilter extends LoggerFilter -{ - - /** - * Minimum level allowed - */ - private Level levelMin; - - /** - * Maximum level allowed - */ - private Level levelMax; - - public LevelRangeFilter() - { - } - - /** - * {@inheritDoc} - */ - @Override - public int decide(LogMessage msg) - { - Level lv = msg.getLevel(); - if (lv.compare(levelMin) >= 0 && lv.compare(levelMax) <= 0) - { - return LoggerFilter.ALLOW; - } - return LoggerFilter.DENY; - } - - public Level getLevelMin() - { - return levelMin; - } - - public void setLevelMin(String levelMin) - { - this.levelMin = Level.getLevel(levelMin); - } - - public Level getLevelMax() - { - return levelMax; - } - - public void setLevelMax(String levelMax) - { - this.levelMax = Level.getLevel(levelMax); - } - -} +/** + * + *Copyright 2014 The Darks Logs Project (Liu lihua) + * + *Licensed under the Apache License, Version 2.0 (the "License"); + *you may not use this file except in compliance with the License. + *You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + *Unless required by applicable law or agreed to in writing, software + *distributed under the License is distributed on an "AS IS" BASIS, + *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + *See the License for the specific language governing permissions and + *limitations under the License. + */ + +package darks.log.filter; + +import darks.log.Level; +import darks.log.LogMessage; + +/** + * Level range filter will output log which level between levelMin and levelMax. + *
+ * Example: + *+ * logd.appender.console=ConsoleAppender + * logd.appender.console.layout=PatternLayout + * logd.appender.console.layout.pattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} - %m%n + * logd.appender.console.filter=LevelRangeFilter + * logd.appender.console.filter.levelMin=debug + * logd.appender.console.filter.levelMax=info + * logd.appender.console.filter.accept=true + *+ * LevelRangeFilter.java + * + * @version 1.0.0 + * @author Liu lihua + */ +public class LevelRangeFilter extends LoggerFilter +{ + + /** + * Minimum level allowed + */ + private Level levelMin; + + /** + * Maximum level allowed + */ + private Level levelMax; + + public LevelRangeFilter() + { + } + + /** + * {@inheritDoc} + */ + @Override + public int decide(LogMessage msg) + { + if (levelMin == null || levelMax == null) + { + return LoggerFilter.ALLOW; + } + Level lv = msg.getLevel(); + if (lv.compare(levelMin) >= 0 && lv.compare(levelMax) <= 0) + { + return LoggerFilter.ALLOW; + } + return LoggerFilter.DENY; + } + + public Level getLevelMin() + { + return levelMin; + } + + public void setLevelMin(String levelMin) + { + this.levelMin = Level.getLevel(levelMin); + } + + public Level getLevelMax() + { + return levelMax; + } + + public void setLevelMax(String levelMax) + { + this.levelMax = Level.getLevel(levelMax); + } + +} diff --git a/src/darks/log/filter/RegexMatchFilter.java b/src/darks/log/filter/RegexMatchFilter.java index baaa1eb..bac97b7 100644 --- a/src/darks/log/filter/RegexMatchFilter.java +++ b/src/darks/log/filter/RegexMatchFilter.java @@ -60,6 +60,10 @@ public RegexMatchFilter() @Override public int decide(LogMessage msg) { + if (regex == null) + { + return LoggerFilter.ALLOW; + } Matcher matcher = regex.matcher(msg.getMessage()); if (matcher.find()) {