From 95dc5ac9769c61c4c9a596dd6d6989fdd6c37869 Mon Sep 17 00:00:00 2001 From: Nikita Fedkin Date: Tue, 18 Jul 2023 00:02:56 +0200 Subject: [PATCH 1/6] =?UTF-8?q?=D0=9D=D0=B0=D0=BC=D0=B5=D1=82=D0=BA=D0=B8?= =?UTF-8?q?=20=D0=BF=D0=BE=20=D0=B0=D0=BF=D0=BF=D0=B5=D0=BD=D0=B4=D0=B5?= =?UTF-8?q?=D1=80=D1=83=20=D0=B2=20language=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LanguageClientAwareAppender.java | 62 +++++++++++++++++++ ...guageClientAwareAppenderConfiguration.java | 34 ++++++++++ .../language-client-aware-appender.xml | 18 ++++++ src/main/resources/logback-spring.xml | 8 +++ 4 files changed, 122 insertions(+) create mode 100644 src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java create mode 100644 src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppenderConfiguration.java create mode 100644 src/main/resources/language-client-aware-appender.xml create mode 100644 src/main/resources/logback-spring.xml diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java new file mode 100644 index 00000000000..43140d6138d --- /dev/null +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java @@ -0,0 +1,62 @@ +/* + * This file is a part of BSL Language Server. + * + * Copyright (c) 2018-2023 + * Alexey Sosnoviy , Nikita Fedkin 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.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.eclipse.lsp4j.services.LanguageClient; + +import java.io.IOException; + +public class LanguageClientAwareAppender + extends ConsoleAppender { + + protected static LanguageClientAwareAppender INSTANCE; + + @Setter + @Nullable + private LanguageClientHolder clientHolder; + + public LanguageClientAwareAppender() { + super(); + // hacky hack + INSTANCE = this; + } + + @Override + protected void writeOut(ILoggingEvent event) throws IOException { + if (clientHolder != null && clientHolder.isConnected()) { + LanguageClient languageClient = clientHolder.getClient().orElseThrow(); + + var params = new MessageParams(MessageType.Info, event.getFormattedMessage()); + languageClient.logMessage(params); + + return; + } + super.writeOut(event); + } +} diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppenderConfiguration.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppenderConfiguration.java new file mode 100644 index 00000000000..6b664953116 --- /dev/null +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppenderConfiguration.java @@ -0,0 +1,34 @@ +/* + * This file is a part of BSL Language Server. + * + * Copyright (c) 2018-2023 + * Alexey Sosnoviy , Nikita Fedkin 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; + +@Configuration +public class LanguageClientAwareAppenderConfiguration { + + @Bean + public LanguageClientAwareAppender languageClientAwareAppender() { + return LanguageClientAwareAppender.INSTANCE; + } +} diff --git a/src/main/resources/language-client-aware-appender.xml b/src/main/resources/language-client-aware-appender.xml new file mode 100644 index 00000000000..199315fc5fd --- /dev/null +++ b/src/main/resources/language-client-aware-appender.xml @@ -0,0 +1,18 @@ + + + + + + + + ${CONSOLE_LOG_THRESHOLD} + + + ${CONSOLE_LOG_PATTERN} + ${CONSOLE_LOG_CHARSET} + + + \ No newline at end of file diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 00000000000..d32f81a917d --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file From e9aef17422bff759a4820b75502fdb7b483aa0ea Mon Sep 17 00:00:00 2001 From: Nikita Fedkin Date: Wed, 19 Jul 2023 23:34:58 +0200 Subject: [PATCH 2/6] =?UTF-8?q?=D0=92=D1=8B=D0=B2=D0=BE=D0=B4=20=D0=B8?= =?UTF-8?q?=D0=BD=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B2?= =?UTF-8?q?=20=D0=BD=D1=83=D0=B6=D0=BD=D0=BE=D0=BC=20=D1=83=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=BD=D0=B5=20=D0=BB=D0=BE=D0=B3=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LanguageClientAwareAppender.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java index 43140d6138d..d20e8ecdf15 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java @@ -21,6 +21,7 @@ */ 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; @@ -29,16 +30,25 @@ import org.eclipse.lsp4j.MessageParams; import org.eclipse.lsp4j.MessageType; import org.eclipse.lsp4j.services.LanguageClient; +import org.springframework.beans.factory.annotation.Autowired; import java.io.IOException; +import java.util.Map; public class LanguageClientAwareAppender extends ConsoleAppender { protected static LanguageClientAwareAppender INSTANCE; - @Setter + private static final Map loggingLevels = Map.of( + Level.DEBUG, MessageType.Log, + Level.ERROR, MessageType.Error, + Level.INFO, MessageType.Info, + Level.WARN, MessageType.Warning + ); + @Nullable + @Setter(onMethod_ = {@Autowired}) private LanguageClientHolder clientHolder; public LanguageClientAwareAppender() { @@ -52,7 +62,14 @@ protected void writeOut(ILoggingEvent event) throws IOException { if (clientHolder != null && clientHolder.isConnected()) { LanguageClient languageClient = clientHolder.getClient().orElseThrow(); - var params = new MessageParams(MessageType.Info, event.getFormattedMessage()); + var messageType = loggingLevels.getOrDefault(event.getLevel(), MessageType.Log); + String message = "[%s] [%s] [%s]: %s".formatted( + event.getLevel(), + event.getThreadName(), + event.getLoggerName(), + event.getFormattedMessage() + ); + var params = new MessageParams(messageType, message); languageClient.logMessage(params); return; From da60e680cd8a6288d361a5acee57f0b2323b4489 Mon Sep 17 00:00:00 2001 From: Nikita Fedkin Date: Wed, 26 Jul 2023 11:27:18 +0200 Subject: [PATCH 3/6] =?UTF-8?q?Info=20=D0=BA=D0=B0=D0=BA=20=D1=83=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B5=D0=BD=D1=8C=20=D0=BB=D0=BE=D0=B3=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=BF=D0=BE=20=D1=83?= =?UTF-8?q?=D0=BC=D0=BE=D0=BB=D1=87=D0=B0=D0=BD=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/logback-spring.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index d32f81a917d..be77b622cab 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -2,7 +2,7 @@ - + - \ No newline at end of file + From 3bd07d3641b1b39a725908729ce3a6bb7815ef85 Mon Sep 17 00:00:00 2001 From: Nikita Fedkin Date: Wed, 26 Jul 2023 21:57:00 +0200 Subject: [PATCH 4/6] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3,=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4=20?= =?UTF-8?q?=D0=B4=D0=B0=D1=82=D1=8B,=20javadoc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../LanguageClientAwareAppender.java | 32 ++++++++++++++++--- ...uration.java => LogbackConfiguration.java} | 8 ++++- .../language-client-aware-appender.xml | 6 ---- 3 files changed, 35 insertions(+), 11 deletions(-) rename src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/{LanguageClientAwareAppenderConfiguration.java => LogbackConfiguration.java} (84%) diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java index d20e8ecdf15..56a77678a51 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppender.java @@ -29,42 +29,66 @@ import lombok.Setter; import org.eclipse.lsp4j.MessageParams; import org.eclipse.lsp4j.MessageType; -import org.eclipse.lsp4j.services.LanguageClient; 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 { + /** + * Singletone-like хранилище проинициализированного инфраструктурой Logback аппендера + * для последующего возврата его через {@link LogbackConfiguration#languageClientAwareAppender()}. + */ protected static LanguageClientAwareAppender INSTANCE; private static final Map loggingLevels = Map.of( - Level.DEBUG, MessageType.Log, + 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; + /** + * Конструктор по умолчанию. + *

+ * Сохраняет сконструированный объект в переменную {@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()) { - LanguageClient languageClient = clientHolder.getClient().orElseThrow(); + var languageClient = clientHolder.getClient().orElseThrow(); var messageType = loggingLevels.getOrDefault(event.getLevel(), MessageType.Log); - String message = "[%s] [%s] [%s]: %s".formatted( + String message = "[%s - %s] [%s] [%s]: %s".formatted( event.getLevel(), + event.getInstant(), event.getThreadName(), event.getLoggerName(), event.getFormattedMessage() diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppenderConfiguration.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LogbackConfiguration.java similarity index 84% rename from src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppenderConfiguration.java rename to src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LogbackConfiguration.java index 6b664953116..eee439b6cbc 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LanguageClientAwareAppenderConfiguration.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/infrastructure/LogbackConfiguration.java @@ -24,9 +24,15 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +/** + * Spring-конфигурация для настройки logback. + */ @Configuration -public class LanguageClientAwareAppenderConfiguration { +public class LogbackConfiguration { + /** + * @return Настроенный аппендер сообщений в LanguageClient. + */ @Bean public LanguageClientAwareAppender languageClientAwareAppender() { return LanguageClientAwareAppender.INSTANCE; diff --git a/src/main/resources/language-client-aware-appender.xml b/src/main/resources/language-client-aware-appender.xml index 199315fc5fd..4f8d680ab46 100644 --- a/src/main/resources/language-client-aware-appender.xml +++ b/src/main/resources/language-client-aware-appender.xml @@ -1,10 +1,4 @@ - - - From e577e4af03b2c29b47d919eeebac287f90a4a26f Mon Sep 17 00:00:00 2001 From: Nikita Fedkin Date: Wed, 26 Jul 2023 21:58:08 +0200 Subject: [PATCH 5/6] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=D0=B0=20todo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/computer/DiagnosticComputer.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/context/computer/DiagnosticComputer.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/context/computer/DiagnosticComputer.java index 6a91c3d43ab..d1f2aa7d549 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/context/computer/DiagnosticComputer.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/context/computer/DiagnosticComputer.java @@ -47,14 +47,7 @@ public List compute(DocumentContext documentContext) { try { return diagnostic.getDiagnostics(documentContext).stream(); } catch (RuntimeException e) { - /* - TODO: - В случае если подключен ленг клиент, то логирование ошибки в консоль приводит к падению сервера - т.к данный лог идёт в выхлоп не по протоколу. - Требуется обернуть логгер в случае подключенного ленг клиента и слать прокольное событие - которое запишет ошибку в лог. - */ - String message = String.format( + var message = String.format( "Diagnostic computation error.%nFile: %s%nDiagnostic: %s", documentContext.getUri(), diagnostic.getInfo().getCode() From 7bc0f54d2e9969bb2db3a8fa972dfbc082a3412b Mon Sep 17 00:00:00 2001 From: Nikita Fedkin Date: Fri, 28 Jul 2023 21:45:12 +0200 Subject: [PATCH 6/6] =?UTF-8?q?=D0=9E=D1=82=D0=B2=D1=8F=D0=B7=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=82=20CommandLineRunner=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BE=D1=81=D0=BD=D0=BE=D0=B2=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Чтобы исключить запуск сервера каждый раз при поднятии спринг-контекста. --- .../_1c_syntax/bsl/languageserver/BSLLSPLauncher.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/_1c_syntax/bsl/languageserver/BSLLSPLauncher.java b/src/main/java/com/github/_1c_syntax/bsl/languageserver/BSLLSPLauncher.java index a2a9e028f2f..0f6cfb922d6 100644 --- a/src/main/java/com/github/_1c_syntax/bsl/languageserver/BSLLSPLauncher.java +++ b/src/main/java/com/github/_1c_syntax/bsl/languageserver/BSLLSPLauncher.java @@ -28,7 +28,6 @@ import com.github._1c_syntax.bsl.languageserver.cli.WebsocketCommand; import com.github._1c_syntax.utils.CaseInsensitivePattern; import lombok.RequiredArgsConstructor; -import org.springframework.boot.CommandLineRunner; import org.springframework.boot.ExitCodeGenerator; import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; @@ -71,7 +70,7 @@ havingValue = "true", matchIfMissing = true) @RequiredArgsConstructor -public class BSLLSPLauncher implements Callable, CommandLineRunner, ExitCodeGenerator { +public class BSLLSPLauncher implements Callable, ExitCodeGenerator { private static final String DEFAULT_COMMAND = "lsp"; @@ -106,7 +105,10 @@ public static void main(String[] args) { var applicationContext = new SpringApplicationBuilder(BSLLSPLauncher.class) .web(getWebApplicationType(args)) .run(args); + var launcher = applicationContext.getBean(BSLLSPLauncher.class); + launcher.run(args); + if (launcher.getExitCode() >= 0) { System.exit( SpringApplication.exit(applicationContext) @@ -114,7 +116,6 @@ public static void main(String[] args) { } } - @Override public void run(String... args) { var cmd = new CommandLine(this, picocliFactory);