mirror of
https://github.com/suk-ws/messiva.git
synced 2024-11-22 16:54:51 +08:00
add ILogLevel as new LogLevel abstract, better Logger
- add ILogLevel as new LogLevel abstract - all reference to LogLevel have been changed to reference ILogLevel - add (Message) parameter set to ILogLevelImpl and Logger - add (String, ILogLevel) and (Message, ILogLevel) to Logger
This commit is contained in:
parent
ab95aa71d7
commit
f4697a45c0
@ -1,6 +1,6 @@
|
||||
## Messiva
|
||||
|
||||
PROJECT_VERSION = 0.1.1
|
||||
PROJECT_VERSION = 0.2.0
|
||||
|
||||
## dependencies
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
6
src/main/java/cc/sukazyo/messiva/log/ILogLevel.java
Normal file
6
src/main/java/cc/sukazyo/messiva/log/ILogLevel.java
Normal file
@ -0,0 +1,6 @@
|
||||
package cc.sukazyo.messiva.log;
|
||||
|
||||
public interface ILogLevel {
|
||||
float level();
|
||||
String tag();
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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; }
|
||||
|
||||
}
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user