Skip to content

Commit

Permalink
1.Add some logger's interface
Browse files Browse the repository at this point in the history
2.Add LevelMatchFilter
3.Modify logger thread delay sleep number.
4.Update README.MD
  • Loading branch information
liulhdarks committed Mar 27, 2014
1 parent 8479de2 commit 230f999
Show file tree
Hide file tree
Showing 15 changed files with 423 additions and 294 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Example:
static Logger log = Logger.getLogger("darks.logs.test.TestLogger");
static Logger log = Logger.getLogger("TestLogger");
</pre>
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.<br/>
Example:
<pre>
log.debug("This is the darks logs hello world.");
Expand Down Expand Up @@ -76,7 +76,7 @@ Example:<br/>

### 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<br/>
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.<br/>
Example:
<pre>
logd.root=info,console
Expand Down Expand Up @@ -134,6 +134,18 @@ Appender is the base class of all appenders. Appender can configure layout, filt
logd.appender.console.filter.accept=true
</pre>

LevelMatchFilter:Level match filter will output log which level has been contained by levels.<br/>
Example:
<pre>
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
</pre>

Custom filter:
<pre>
public class CustomFilter extends LoggerFilter
Expand Down
Binary file modified bin/darks/log/DefaultLogger.class
Binary file not shown.
Binary file modified bin/darks/log/InvalidLogger.class
Binary file not shown.
Binary file modified bin/darks/log/Logger.class
Binary file not shown.
Binary file modified bin/darks/log/LoggerFactory.class
Binary file not shown.
Binary file modified bin/darks/log/LoggerThread.class
Binary file not shown.
Binary file modified bin/darks/log/filter/LevelRangeFilter.class
Binary file not shown.
20 changes: 2 additions & 18 deletions src/darks/log/DefaultLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,9 @@ private LogMessage buildMessage(Level level, String msg, ThrowableInfo info)
* {@inheritDoc}
*/
@Override
public void addAppender(Appender appender, boolean rooted)
public void addAppender(Appender appender)
{
if (rooted)
{
Category cate = category;
while (cate.getParent() != null)
{
cate = cate.getParent();
}
synchronized (cate)
{
cate.getAppenderList().add(appender);
}
}
else
{
category.getAppenderList().add(appender);
}
category.getAppenderList().add(appender);
}

/**
Expand Down Expand Up @@ -204,5 +189,4 @@ public void setInherit(boolean inheritRoot)
}
}


}
2 changes: 1 addition & 1 deletion src/darks/log/InvalidLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void log(Level level, Object msg, Throwable t)
* {@inheritDoc}
*/
@Override
public void addAppender(Appender appender, boolean rooted)
public void addAppender(Appender appender)
{

}
Expand Down
17 changes: 12 additions & 5 deletions src/darks/log/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
*
* Logger.java
*
* @version 1.0.1
* @author Liu lihua 2014-3-21
* @version 1.0.2
* @author Liu lihua
*/
public abstract class Logger
{
Expand Down Expand Up @@ -323,10 +323,8 @@ public void log(Level level, Object msg)
* Add appender object
*
* @param appender Appender object
* @param rooted If true, appender will be added to root logger. otherwise
* be added to current logger.
*/
public abstract void addAppender(Appender appender, boolean rooted);
public abstract void addAppender(Appender appender);

/**
* Check if debug mode enabled.
Expand Down Expand Up @@ -369,4 +367,13 @@ public void setInherit(boolean inheritRoot)
{
}

/**
* Get root logger
*
* @return Logger object
*/
public static Logger getRootLogger()
{
return LoggerFactory.getRootLogger();
}
}
194 changes: 102 additions & 92 deletions src/darks/log/LoggerFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,108 +34,118 @@
* LoggerFactory.java
*
* @see Logger
* @version 1.0.0
* @author Liu lihua 2014-3-21
* @version 1.0.1
* @author Liu lihua
*/
public final class LoggerFactory
{

static ConfigLoader loader;
static ConfigLoader loader;

static volatile boolean inited = false;
static volatile boolean inited = false;

static Logger rootLogger;
static Logger rootLogger;

static
{
loader = new ConfigLoader();
try
{
inited = loader.initConfig();
}
catch (Exception e)
{
Kernel.logWarn(e.getMessage());
inited = false;
}
rootLogger = getRootLogger();
}
static
{
loader = new ConfigLoader();
try
{
inited = loader.initConfig();
}
catch (Exception e)
{
Kernel.logWarn(e.getMessage());
inited = false;
}
rootLogger = createRootLogger();
}

private LoggerFactory()
{
}
private LoggerFactory()
{
}

/**
* Create logger object. You can use {@linkplain darks.log.Logger Logger}
* instead of it.
*
* @param tag Tag string
* @return If succeed to be initialized, return
* {@linkplain darks.log.DefaultLogger DefaultLogger}. Otherwise
* return {@linkplain darks.log.InvalidLogger InvalidLogger}.
*/
public static synchronized Logger getLogger(String tag)
{
if (!inited)
{
return new InvalidLogger();
}
LoggerConfig cfg = Logger.Config;
Category category = getCategory(cfg, tag);
category.setInherit(cfg.getInherit(category.getName()));
category.buildAppenderArray();
return new DefaultLogger(category, tag);
}
/**
* Create logger object. You can use {@linkplain darks.log.Logger Logger}
* instead of it.
*
* @param tag Tag string
* @return If succeed to be initialized, return
* {@linkplain darks.log.DefaultLogger DefaultLogger}. Otherwise
* return {@linkplain darks.log.InvalidLogger InvalidLogger}.
*/
public static synchronized Logger getLogger(String tag)
{
if (!inited)
{
return new InvalidLogger();
}
LoggerConfig cfg = Logger.Config;
Category category = getCategory(cfg, tag);
category.setInherit(cfg.getInherit(category.getName()));
category.buildAppenderArray();
return new DefaultLogger(category, tag);
}

private static Logger getRootLogger()
{
return new DefaultLogger(Logger.Config.getRoot(), "");
}
/**
* Get root logger
*
* @return Logger object
*/
public static Logger getRootLogger()
{
return rootLogger;
}

/**
* 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;
}
private static Logger createRootLogger()
{
return new DefaultLogger(Logger.Config.getRoot(), "");
}

/**
* 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<String, Category> 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<String, Category> 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;
}
}
Loading

0 comments on commit 230f999

Please sign in to comment.