mirror of
https://github.com/suk-ws/messiva.git
synced 2024-11-22 21:54:50 +08:00
建立简单Log数据模型和输出模型
- 基础 Log 模型,记录了消息和时间戳,线程,等级,调用堆栈跟踪 - 同时消息以 Message 封装 - 输出器接口用以Log对象输出,和基于System.out.println的控制台输出器——包含预定的格式规范器 - 基础的 Logger,可以储存一些输出器,并根据信息字符串创建简单的消息对象分发到输出器中
This commit is contained in:
parent
4728636f63
commit
492a838d90
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
52
src/main/java/cc/sukazyo/messiva/Logger.java
Normal file
52
src/main/java/cc/sukazyo/messiva/Logger.java
Normal file
@ -0,0 +1,52 @@
|
||||
package cc.sukazyo.messiva;
|
||||
|
||||
import cc.sukazyo.messiva.appender.IAppender;
|
||||
import cc.sukazyo.messiva.log.Log;
|
||||
import cc.sukazyo.messiva.log.LogLevel;
|
||||
import cc.sukazyo.messiva.log.Message;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Logger {
|
||||
|
||||
public final List<IAppender> appends = new ArrayList<>();
|
||||
|
||||
public Logger () {}
|
||||
|
||||
public Logger (IAppender... appends) {
|
||||
this.appends.addAll(Arrays.asList(appends));
|
||||
}
|
||||
|
||||
public void trace (String message) {
|
||||
pushToAllAppender(new Log(1, new Message(message), LogLevel.TRACE));
|
||||
}
|
||||
|
||||
public void info (String message) {
|
||||
pushToAllAppender(new Log(1, new Message(message), LogLevel.INFO));
|
||||
}
|
||||
|
||||
public void warn (String message) {
|
||||
pushToAllAppender(new Log(1, new Message(message), LogLevel.WARN));
|
||||
}
|
||||
|
||||
public void warning (String message) {
|
||||
pushToAllAppender(new Log(1, new Message(message), LogLevel.WARN));
|
||||
}
|
||||
|
||||
public void error (String message) {
|
||||
pushToAllAppender(new Log(1, new Message(message), LogLevel.ERROR));
|
||||
}
|
||||
|
||||
public void fatal (String message) {
|
||||
pushToAllAppender(new Log(1, new Message(message), LogLevel.FATAL));
|
||||
}
|
||||
|
||||
private void pushToAllAppender (Log log) {
|
||||
for (IAppender appender : appends) {
|
||||
appender.pushLog(log);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package cc.sukazyo.messiva.appender;
|
||||
|
||||
import cc.sukazyo.messiva.log.Log;
|
||||
import cc.sukazyo.messiva.utils.StringUtils;
|
||||
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class ConsoleAppender implements IAppender {
|
||||
|
||||
private final ReentrantLock syncLock = new ReentrantLock();
|
||||
|
||||
// private final AppendDaemon DAEMON;
|
||||
// private static class AppendDaemon extends Thread {
|
||||
// public AppendDaemon () { this.setName("console-appender-daemon::" + UUID.randomUUID()); }
|
||||
// @Override public void run () {
|
||||
//
|
||||
// }
|
||||
// }
|
||||
//
|
||||
public ConsoleAppender() {
|
||||
// DAEMON = new AppendDaemon();
|
||||
// DAEMON.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pushLog (Log log) {
|
||||
syncLock.lock();
|
||||
System.out.println(formatMessage(log));
|
||||
syncLock.unlock();
|
||||
}
|
||||
|
||||
private static String formatMessage (Log log) {
|
||||
final StringBuilder message = new StringBuilder();
|
||||
message.append('[').append(log.timestamp)
|
||||
.append("][").append(log.thread.getName())
|
||||
.append("]");
|
||||
final String promptNewline = StringUtils.repeatChar('\'', message.length());
|
||||
message.append('[').append(log.level.tag).append(']').append(log.message.message[0]);
|
||||
for (int i = 1; i < log.message.message.length; i++) {
|
||||
message.append('\n').append(promptNewline)
|
||||
.append('[').append(log.level.tag).append(']').append(log.message.message[i]);
|
||||
}
|
||||
return message.toString();
|
||||
}
|
||||
|
||||
}
|
9
src/main/java/cc/sukazyo/messiva/appender/IAppender.java
Normal file
9
src/main/java/cc/sukazyo/messiva/appender/IAppender.java
Normal file
@ -0,0 +1,9 @@
|
||||
package cc.sukazyo.messiva.appender;
|
||||
|
||||
import cc.sukazyo.messiva.log.Log;
|
||||
|
||||
public interface IAppender {
|
||||
|
||||
void pushLog (Log log);
|
||||
|
||||
}
|
45
src/main/java/cc/sukazyo/messiva/log/Log.java
Normal file
45
src/main/java/cc/sukazyo/messiva/log/Log.java
Normal file
@ -0,0 +1,45 @@
|
||||
package cc.sukazyo.messiva.log;
|
||||
|
||||
import cc.sukazyo.messiva.utils.StackUtils;
|
||||
|
||||
public class Log {
|
||||
|
||||
public final Message message;
|
||||
public final LogLevel level;
|
||||
public final long timestamp;
|
||||
public final Thread thread;
|
||||
public final StackTraceElement[] stackTrace;
|
||||
|
||||
public Log (Message message, LogLevel level, Thread thread, long timestamp, StackTraceElement[] stackTrace) {
|
||||
this.message = message;
|
||||
this.level = level;
|
||||
this.thread = thread;
|
||||
this.timestamp = timestamp;
|
||||
this.stackTrace = stackTrace;
|
||||
}
|
||||
|
||||
public Log (int stackOffset, Message message, LogLevel level, Thread thread, long timestamp) {
|
||||
this(message, level, thread, timestamp, StackUtils.getStackTrace(stackOffset+1));
|
||||
}
|
||||
|
||||
public Log (Message message, LogLevel level, Thread thread, long timestamp) {
|
||||
this(1, message, level, thread, timestamp);
|
||||
}
|
||||
|
||||
public Log (int stackOffset, Message message, LogLevel level, Thread thread) {
|
||||
this(stackOffset+1, message, level, thread, System.currentTimeMillis());
|
||||
}
|
||||
|
||||
public Log (Message message, LogLevel level, Thread thread) {
|
||||
this(1, message, level, thread);
|
||||
}
|
||||
|
||||
public Log (int stackOffset, Message message, LogLevel level) {
|
||||
this(stackOffset+1, message, level, Thread.currentThread());
|
||||
}
|
||||
|
||||
public Log (Message message, LogLevel level) {
|
||||
this(1, message, level);
|
||||
}
|
||||
|
||||
}
|
19
src/main/java/cc/sukazyo/messiva/log/LogLevel.java
Normal file
19
src/main/java/cc/sukazyo/messiva/log/LogLevel.java
Normal file
@ -0,0 +1,19 @@
|
||||
package cc.sukazyo.messiva.log;
|
||||
|
||||
public enum LogLevel {
|
||||
|
||||
TRACE(-1f, "TRAC"),
|
||||
INFO(0f, "INFO"),
|
||||
WARN(0.5f, "WARN"),
|
||||
ERROR(1f, "ERRO"),
|
||||
FATAL(10f, "FTAL");
|
||||
|
||||
public final float level;
|
||||
public final String tag;
|
||||
|
||||
LogLevel (float level, String tag) {
|
||||
this.level = level;
|
||||
this.tag = tag;
|
||||
}
|
||||
|
||||
}
|
11
src/main/java/cc/sukazyo/messiva/log/Message.java
Normal file
11
src/main/java/cc/sukazyo/messiva/log/Message.java
Normal file
@ -0,0 +1,11 @@
|
||||
package cc.sukazyo.messiva.log;
|
||||
|
||||
public class Message {
|
||||
|
||||
public final String[] message;
|
||||
|
||||
public Message(String message) {
|
||||
this.message = message.split("\n");
|
||||
}
|
||||
|
||||
}
|
14
src/main/java/cc/sukazyo/messiva/utils/StackUtils.java
Normal file
14
src/main/java/cc/sukazyo/messiva/utils/StackUtils.java
Normal file
@ -0,0 +1,14 @@
|
||||
package cc.sukazyo.messiva.utils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class StackUtils {
|
||||
|
||||
public static StackTraceElement[] getStackTrace (int offset) {
|
||||
offset += 2;
|
||||
StackTraceElement[] origins = Thread.currentThread().getStackTrace();
|
||||
origins = Arrays.copyOfRange(origins, offset, origins.length);
|
||||
return origins;
|
||||
}
|
||||
|
||||
}
|
15
src/main/java/cc/sukazyo/messiva/utils/StringUtils.java
Normal file
15
src/main/java/cc/sukazyo/messiva/utils/StringUtils.java
Normal file
@ -0,0 +1,15 @@
|
||||
package cc.sukazyo.messiva.utils;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
@Nonnull
|
||||
public static String repeatChar (char c, int i) {
|
||||
final char[] chars = new char[i];
|
||||
Arrays.fill(chars, c);
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user