diff --git a/gradle.properties b/gradle.properties index 21dd394..894aa5a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Messiva -PROJECT_VERSION = 0.1.1 +PROJECT_VERSION = 0.2.0 ## dependencies diff --git a/src/main/java/cc/sukazyo/messiva/appender/AppenderRestrictableByLevel.java b/src/main/java/cc/sukazyo/messiva/appender/AppenderRestrictableByLevel.java index 167c3da..e842981 100644 --- a/src/main/java/cc/sukazyo/messiva/appender/AppenderRestrictableByLevel.java +++ b/src/main/java/cc/sukazyo/messiva/appender/AppenderRestrictableByLevel.java @@ -2,8 +2,8 @@ package cc.sukazyo.messiva.appender; import cc.sukazyo.messiva.component.LevelRestrictComponent; import cc.sukazyo.messiva.formatter.ILogFormatter; +import cc.sukazyo.messiva.log.ILogLevel; import cc.sukazyo.messiva.log.Log; -import cc.sukazyo.messiva.log.LogLevel; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -22,13 +22,13 @@ public abstract class AppenderRestrictableByLevel extends Appender { } @Nonnull - public AppenderRestrictableByLevel minLevel(@Nonnull LogLevel minLevel) { + public AppenderRestrictableByLevel minLevel(@Nonnull ILogLevel minLevel) { levelSetting.minLevel(minLevel); return this; } @Nonnull - public AppenderRestrictableByLevel maxLevel(@Nonnull LogLevel maxLevel) { + public AppenderRestrictableByLevel maxLevel(@Nonnull ILogLevel maxLevel) { levelSetting.maxLevel(maxLevel); return this; } diff --git a/src/main/java/cc/sukazyo/messiva/appender/ConsoleAppender.java b/src/main/java/cc/sukazyo/messiva/appender/ConsoleAppender.java index 55ea206..9820bc7 100644 --- a/src/main/java/cc/sukazyo/messiva/appender/ConsoleAppender.java +++ b/src/main/java/cc/sukazyo/messiva/appender/ConsoleAppender.java @@ -2,10 +2,11 @@ package cc.sukazyo.messiva.appender; import cc.sukazyo.messiva.formatter.ILogFormatter; import cc.sukazyo.messiva.log.Log; -import cc.sukazyo.messiva.utils.StringUtils; import javax.annotation.Nonnull; import javax.annotation.Nullable; + +import java.util.Objects; import java.util.concurrent.locks.ReentrantLock; public class ConsoleAppender extends AppenderRestrictableByLevel { diff --git a/src/main/java/cc/sukazyo/messiva/appender/IAppender.java b/src/main/java/cc/sukazyo/messiva/appender/IAppender.java index a88d362..ddade53 100644 --- a/src/main/java/cc/sukazyo/messiva/appender/IAppender.java +++ b/src/main/java/cc/sukazyo/messiva/appender/IAppender.java @@ -1,9 +1,11 @@ package cc.sukazyo.messiva.appender; +import javax.annotation.Nonnull; + import cc.sukazyo.messiva.log.Log; public interface IAppender { - void pushLog (Log log); + void pushLog (@Nonnull Log log); } diff --git a/src/main/java/cc/sukazyo/messiva/component/LevelRestrictComponent.java b/src/main/java/cc/sukazyo/messiva/component/LevelRestrictComponent.java index e6ba341..11c1b14 100644 --- a/src/main/java/cc/sukazyo/messiva/component/LevelRestrictComponent.java +++ b/src/main/java/cc/sukazyo/messiva/component/LevelRestrictComponent.java @@ -1,51 +1,52 @@ package cc.sukazyo.messiva.component; -import cc.sukazyo.messiva.log.LogLevel; +import cc.sukazyo.messiva.log.ILogLevel; +import cc.sukazyo.messiva.log.LogLevels; import javax.annotation.Nonnull; public class LevelRestrictComponent { - @Nonnull private LogLevel minLevel; - @Nonnull private LogLevel maxLevel; + @Nonnull private ILogLevel minLevel; + @Nonnull private ILogLevel maxLevel; - public LevelRestrictComponent (@Nonnull LogLevel minLevel, @Nonnull LogLevel maxLevel) { + public LevelRestrictComponent (@Nonnull ILogLevel minLevel, @Nonnull ILogLevel maxLevel) { this.minLevel = minLevel; this.maxLevel = maxLevel; } - public LevelRestrictComponent (@Nonnull LogLevel minLevel) { - this(minLevel, LogLevel.NONE); + public LevelRestrictComponent (@Nonnull ILogLevel minLevel) { + this(minLevel, LogLevels.NONE); } public LevelRestrictComponent () { - this(LogLevel.ALL); + this(LogLevels.ALL); } @Nonnull - public LevelRestrictComponent minLevel (@Nonnull LogLevel minLevel) { + public LevelRestrictComponent minLevel (@Nonnull ILogLevel minLevel) { this.minLevel = minLevel; return this; } @Nonnull - public LevelRestrictComponent maxLevel (@Nonnull LogLevel maxLevel) { + public LevelRestrictComponent maxLevel (@Nonnull ILogLevel maxLevel) { this.maxLevel = maxLevel; return this; } @Nonnull - public LogLevel minLevel () { + public ILogLevel minLevel () { return this.minLevel; } @Nonnull - public LogLevel maxLevel () { + public ILogLevel maxLevel () { return this.maxLevel; } - public boolean checkLevel (@Nonnull LogLevel level) { - return level.level >= minLevel.level && level.level <= maxLevel.level; + public boolean checkLevel (@Nonnull ILogLevel level) { + return level.level() >= minLevel.level() && level.level() <= maxLevel.level(); } } diff --git a/src/main/java/cc/sukazyo/messiva/formatter/SimpleFormatter.java b/src/main/java/cc/sukazyo/messiva/formatter/SimpleFormatter.java index 8a955cd..60d7ee2 100644 --- a/src/main/java/cc/sukazyo/messiva/formatter/SimpleFormatter.java +++ b/src/main/java/cc/sukazyo/messiva/formatter/SimpleFormatter.java @@ -22,10 +22,10 @@ public class SimpleFormatter implements ILogFormatter { message.append(startTimestamp).append(log.timestamp).append(endTimestamp) .append(startThreadName).append(log.thread.getName()).append(endThreadName); final String promptNewline = StringUtils.repeatChar(followingLineFillChar, message.length()); - message.append(startLevelTag).append(log.level.tag).append(endLevelTag).append(log.message.message[0]); + message.append(startLevelTag).append(log.level.tag()).append(endLevelTag).append(log.message.message[0]); for (int i = 1; i < log.message.message.length; i++) { message.append('\n').append(promptNewline) - .append(startLevelTag).append(log.level.tag).append(endLevelTag).append(log.message.message[i]); + .append(startLevelTag).append(log.level.tag()).append(endLevelTag).append(log.message.message[i]); } return message.toString(); } diff --git a/src/main/java/cc/sukazyo/messiva/log/ILogLevel.java b/src/main/java/cc/sukazyo/messiva/log/ILogLevel.java new file mode 100644 index 0000000..6f3caa5 --- /dev/null +++ b/src/main/java/cc/sukazyo/messiva/log/ILogLevel.java @@ -0,0 +1,6 @@ +package cc.sukazyo.messiva.log; + +public interface ILogLevel { + float level(); + String tag(); +} diff --git a/src/main/java/cc/sukazyo/messiva/log/ILogLevelImpl.java b/src/main/java/cc/sukazyo/messiva/log/ILogLevelImpl.java index 9f94106..b2d657c 100644 --- a/src/main/java/cc/sukazyo/messiva/log/ILogLevelImpl.java +++ b/src/main/java/cc/sukazyo/messiva/log/ILogLevelImpl.java @@ -16,4 +16,18 @@ public interface ILogLevelImpl { void fatal (String message); + void trace (Message message); + + void debug (Message message); + + void info (Message message); + + void warn (Message message); + + void warning (Message message); + + void error (Message message); + + void fatal (Message message); + } diff --git a/src/main/java/cc/sukazyo/messiva/log/Log.java b/src/main/java/cc/sukazyo/messiva/log/Log.java index 35948f7..2420c5d 100644 --- a/src/main/java/cc/sukazyo/messiva/log/Log.java +++ b/src/main/java/cc/sukazyo/messiva/log/Log.java @@ -1,16 +1,20 @@ package cc.sukazyo.messiva.log; +import javax.annotation.Nonnegative; +import javax.annotation.Nonnull; + import cc.sukazyo.messiva.utils.StackUtils; public class Log { - public final Message message; - public final LogLevel level; + @Nonnull public final Message message; + @Nonnull public final ILogLevel level; public final long timestamp; - public final Thread thread; - public final StackTraceElement[] stackTrace; + @Nonnull public final Thread thread; + @Nonnull public final StackTraceElement[] stackTrace; - public Log (Message message, LogLevel level, Thread thread, long timestamp, StackTraceElement[] stackTrace) { + public Log (@Nonnull Message message, @Nonnull ILogLevel level, + @Nonnull Thread thread,long timestamp, @Nonnull StackTraceElement[] stackTrace) { this.message = message; this.level = level; this.thread = thread; @@ -18,27 +22,34 @@ public class Log { this.stackTrace = stackTrace; } - public Log (int stackOffset, Message message, LogLevel level, Thread thread, long timestamp) { + public Log (@Nonnegative int stackOffset, + @Nonnull Message message, @Nonnull ILogLevel level, + @Nonnull Thread thread, long timestamp) { this(message, level, thread, timestamp, StackUtils.getStackTrace(stackOffset+1)); } - public Log (Message message, LogLevel level, Thread thread, long timestamp) { + public Log (@Nonnull Message message, @Nonnull ILogLevel level, + @Nonnull Thread thread, long timestamp) { this(1, message, level, thread, timestamp); } - public Log (int stackOffset, Message message, LogLevel level, Thread thread) { + public Log (@Nonnegative int stackOffset, + @Nonnull Message message, @Nonnull ILogLevel level, + @Nonnull Thread thread) { this(stackOffset+1, message, level, thread, System.currentTimeMillis()); } - public Log (Message message, LogLevel level, Thread thread) { + public Log (@Nonnull Message message, @Nonnull ILogLevel level, + @Nonnull Thread thread) { this(1, message, level, thread); } - public Log (int stackOffset, Message message, LogLevel level) { + public Log (@Nonnegative int stackOffset, + @Nonnull Message message, @Nonnull ILogLevel level) { this(stackOffset+1, message, level, Thread.currentThread()); } - public Log (Message message, LogLevel level) { + public Log (@Nonnull Message message, @Nonnull ILogLevel level) { this(1, message, level); } diff --git a/src/main/java/cc/sukazyo/messiva/log/LogLevel.java b/src/main/java/cc/sukazyo/messiva/log/LogLevels.java similarity index 64% rename from src/main/java/cc/sukazyo/messiva/log/LogLevel.java rename to src/main/java/cc/sukazyo/messiva/log/LogLevels.java index efe5ada..8d3284d 100644 --- a/src/main/java/cc/sukazyo/messiva/log/LogLevel.java +++ b/src/main/java/cc/sukazyo/messiva/log/LogLevels.java @@ -1,6 +1,6 @@ package cc.sukazyo.messiva.log; -public enum LogLevel { +public enum LogLevels implements ILogLevel { ALL (-Float.MAX_VALUE, "####"), TRACE(-1f, "TRAC"), @@ -14,9 +14,15 @@ public enum LogLevel { public final float level; public final String tag; - LogLevel (float level, String tag) { + LogLevels (float level, String tag) { this.level = level; this.tag = tag; } + @Override + public float level () { return this.level; } + + @Override + public String tag () { return this.tag; } + } diff --git a/src/main/java/cc/sukazyo/messiva/logger/Logger.java b/src/main/java/cc/sukazyo/messiva/logger/Logger.java index e8b56aa..194b3d9 100644 --- a/src/main/java/cc/sukazyo/messiva/logger/Logger.java +++ b/src/main/java/cc/sukazyo/messiva/logger/Logger.java @@ -2,10 +2,7 @@ package cc.sukazyo.messiva.logger; import cc.sukazyo.messiva.component.LevelRestrictComponent; import cc.sukazyo.messiva.appender.IAppender; -import cc.sukazyo.messiva.log.ILogLevelImpl; -import cc.sukazyo.messiva.log.Log; -import cc.sukazyo.messiva.log.LogLevel; -import cc.sukazyo.messiva.log.Message; +import cc.sukazyo.messiva.log.*; import javax.annotation.Nonnull; import java.util.ArrayList; @@ -28,45 +25,81 @@ public class Logger implements ILogLevelImpl { this.appends.addAll(Arrays.asList(appends)); } - public Logger minLevel (@Nonnull LogLevel minLevel) { + public Logger minLevel (@Nonnull ILogLevel minLevel) { levelSetting.minLevel(minLevel); return this; } - public Logger maxLevel (@Nonnull LogLevel maxLevel) { + public Logger maxLevel (@Nonnull ILogLevel maxLevel) { levelSetting.maxLevel(maxLevel); return this; } public void trace (String message) { - pushToAllAppender(new Log(1, new Message(message), LogLevel.TRACE)); + pushToAllAppender(new Log(1, new Message(message), LogLevels.TRACE)); } public void debug (String message) { - pushToAllAppender(new Log(1, new Message(message), LogLevel.DEBUG)); + pushToAllAppender(new Log(1, new Message(message), LogLevels.DEBUG)); } public void info (String message) { - pushToAllAppender(new Log(1, new Message(message), LogLevel.INFO)); + pushToAllAppender(new Log(1, new Message(message), LogLevels.INFO)); } public void warn (String message) { - pushToAllAppender(new Log(1, new Message(message), LogLevel.WARN)); + pushToAllAppender(new Log(1, new Message(message), LogLevels.WARN)); } public void warning (String message) { - pushToAllAppender(new Log(1, new Message(message), LogLevel.WARN)); + pushToAllAppender(new Log(1, new Message(message), LogLevels.WARN)); } public void error (String message) { - pushToAllAppender(new Log(1, new Message(message), LogLevel.ERROR)); + pushToAllAppender(new Log(1, new Message(message), LogLevels.ERROR)); } public void fatal (String message) { - pushToAllAppender(new Log(1, new Message(message), LogLevel.FATAL)); + pushToAllAppender(new Log(1, new Message(message), LogLevels.FATAL)); } - private void pushToAllAppender (@Nonnull Log log) { + public void trace (Message message) { + pushToAllAppender(new Log(1, message, LogLevels.TRACE)); + } + + public void debug (Message message) { + pushToAllAppender(new Log(1, message, LogLevels.DEBUG)); + } + + public void info (Message message) { + pushToAllAppender(new Log(1, message, LogLevels.INFO)); + } + + public void warn (Message message) { + pushToAllAppender(new Log(1, message, LogLevels.WARN)); + } + + public void warning (Message message) { + pushToAllAppender(new Log(1, message, LogLevels.WARN)); + } + + public void error (Message message) { + pushToAllAppender(new Log(1, message, LogLevels.ERROR)); + } + + public void fatal (Message message) { + pushToAllAppender(new Log(1, message, LogLevels.FATAL)); + } + + public void log (String message, ILogLevel level) { + pushToAllAppender(new Log(1, new Message(message), level)); + } + + public void log (Message message, ILogLevel level) { + pushToAllAppender(new Log(1, message, level)); + } + + protected void pushToAllAppender (@Nonnull Log log) { if (!levelSetting.checkLevel(log.level)) return; for (IAppender appender : appends) { appender.pushLog(log);