-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Аппендер вывода логов в LanguageClient, если он подключен #3118
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
95dc5ac
Наметки по аппендеру в language client
nixel2007 e9aef17
Вывод информации в нужном уровне лога
nixel2007 da60e68
Info как уровень логирования по умолчанию
nixel2007 3bd07d3
Рефакторинг, вывод даты, javadoc
nixel2007 e577e4a
Убрана todo
nixel2007 fc6b506
Merge remote-tracking branch 'origin/develop' into feature/language-c…
nixel2007 7bc0f54
Отвязывание от CommandLineRunner для основного класса приложения
nixel2007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
фуфу. Говоришь что проверяешь наличие, а сам небезопасно Optional вскрываешь
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isConnected внутри проверяет наличие опшионала :)