-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3118 from 1c-syntax/feature/language-client-aware…
…-appender Аппендер вывода логов в LanguageClient, если он подключен
- Loading branch information
Showing
6 changed files
with
168 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
.../com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* This file is a part of BSL Language Server. | ||
* | ||
* Copyright (c) 2018-2023 | ||
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> and contributors | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* BSL Language Server is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3.0 of the License, or (at your option) any later version. | ||
* | ||
* BSL Language Server is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with BSL Language Server. | ||
*/ | ||
package com.github._1c_syntax.bsl.languageserver.infrastructure; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.ConsoleAppender; | ||
import com.github._1c_syntax.bsl.languageserver.LanguageClientHolder; | ||
import jakarta.annotation.Nullable; | ||
import lombok.Setter; | ||
import org.eclipse.lsp4j.MessageParams; | ||
import org.eclipse.lsp4j.MessageType; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* Расширение штатного {@link ConsoleAppender}, выводящего сообщения | ||
* в {@link org.eclipse.lsp4j.services.LanguageClient}, если он подключен, | ||
* или в штатные потоки вывода в обратном случае. | ||
*/ | ||
public class LanguageClientAwareAppender | ||
extends ConsoleAppender<ILoggingEvent> { | ||
|
||
/** | ||
* Singletone-like хранилище проинициализированного инфраструктурой Logback аппендера | ||
* для последующего возврата его через {@link LogbackConfiguration#languageClientAwareAppender()}. | ||
*/ | ||
protected static LanguageClientAwareAppender INSTANCE; | ||
|
||
private static final Map<Level, MessageType> loggingLevels = Map.of( | ||
Level.TRACE, MessageType.Log, | ||
Level.DEBUG, MessageType.Log, | ||
Level.ERROR, MessageType.Error, | ||
Level.INFO, MessageType.Info, | ||
Level.WARN, MessageType.Warning | ||
); | ||
|
||
/** | ||
* Хранилище возможно подключенного LanguageClient. | ||
*/ | ||
@Nullable | ||
@Setter(onMethod_ = {@Autowired}) | ||
private LanguageClientHolder clientHolder; | ||
|
||
/** | ||
* Конструктор по умолчанию. | ||
* <p> | ||
* Сохраняет сконструированный объект в переменную {@link LanguageClientAwareAppender#INSTANCE}. | ||
*/ | ||
public LanguageClientAwareAppender() { | ||
super(); | ||
// hacky hack | ||
INSTANCE = this; | ||
} | ||
|
||
/** | ||
* Общий метод вывода информации, проверяющий наличие подключенного LanguageClient. | ||
* | ||
* @param event Логируемое событие | ||
* @throws IOException Выбрасывает исключение в случае ошибок записи в стандартные потоки вывода. | ||
*/ | ||
@Override | ||
protected void writeOut(ILoggingEvent event) throws IOException { | ||
if (clientHolder != null && clientHolder.isConnected()) { | ||
var languageClient = clientHolder.getClient().orElseThrow(); | ||
|
||
var messageType = loggingLevels.getOrDefault(event.getLevel(), MessageType.Log); | ||
String message = "[%s - %s] [%s] [%s]: %s".formatted( | ||
event.getLevel(), | ||
event.getInstant(), | ||
event.getThreadName(), | ||
event.getLoggerName(), | ||
event.getFormattedMessage() | ||
); | ||
var params = new MessageParams(messageType, message); | ||
languageClient.logMessage(params); | ||
|
||
return; | ||
} | ||
super.writeOut(event); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...in/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LogbackConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* This file is a part of BSL Language Server. | ||
* | ||
* Copyright (c) 2018-2023 | ||
* Alexey Sosnoviy <[email protected]>, Nikita Fedkin <[email protected]> and contributors | ||
* | ||
* SPDX-License-Identifier: LGPL-3.0-or-later | ||
* | ||
* BSL Language Server is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3.0 of the License, or (at your option) any later version. | ||
* | ||
* BSL Language Server is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with BSL Language Server. | ||
*/ | ||
package com.github._1c_syntax.bsl.languageserver.infrastructure; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* Spring-конфигурация для настройки logback. | ||
*/ | ||
@Configuration | ||
public class LogbackConfiguration { | ||
|
||
/** | ||
* @return Настроенный аппендер сообщений в LanguageClient. | ||
*/ | ||
@Bean | ||
public LanguageClientAwareAppender languageClientAwareAppender() { | ||
return LanguageClientAwareAppender.INSTANCE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<included> | ||
<appender name="LANGUAGE_CLIENT_AWARE" class="com.github._1c_syntax.bsl.languageserver.infrastructure.LanguageClientAwareAppender"> | ||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | ||
<level>${CONSOLE_LOG_THRESHOLD}</level> | ||
</filter> | ||
<encoder> | ||
<pattern>${CONSOLE_LOG_PATTERN}</pattern> | ||
<charset>${CONSOLE_LOG_CHARSET}</charset> | ||
</encoder> | ||
</appender> | ||
</included> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/> | ||
<include resource="language-client-aware-appender.xml" /> | ||
<root level="INFO"> | ||
<appender-ref ref="LANGUAGE_CLIENT_AWARE" /> | ||
</root> | ||
</configuration> |