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

mica-logging 重新设计 gitee #I3CVAT

上级 7997bb99
/*
* 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.logging.appender;
import ch.qos.logback.classic.LoggerContext;
/**
* logging Appender 抽象
*
* @author L.cm
*/
public interface ILoggingAppender {
/**
* all.log
*/
String LOG_FILE_ALL = "all.log";
/**
* error.log
*/
String LOG_FILE_ERROR = "error.log";
/**
* 启动
*
* @param context LoggerContext
*/
void start();
void start(LoggerContext context);
/**
* 重置
*
* @param context LoggerContext
*/
void reset();
void reset(LoggerContext context);
}
/*
* 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.logging.appender;
import ch.qos.logback.classic.LoggerContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.logging.config.MicaLoggingProperties;
/**
* 纯文件日志输出,all.log 和 error.log
*
* @author L.cm
*/
@Slf4j
@RequiredArgsConstructor
public class LoggingFileAppender implements ILoggingAppender {
private final MicaLoggingProperties properties;
@Override
public void start() {
public void start(LoggerContext context) {
}
@Override
public void reset() {
public void reset(LoggerContext context) {
}
}
/*
* 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.logging.appender;
import ch.qos.logback.classic.LoggerContext;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.logging.config.MicaLoggingProperties;
/**
* json 日志输出,json 日志只输出 all.log
*
* @author L.cm
*/
@RequiredArgsConstructor
public class LoggingJsonFileAppender implements ILoggingAppender {
private final MicaLoggingProperties properties;
@Override
public void start() {
public void start(LoggerContext context) {
}
@Override
public void reset() {
public void reset(LoggerContext context) {
}
}
/*
* 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.logging.appender;
import ch.qos.logback.classic.LoggerContext;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.logging.config.MicaLoggingProperties;
/**
* LogStash 输出 json
*
* @author L.cm
*/
@RequiredArgsConstructor
public class LoggingLogStashAppender implements ILoggingAppender {
private final MicaLoggingProperties properties;
@Override
public void start() {
public void start(LoggerContext context) {
}
@Override
public void reset() {
public void reset(LoggerContext context) {
}
}
......@@ -17,6 +17,7 @@
package net.dreamlu.mica.logging.config;
import net.dreamlu.mica.auto.annotation.AutoContextInitializer;
import net.dreamlu.mica.logging.appender.ILoggingAppender;
import net.dreamlu.mica.logging.utils.LoggingUtil;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
......@@ -37,7 +38,7 @@ public class LoggingInitializer implements ApplicationContextInitializer<Configu
// 读取系统配置的日志目录,默认为项目下 logs
String logBase = environment.getProperty("logging.file.path", LoggingUtil.DEFAULT_LOG_DIR);
// 用于 spring boot admin 中展示日志
System.setProperty("logging.file.name", logBase + "/${spring.application.name}/all.log");
System.setProperty("logging.file.name", logBase + "/${spring.application.name}/" + ILoggingAppender.LOG_FILE_ALL);
}
@Override
......
......@@ -66,7 +66,7 @@ public class MicaLoggingConfiguration {
customFields.put("profile", profile);
String customFieldsJson = JsonUtil.toJson(customFields);
// 是否采用 json 格式化
boolean useJsonFormat = loggingProperties.isUseJsonFormat();
boolean useJsonFormat = loggingProperties.getFiles().isUseJsonFormat();
if (logStashProperties.isEnabled()) {
LoggingUtil.addLogStashTcpSocketAppender(context, customFieldsJson, logStashProperties);
} else {
......
......@@ -33,10 +33,6 @@ import org.springframework.cloud.context.config.annotation.RefreshScope;
public class MicaLoggingProperties {
public static final String PREFIX = "mica.logging";
/**
* 使用 json 格式化
*/
private boolean useJsonFormat = false;
private final Console console = new Console();
private final Files files = new Files();
private final Logstash logstash = new Logstash();
......@@ -58,6 +54,10 @@ public class MicaLoggingProperties {
* 是否开启文件日志
*/
private boolean enabled = true;
/**
* 使用 json 格式化
*/
private boolean useJsonFormat = false;
}
@Getter
......
/*
* 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.logging.listener;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.spi.ContextAwareBase;
import lombok.RequiredArgsConstructor;
import net.dreamlu.mica.logging.appender.ILoggingAppender;
import java.util.List;
/**
* Logback configuration is achieved by configuration file and API.
* When configuration file change is detected, the configuration is reset.
* This listener ensures that the programmatic configuration is also re-applied after reset.
*
* @author L.cm
*/
@RequiredArgsConstructor
public class LogbackLoggerContextListener extends ContextAwareBase implements LoggerContextListener {
private final List<ILoggingAppender> appenderList;
@Override
public boolean isResetResistant() {
return true;
}
@Override
public void onStart(LoggerContext context) {
for (ILoggingAppender appender : appenderList) {
appender.start(context);
}
}
@Override
public void onReset(LoggerContext context) {
for (ILoggingAppender appender : appenderList) {
appender.reset(context);
}
}
@Override
public void onStop(LoggerContext context) {
// Nothing to do.
}
@Override
public void onLevelChange(Logger logger, Level level) {
// Nothing to do.
}
}
......@@ -335,7 +335,7 @@ public class LoggingUtil {
if (this.loggingProperties.getLogstash().isEnabled()) {
addLogStashTcpSocketAppender(context, customFields, loggingProperties.getLogstash());
} else {
boolean useJsonFormat = loggingProperties.isUseJsonFormat();
boolean useJsonFormat = loggingProperties.getFiles().isUseJsonFormat();
addFileAppender(context, logFile, logErrorFile, useJsonFormat, customFields);
}
}
......@@ -345,7 +345,7 @@ public class LoggingUtil {
if (this.loggingProperties.getLogstash().isEnabled()) {
addLogStashTcpSocketAppender(context, customFields, loggingProperties.getLogstash());
} else {
boolean useJsonFormat = loggingProperties.isUseJsonFormat();
boolean useJsonFormat = loggingProperties.getFiles().isUseJsonFormat();
addFileAppender(context, logFile, logErrorFile, useJsonFormat, customFields);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册