提交 4fe4bde0 编写于 作者: 如梦技术's avatar 如梦技术 🐛

新增一个 mica-tinylog 组件。

上级 35d75f79
......@@ -4,10 +4,10 @@ plugins {
ext {
javaVersion = JavaVersion.VERSION_1_8
springBootVersion = "2.6.2"
springBootVersion = "2.6.3"
springNativeVersion = "0.10.5"
springCloudVersion = "2021.0.0"
micaAutoVersion = "2.2.1"
micaAutoVersion = "2.2.2"
micaWeiXinVersion = "2.1.2"
knife4jVersion = "3.0.3"
springdocVersion = "1.5.10"
......@@ -24,6 +24,7 @@ ext {
zxingVersion = "3.4.1"
jetCacheVersion = "2.6.2"
jfinalVersion = "4.9.16"
tinylogVersion = "2.4.1"
}
configure(subprojects) {
......
......@@ -30,6 +30,7 @@ dependencyManagement {
dependency "net.dreamlu:mica-metrics:${VERSION}"
dependency "net.dreamlu:mica-caffeine:${VERSION}"
dependency "net.dreamlu:mica-logging:${VERSION}"
dependency "net.dreamlu:mica-tinylog:${VERSION}"
dependency "net.dreamlu:mica-qrcode:${VERSION}"
dependency "net.dreamlu:mica-jetcache:${VERSION}"
dependency "net.dreamlu:mica-lite:${VERSION}"
......
# mica-tinylog 组件
## 说明
`tinylog` 组件。支持 Spring boot 配置,支持启动完毕后关闭 Console,
## 使用
### maven
```xml
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-tinylog</artifactId>
<version>${version}</version>
</dependency>
```
### gradle
```groovy
compile("net.dreamlu:mica-tinylog:${version}")
```
dependencies {
implementation "org.tinylog:slf4j-tinylog:${tinylogVersion}"
implementation "org.tinylog:tinylog-impl:${tinylogVersion}"
implementation "org.springframework.boot:spring-boot"
compileOnly "org.springframework.cloud:spring-cloud-context"
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor:$springBootVersion"
annotationProcessor "org.springframework.boot:spring-boot-autoconfigure-processor:$springBootVersion"
}
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.mica.tinylog.config;
import net.dreamlu.mica.tinylog.listener.TinyLogStartedEventListener;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
/**
* logging 日志配置
*
* @author L.cm
*/
@Order(Ordered.HIGHEST_PRECEDENCE)
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(MicaTinyLogProperties.class)
public class MicaTinyLogConfiguration {
@Bean
public TinyLogStartedEventListener loggingStartedEventListener(MicaTinyLogProperties properties) {
return new TinyLogStartedEventListener(properties);
}
}
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.mica.tinylog.config;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
/**
* logging 配置
*
* @author L.cm
*/
@Getter
@Setter
@RefreshScope
@ConfigurationProperties(MicaTinyLogProperties.PREFIX)
public class MicaTinyLogProperties {
public static final String PREFIX = "mica.tinylog";
private final Console console = new Console();
@Getter
@Setter
public static class Console {
/**
* 是否启动完成后关闭控制台日志,适用于,正式环境
*/
private boolean closeAfterStart = false;
}
}
/*
* Copyright (c) 2019-2029, Dreamlu (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.mica.tinylog.core;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.logging.*;
import org.springframework.boot.logging.logback.LogbackLoggingSystem;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.Environment;
import org.springframework.util.ClassUtils;
import org.tinylog.Level;
import org.tinylog.configuration.Configuration;
/**
* TinyLog LoggingSystem
*
* @author L.cm
*/
public class TinyLogLoggingSystem extends Slf4JLoggingSystem {
private static final LogLevels<Level> LEVELS = new LogLevels<>();
static {
LEVELS.map(LogLevel.TRACE, Level.TRACE);
LEVELS.map(LogLevel.DEBUG, Level.DEBUG);
LEVELS.map(LogLevel.INFO, Level.INFO);
LEVELS.map(LogLevel.WARN, Level.WARN);
LEVELS.map(LogLevel.ERROR, Level.ERROR);
LEVELS.map(LogLevel.FATAL, Level.ERROR);
LEVELS.map(LogLevel.OFF, Level.OFF);
}
public TinyLogLoggingSystem(ClassLoader classLoader) {
super(classLoader);
}
@Override
protected String[] getStandardConfigLocations() {
return new String[]{"tinylog-dev.properties", "tinylog-test.properties", "tinylog.properties"};
}
@Override
protected void loadDefaults(LoggingInitializationContext initializationContext, LogFile logFile) {
Environment environment = initializationContext.getEnvironment();
Binder.get(environment)
.bind("tinylog", Bindable.mapOf(String.class, String.class))
.ifBound(props -> props.forEach(Configuration::set));
}
/**
* {@link LoggingSystemFactory} that returns {@link LogbackLoggingSystem} if possible.
*/
@Order
public static class Factory implements LoggingSystemFactory {
private static final boolean PRESENT = ClassUtils.isPresent("org.tinylog.Logger",
LogbackLoggingSystem.Factory.class.getClassLoader());
@Override
public LoggingSystem getLoggingSystem(ClassLoader classLoader) {
if (PRESENT) {
return new LogbackLoggingSystem(classLoader);
}
return null;
}
}
}
/*
* Copyright (c) 2019-2029, Dreamlu 卢春梦 (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.mica.tinylog.listener;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.tinylog.config.MicaTinyLogProperties;
import net.dreamlu.mica.tinylog.writers.ConsoleWriter;
import org.springframework.boot.web.context.WebServerInitializedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
/**
* 项目启动事件通知
*
* @author L.cm
*/
@RequiredArgsConstructor
public class TinyLogStartedEventListener {
private final MicaTinyLogProperties properties;
@Async
@Order
@EventListener(WebServerInitializedEvent.class)
public void afterStart() {
// 1. 关闭控制台
MicaTinyLogProperties.Console console = properties.getConsole();
if (console.isCloseAfterStart()) {
ConsoleWriter.setEnabled(false);
}
}
}
/*
* Copyright (c) 2019-2029, Dreamlu (596392912@qq.com & www.dreamlu.net).
* <p>
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.dreamlu.mica.tinylog.writers;
import org.tinylog.core.LogEntry;
import org.tinylog.core.LogEntryValue;
import org.tinylog.writers.Writer;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
/**
* console Writer
*
* @author L.cm
*/
public class ConsoleWriter implements Writer {
private final org.tinylog.writers.ConsoleWriter consoleWriter;
private static volatile boolean enabled = true;
public ConsoleWriter() {
this(Collections.emptyMap());
}
public ConsoleWriter(final Map<String, String> properties) {
this.consoleWriter = new org.tinylog.writers.ConsoleWriter(properties);
}
@Override
public Collection<LogEntryValue> getRequiredLogEntryValues() {
return consoleWriter.getRequiredLogEntryValues();
}
@Override
public void write(LogEntry logEntry) throws Exception {
// 判断是否开启 控制台日志
if (enabled) {
consoleWriter.write(logEntry);
}
}
public static void setEnabled(boolean enabled) {
ConsoleWriter.enabled = enabled;
}
@Override
public void flush() throws Exception {
}
@Override
public void close() throws Exception {
}
}
# TinyLog Logging Systems
org.springframework.boot.logging.LoggingSystemFactory=\
net.dreamlu.mica.tinylog.core.TinyLogLoggingSystem.Factory
package net.dreamlu.mica.tinylog.test;
import org.tinylog.Logger;
public class TinyLogTest {
public static void main(String[] args) {
Logger.info("123123");
}
}
writer = console
writer.format = {date: HH:mm:ss.SSS} {level}: {message}
......@@ -19,3 +19,4 @@ include "mica-jetcache"
include "mica-lite"
include "mica-activerecord"
include "mica-prometheus"
include "mica-tinylog"
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册