1
0
mirror of https://github.com/suk-ws/messiva.git synced 2024-11-22 22:37:36 +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:
A.C.Sukazyo Eyre 2023-10-15 12:40:47 +08:00
parent ab95aa71d7
commit f4697a45c0
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
11 changed files with 122 additions and 48 deletions

View File

@ -1,6 +1,6 @@
## Messiva ## Messiva
PROJECT_VERSION = 0.1.1 PROJECT_VERSION = 0.2.0
## dependencies ## dependencies

View File

@ -2,8 +2,8 @@ package cc.sukazyo.messiva.appender;
import cc.sukazyo.messiva.component.LevelRestrictComponent; import cc.sukazyo.messiva.component.LevelRestrictComponent;
import cc.sukazyo.messiva.formatter.ILogFormatter; import cc.sukazyo.messiva.formatter.ILogFormatter;
import cc.sukazyo.messiva.log.ILogLevel;
import cc.sukazyo.messiva.log.Log; import cc.sukazyo.messiva.log.Log;
import cc.sukazyo.messiva.log.LogLevel;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -22,13 +22,13 @@ public abstract class AppenderRestrictableByLevel extends Appender {
} }
@Nonnull @Nonnull
public AppenderRestrictableByLevel minLevel(@Nonnull LogLevel minLevel) { public AppenderRestrictableByLevel minLevel(@Nonnull ILogLevel minLevel) {
levelSetting.minLevel(minLevel); levelSetting.minLevel(minLevel);
return this; return this;
} }
@Nonnull @Nonnull
public AppenderRestrictableByLevel maxLevel(@Nonnull LogLevel maxLevel) { public AppenderRestrictableByLevel maxLevel(@Nonnull ILogLevel maxLevel) {
levelSetting.maxLevel(maxLevel); levelSetting.maxLevel(maxLevel);
return this; return this;
} }

View File

@ -2,10 +2,11 @@ package cc.sukazyo.messiva.appender;
import cc.sukazyo.messiva.formatter.ILogFormatter; import cc.sukazyo.messiva.formatter.ILogFormatter;
import cc.sukazyo.messiva.log.Log; import cc.sukazyo.messiva.log.Log;
import cc.sukazyo.messiva.utils.StringUtils;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
public class ConsoleAppender extends AppenderRestrictableByLevel { public class ConsoleAppender extends AppenderRestrictableByLevel {

View File

@ -1,9 +1,11 @@
package cc.sukazyo.messiva.appender; package cc.sukazyo.messiva.appender;
import javax.annotation.Nonnull;
import cc.sukazyo.messiva.log.Log; import cc.sukazyo.messiva.log.Log;
public interface IAppender { public interface IAppender {
void pushLog (Log log); void pushLog (@Nonnull Log log);
} }

View File

@ -1,51 +1,52 @@
package cc.sukazyo.messiva.component; 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; import javax.annotation.Nonnull;
public class LevelRestrictComponent { public class LevelRestrictComponent {
@Nonnull private LogLevel minLevel; @Nonnull private ILogLevel minLevel;
@Nonnull private LogLevel maxLevel; @Nonnull private ILogLevel maxLevel;
public LevelRestrictComponent (@Nonnull LogLevel minLevel, @Nonnull LogLevel maxLevel) { public LevelRestrictComponent (@Nonnull ILogLevel minLevel, @Nonnull ILogLevel maxLevel) {
this.minLevel = minLevel; this.minLevel = minLevel;
this.maxLevel = maxLevel; this.maxLevel = maxLevel;
} }
public LevelRestrictComponent (@Nonnull LogLevel minLevel) { public LevelRestrictComponent (@Nonnull ILogLevel minLevel) {
this(minLevel, LogLevel.NONE); this(minLevel, LogLevels.NONE);
} }
public LevelRestrictComponent () { public LevelRestrictComponent () {
this(LogLevel.ALL); this(LogLevels.ALL);
} }
@Nonnull @Nonnull
public LevelRestrictComponent minLevel (@Nonnull LogLevel minLevel) { public LevelRestrictComponent minLevel (@Nonnull ILogLevel minLevel) {
this.minLevel = minLevel; this.minLevel = minLevel;
return this; return this;
} }
@Nonnull @Nonnull
public LevelRestrictComponent maxLevel (@Nonnull LogLevel maxLevel) { public LevelRestrictComponent maxLevel (@Nonnull ILogLevel maxLevel) {
this.maxLevel = maxLevel; this.maxLevel = maxLevel;
return this; return this;
} }
@Nonnull @Nonnull
public LogLevel minLevel () { public ILogLevel minLevel () {
return this.minLevel; return this.minLevel;
} }
@Nonnull @Nonnull
public LogLevel maxLevel () { public ILogLevel maxLevel () {
return this.maxLevel; return this.maxLevel;
} }
public boolean checkLevel (@Nonnull LogLevel level) { public boolean checkLevel (@Nonnull ILogLevel level) {
return level.level >= minLevel.level && level.level <= maxLevel.level; return level.level() >= minLevel.level() && level.level() <= maxLevel.level();
} }
} }

View File

@ -22,10 +22,10 @@ public class SimpleFormatter implements ILogFormatter {
message.append(startTimestamp).append(log.timestamp).append(endTimestamp) message.append(startTimestamp).append(log.timestamp).append(endTimestamp)
.append(startThreadName).append(log.thread.getName()).append(endThreadName); .append(startThreadName).append(log.thread.getName()).append(endThreadName);
final String promptNewline = StringUtils.repeatChar(followingLineFillChar, message.length()); 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++) { for (int i = 1; i < log.message.message.length; i++) {
message.append('\n').append(promptNewline) 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(); return message.toString();
} }

View File

@ -0,0 +1,6 @@
package cc.sukazyo.messiva.log;
public interface ILogLevel {
float level();
String tag();
}

View File

@ -16,4 +16,18 @@ public interface ILogLevelImpl {
void fatal (String message); 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);
} }

View File

@ -1,16 +1,20 @@
package cc.sukazyo.messiva.log; package cc.sukazyo.messiva.log;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import cc.sukazyo.messiva.utils.StackUtils; import cc.sukazyo.messiva.utils.StackUtils;
public class Log { public class Log {
public final Message message; @Nonnull public final Message message;
public final LogLevel level; @Nonnull public final ILogLevel level;
public final long timestamp; public final long timestamp;
public final Thread thread; @Nonnull public final Thread thread;
public final StackTraceElement[] stackTrace; @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.message = message;
this.level = level; this.level = level;
this.thread = thread; this.thread = thread;
@ -18,27 +22,34 @@ public class Log {
this.stackTrace = stackTrace; 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)); 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); 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()); 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); 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()); 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); this(1, message, level);
} }

View File

@ -1,6 +1,6 @@
package cc.sukazyo.messiva.log; package cc.sukazyo.messiva.log;
public enum LogLevel { public enum LogLevels implements ILogLevel {
ALL (-Float.MAX_VALUE, "####"), ALL (-Float.MAX_VALUE, "####"),
TRACE(-1f, "TRAC"), TRACE(-1f, "TRAC"),
@ -14,9 +14,15 @@ public enum LogLevel {
public final float level; public final float level;
public final String tag; public final String tag;
LogLevel (float level, String tag) { LogLevels (float level, String tag) {
this.level = level; this.level = level;
this.tag = tag; this.tag = tag;
} }
@Override
public float level () { return this.level; }
@Override
public String tag () { return this.tag; }
} }

View File

@ -2,10 +2,7 @@ package cc.sukazyo.messiva.logger;
import cc.sukazyo.messiva.component.LevelRestrictComponent; import cc.sukazyo.messiva.component.LevelRestrictComponent;
import cc.sukazyo.messiva.appender.IAppender; import cc.sukazyo.messiva.appender.IAppender;
import cc.sukazyo.messiva.log.ILogLevelImpl; import cc.sukazyo.messiva.log.*;
import cc.sukazyo.messiva.log.Log;
import cc.sukazyo.messiva.log.LogLevel;
import cc.sukazyo.messiva.log.Message;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import java.util.ArrayList; import java.util.ArrayList;
@ -28,45 +25,81 @@ public class Logger implements ILogLevelImpl {
this.appends.addAll(Arrays.asList(appends)); this.appends.addAll(Arrays.asList(appends));
} }
public Logger minLevel (@Nonnull LogLevel minLevel) { public Logger minLevel (@Nonnull ILogLevel minLevel) {
levelSetting.minLevel(minLevel); levelSetting.minLevel(minLevel);
return this; return this;
} }
public Logger maxLevel (@Nonnull LogLevel maxLevel) { public Logger maxLevel (@Nonnull ILogLevel maxLevel) {
levelSetting.maxLevel(maxLevel); levelSetting.maxLevel(maxLevel);
return this; return this;
} }
public void trace (String message) { 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) { 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) { 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) { 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) { 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) { 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) { 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; if (!levelSetting.checkLevel(log.level)) return;
for (IAppender appender : appends) { for (IAppender appender : appends) {
appender.pushLog(log); appender.pushLog(log);