diff --git a/logviewer/frontend/index.html b/logviewer/frontend/index.html new file mode 100644 index 0000000000..627308e8d4 --- /dev/null +++ b/logviewer/frontend/index.html @@ -0,0 +1,29 @@ + + + + + + + + + + + + +
+ + diff --git a/logviewer/frontend/themes/logviewer/main-layout.css b/logviewer/frontend/themes/logviewer/main-layout.css new file mode 100644 index 0000000000..3477da5822 --- /dev/null +++ b/logviewer/frontend/themes/logviewer/main-layout.css @@ -0,0 +1,36 @@ +[slot='drawer'] { + background-image: linear-gradient(0deg, var(--lumo-shade-5pct), var(--lumo-shade-5pct)); +} + +[slot='drawer'] nav a { + text-decoration: none; + transition: color 140ms; +} + +[slot='drawer'] nav a .la { + margin-top: calc(var(--lumo-space-xs) * 0.5); +} + +[slot='drawer'] nav a::before { + border-radius: var(--lumo-border-radius); + bottom: calc(var(--lumo-space-xs) * 0.5); + content: ''; + left: 0; + position: absolute; + right: 0; + top: calc(var(--lumo-space-xs) * 0.5); + transition: background-color 140ms; +} + +[slot='drawer'] nav a[highlight] { + color: var(--lumo-primary-text-color); +} + +[slot='drawer'] nav a[highlight]::before { + background-color: var(--lumo-primary-color-10pct); +} + +[slot='drawer'] footer vaadin-context-menu { + align-items: center; + display: flex; +} diff --git a/logviewer/frontend/themes/logviewer/styles.css b/logviewer/frontend/themes/logviewer/styles.css new file mode 100644 index 0000000000..f7dfadb7ed --- /dev/null +++ b/logviewer/frontend/themes/logviewer/styles.css @@ -0,0 +1,4 @@ +/* Import your application global css files here or add the styles directly to this file */ +@import url('./main-layout.css'); +@import url('./views/logs-view.css'); +@import url('line-awesome/dist/line-awesome/css/line-awesome.min.css'); diff --git a/logviewer/frontend/themes/myapp/theme.json b/logviewer/frontend/themes/logviewer/theme.json similarity index 95% rename from logviewer/frontend/themes/myapp/theme.json rename to logviewer/frontend/themes/logviewer/theme.json index 88c4f9aae2..7ccf9d1c46 100644 --- a/logviewer/frontend/themes/myapp/theme.json +++ b/logviewer/frontend/themes/logviewer/theme.json @@ -1 +1 @@ -{"lumoImports":["typography","color","spacing","badge","utility"]} \ No newline at end of file +{"lumoImports":["typography","color","spacing","badge","utility"]} diff --git a/logviewer/frontend/themes/logviewer/views/logs-view.css b/logviewer/frontend/themes/logviewer/views/logs-view.css new file mode 100644 index 0000000000..aa8f478e95 --- /dev/null +++ b/logviewer/frontend/themes/logviewer/views/logs-view.css @@ -0,0 +1,39 @@ +.logs-view { + display: block; + padding: 1em; +} + +:root { + --color-info: rgba(120, 107, 213, 0.5); + --color-warn: rgba(170, 187, 97, 0.5); + --color-error: rgba(255, 75, 75, 0.5); + --color-debug: rgba(127, 255, 212, 0.5); + --color-trace: rgba(58, 118, 58, 0.5); +} + +p { + + line-height: normal; + margin-top: 0.1em; + margin-bottom: 0.1em; +} + +.error { + background-color: var(--color-error); +} + +.warn { + background-color: var(--color-warn); +} + +.info { + background-color: var(--color-info); +} + +.debug { + background-color: var(--color-debug); +} + +.trace { + background-color: var(--color-trace); +} diff --git a/logviewer/frontend/themes/myapp/main-layout.css b/logviewer/frontend/themes/myapp/main-layout.css deleted file mode 100644 index c69ed2c859..0000000000 --- a/logviewer/frontend/themes/myapp/main-layout.css +++ /dev/null @@ -1,36 +0,0 @@ -[slot='drawer'] { - background-image: linear-gradient(0deg, var(--lumo-shade-5pct), var(--lumo-shade-5pct)); -} - -[slot='drawer'] nav a { - text-decoration: none; - transition: color 140ms; -} - -[slot='drawer'] nav a .la { - margin-top: calc(var(--lumo-space-xs) * 0.5); -} - -[slot='drawer'] nav a::before { - border-radius: var(--lumo-border-radius); - bottom: calc(var(--lumo-space-xs) * 0.5); - content: ''; - left: 0; - position: absolute; - right: 0; - top: calc(var(--lumo-space-xs) * 0.5); - transition: background-color 140ms; -} - -[slot='drawer'] nav a[highlight] { - color: var(--lumo-primary-text-color); -} - -[slot='drawer'] nav a[highlight]::before { - background-color: var(--lumo-primary-color-10pct); -} - -[slot='drawer'] footer vaadin-context-menu { - align-items: center; - display: flex; -} diff --git a/logviewer/frontend/themes/myapp/styles.css b/logviewer/frontend/themes/myapp/styles.css deleted file mode 100644 index 9d00368c7b..0000000000 --- a/logviewer/frontend/themes/myapp/styles.css +++ /dev/null @@ -1,3 +0,0 @@ -@import url('./main-layout.css'); -@import url('./views/hello-world-view.css'); -@import url('line-awesome/dist/line-awesome/css/line-awesome.min.css'); \ No newline at end of file diff --git a/logviewer/frontend/themes/myapp/views/hello-world-view.css b/logviewer/frontend/themes/myapp/views/hello-world-view.css deleted file mode 100644 index c61de2b2ef..0000000000 --- a/logviewer/frontend/themes/myapp/views/hello-world-view.css +++ /dev/null @@ -1,4 +0,0 @@ -.hello-world-view { - display: block; - padding: 1em; -} diff --git a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/Application.java b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/Application.java index 99104def50..13ecfc5302 100644 --- a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/Application.java +++ b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/Application.java @@ -28,7 +28,7 @@ * And the initial Config at {@link Config} */ @SpringBootApplication(exclude = {R2dbcAutoConfiguration.class}) -@Theme(value = "myapp") +@Theme(value = "logviewer") @PWA(name = "LogViewer", shortName = "Logs", offlineResources = {"images/logo.png"}) @NpmPackage(value = "line-awesome", version = "1.3.0") @Push diff --git a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/logs/LogRepository.java b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/logs/LogRepository.java index baea561dad..be2ae85de5 100644 --- a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/logs/LogRepository.java +++ b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/logs/LogRepository.java @@ -2,6 +2,7 @@ import org.togetherjava.tjbot.db.generated.tables.pojos.Logevents; +import java.util.Collection; import java.util.List; public interface LogRepository { @@ -19,4 +20,12 @@ public interface LogRepository { * @return List of LogEvents */ List findAll(); + + /** + * Fetches all Events, which LogLevel matches the given Collection, from the DB + * + * @return List of LogEvents + */ + List findWithLevelMatching(Collection logLevels); + } diff --git a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/logs/LogRepositoryImpl.java b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/logs/LogRepositoryImpl.java index 4e484731f7..69fb486ad1 100644 --- a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/logs/LogRepositoryImpl.java +++ b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/logs/LogRepositoryImpl.java @@ -5,6 +5,7 @@ import org.togetherjava.tjbot.db.generated.tables.pojos.Logevents; import org.togetherjava.tjbot.db.generated.tables.records.LogeventsRecord; +import java.util.Collection; import java.util.List; import static org.togetherjava.tjbot.db.generated.tables.Logevents.LOGEVENTS; @@ -50,6 +51,17 @@ public List findAll() { }); } + + @Override + @SuppressWarnings("java:S1602") // Curly Braces are necessary here + public List findWithLevelMatching(Collection logLevels) { + return this.db.read(ctx -> { + return ctx.selectFrom(LOGEVENTS) + .where(LOGEVENTS.LEVEL.in(logLevels)) + .fetch(this::recordToPojo); + }); + } + private Logevents recordToPojo(final LogeventsRecord logRecord) { return new Logevents(logRecord.getId(), logRecord.getTime(), logRecord.getThread(), logRecord.getLevel(), logRecord.getLoggername(), logRecord.getMessage(), diff --git a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/util/LogUtils.java b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/util/LogUtils.java new file mode 100644 index 0000000000..719fb20223 --- /dev/null +++ b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/util/LogUtils.java @@ -0,0 +1,56 @@ +package org.togetherjava.tjbot.logwatcher.util; + +import org.togetherjava.tjbot.db.generated.tables.pojos.Logevents; + +import java.util.Arrays; +import java.util.EnumSet; +import java.util.Locale; +import java.util.Set; +import java.util.stream.Collectors; + +public final class LogUtils { + + /** + * Loglevel of all Configurable Levels in the logger + */ + public enum LogLevel { + INFO, + WARN, + ERROR, + DEBUG, + TRACE; + + /** + * Collects all LogLevel in a Set + * + * @return A Set containing every Loglevel + */ + public static Set getAll() { + return EnumSet.allOf(LogLevel.class); + } + + /** + * Maps the LogLevel to their name and collects it in a Set + * + * @return A Set containing every LogLevel as String + */ + public static Set getAllNames() { + return Arrays.stream(values()).map(Enum::name).collect(Collectors.toUnmodifiableSet()); + } + } + + /** + * Maps a Logevent to the color-coded css-Class (css class has i.e. red as background-color for + * ERROR events) + * + * @param event Logevent from the DB with a specific Logevent + * @return The name of the CSS class to use with this Logevent. + */ + public static String logLevelToCssClass(final Logevents event) { + return event.getLevel().toLowerCase(Locale.ENGLISH); + } + + + + private LogUtils() {} +} diff --git a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/logs/LogsView.java b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/logs/LogsView.java index 1606b9ae2b..2f87902681 100644 --- a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/logs/LogsView.java +++ b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/logs/LogsView.java @@ -1,9 +1,13 @@ package org.togetherjava.tjbot.logwatcher.views.logs; +import com.vaadin.flow.component.AbstractField; import com.vaadin.flow.component.HasValue; +import com.vaadin.flow.component.checkbox.Checkbox; import com.vaadin.flow.component.combobox.ComboBox; +import com.vaadin.flow.component.html.Paragraph; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; -import com.vaadin.flow.component.textfield.TextArea; +import com.vaadin.flow.component.orderedlayout.Scroller; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.data.provider.DataProvider; import com.vaadin.flow.data.renderer.TextRenderer; import com.vaadin.flow.router.PageTitle; @@ -13,6 +17,7 @@ import org.togetherjava.tjbot.logwatcher.accesscontrol.AllowedRoles; import org.togetherjava.tjbot.logwatcher.accesscontrol.Role; import org.togetherjava.tjbot.logwatcher.util.LogReader; +import org.togetherjava.tjbot.logwatcher.util.LogUtils; import org.togetherjava.tjbot.logwatcher.util.NotificationUtils; import org.togetherjava.tjbot.logwatcher.views.MainLayout; @@ -21,7 +26,11 @@ import java.nio.file.Path; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * The Logs View in the Browser @@ -32,24 +41,53 @@ @RouteAlias(value = "", layout = MainLayout.class) @AllowedRoles(roles = {Role.USER}) @PermitAll -public class LogsView extends HorizontalLayout { +public class LogsView extends VerticalLayout { + + private static final Pattern LOGLEVEL_MATCHER = + Pattern.compile("(%s)".formatted(String.join("|", LogUtils.LogLevel.getAllNames()))); /** * Field where the events are displayed */ - private final TextArea text = new TextArea(); + private final VerticalLayout events = new VerticalLayout(); private final transient LogReader watcher; public LogsView(LogReader watcher) { this.watcher = watcher; - addClassName("hello-world-view"); + this.events.setWidthFull(); + + addClassName("logs-view"); - final ComboBox logs = createComboBox(); + HorizontalLayout options = new HorizontalLayout(); + options.setAlignItems(Alignment.START); + + for (final String level : LogUtils.LogLevel.getAllNames()) { + final Checkbox ch = new Checkbox(level); + ch.setValue(true); + ch.addValueChangeListener(this::onLogLevelCheckbox); + options.add(ch); + } + + ComboBox logs = createComboBox(); logs.getOptionalValue().ifPresent(this::fillTextField); - this.text.setWidthFull(); - add(logs, this.text); + add(logs, options, new Scroller(this.events, Scroller.ScrollDirection.VERTICAL)); + } + + private void onLogLevelCheckbox( + AbstractField.ComponentValueChangeEvent event) { + if (!event.isFromClient()) { + return; + } + + final String level = event.getSource().getLabel().toLowerCase(Locale.ENGLISH); + + this.events.getChildren() + .filter(Paragraph.class::isInstance) + .map(Paragraph.class::cast) + .filter(c -> c.getClassName().equals(level)) + .forEach(c -> c.setVisible(event.getValue())); } /** @@ -66,7 +104,8 @@ private ComboBox createComboBox() { logs.setItems(DataProvider.ofCollection(logFiles)); logFiles.stream().findFirst().ifPresent(logs::setValue); - logs.addValueChangeListener(this::onValueChange); + logs.addValueChangeListener(this::onLogFileCombobox); + return logs; } @@ -75,7 +114,7 @@ private ComboBox createComboBox() { * * @param event Generated Event, containing old and new Value */ - private void onValueChange(HasValue.ValueChangeEvent event) { + private void onLogFileCombobox(HasValue.ValueChangeEvent event) { if (Objects.equals(event.getOldValue(), event.getValue())) { return; } @@ -89,7 +128,23 @@ private void onValueChange(HasValue.ValueChangeEvent event) { * @param logFileName Name of the Logfile */ private void fillTextField(final Path logFileName) { - this.text.setValue(String.join("\n", getLogEntries(logFileName))); + this.events.removeAll(); + + final List logEntries = getLogEntries(logFileName); + + String previousGroup = "unknown"; + for (final String event : logEntries) { + final String trimmed = event.trim(); + final Paragraph text = new Paragraph(trimmed); + + final Matcher matcher = LOGLEVEL_MATCHER.matcher(trimmed); + if (matcher.find()) { + previousGroup = matcher.group().toLowerCase(Locale.ENGLISH); + } + + text.addClassName(previousGroup); + this.events.add(text); + } } /** diff --git a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/logs/StreamedView.java b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/logs/StreamedView.java index 86832424a0..8159478865 100644 --- a/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/logs/StreamedView.java +++ b/logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/logs/StreamedView.java @@ -4,20 +4,23 @@ import com.vaadin.flow.component.*; import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.checkbox.Checkbox; +import com.vaadin.flow.component.dependency.CssImport; import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.orderedlayout.HorizontalLayout; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.data.renderer.LocalDateTimeRenderer; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; import com.vaadin.flow.server.SessionDestroyEvent; import com.vaadin.flow.server.VaadinService; +import org.togetherjava.tjbot.db.generated.tables.pojos.Logevents; import org.togetherjava.tjbot.logwatcher.accesscontrol.AllowedRoles; import org.togetherjava.tjbot.logwatcher.accesscontrol.Role; import org.togetherjava.tjbot.logwatcher.constants.LogEventsConstants; import org.togetherjava.tjbot.logwatcher.logs.LogRepository; +import org.togetherjava.tjbot.logwatcher.util.LogUtils; import org.togetherjava.tjbot.logwatcher.views.MainLayout; import org.togetherjava.tjbot.logwatcher.watcher.StreamWatcher; -import org.togetherjava.tjbot.db.generated.tables.pojos.Logevents; import org.vaadin.crudui.crud.impl.GridCrud; import javax.annotation.security.PermitAll; @@ -33,36 +36,66 @@ @Route(value = "streamed", layout = MainLayout.class) @AllowedRoles(roles = {Role.USER}) @PermitAll -public class StreamedView extends HorizontalLayout { +@CssImport(themeFor = "vaadin-grid", value = "./themes/logviewer/views/logs-view.css") +public class StreamedView extends VerticalLayout { private final GridCrud grid = new GridCrud<>(Logevents.class); private final UUID uuid = UUID.randomUUID(); + private final Set enabledLogLevel = new HashSet<>(LogUtils.LogLevel.getAllNames()); public StreamedView(LogRepository logs) { - addClassName("hello-world-view"); + addClassName("logs-view"); - add(new Button("Change Columns", this::onChangeColumns), this.grid); + final HorizontalLayout buttonPanel = + new HorizontalLayout(new Button("Change Columns", this::onChangeColumns)); + + for (final String level : this.enabledLogLevel) { + final Checkbox ch = new Checkbox(level); + ch.setValue(true); + ch.addValueChangeListener(this::onLogLevelCheckbox); + buttonPanel.add(ch); + } - this.grid.setOperations(logs::findAll, null, null, null); + add(buttonPanel, this.grid); + + this.grid.setOperations(() -> logs.findWithLevelMatching(this.enabledLogLevel), null, null, + null); this.grid.setAddOperationVisible(false); this.grid.setDeleteOperationVisible(false); this.grid.setUpdateOperationVisible(false); - this.grid.getGrid() - .setColumns(LogEventsConstants.FIELD_INSTANT, LogEventsConstants.FIELD_LOGGER_NAME, - LogEventsConstants.FIELD_MESSAGE); + final Grid baseGrid = this.grid.getGrid(); + baseGrid.setColumns(LogEventsConstants.FIELD_INSTANT, LogEventsConstants.FIELD_LOGGER_NAME, + LogEventsConstants.FIELD_MESSAGE); setInstantFormatter(); - this.grid.getGrid().getColumns().forEach(c -> c.setAutoWidth(true)); - this.grid.getGrid().getColumns().forEach(c -> c.setResizable(true)); - this.grid.getGrid().recalculateColumnWidths(); - this.grid.getGrid().setColumnReorderingAllowed(true); + baseGrid.getColumns().forEach(c -> c.setAutoWidth(true)); + baseGrid.getColumns().forEach(c -> c.setResizable(true)); + baseGrid.setClassNameGenerator(LogUtils::logLevelToCssClass); + baseGrid.recalculateColumnWidths(); + baseGrid.setColumnReorderingAllowed(true); VaadinService.getCurrent().addSessionDestroyListener(this::onDestroy); final UI ui = UI.getCurrent(); StreamWatcher.addSubscription(this.uuid, () -> ui.access(this.grid::refreshGrid)); } + private void onLogLevelCheckbox( + AbstractField.ComponentValueChangeEvent event) { + if (!event.isFromClient()) { + return; + } + + final String logLevel = event.getSource().getLabel(); + + final boolean isChecked = event.getValue();// don't inline this, else SonarLint is crying + if (isChecked) { + this.enabledLogLevel.add(logLevel); + } else { + this.enabledLogLevel.remove(logLevel); + } + this.grid.refreshGrid(); + } private void onDestroy(SessionDestroyEvent event) { removeHook(); diff --git a/logviewer/src/main/resources/log4j2.xml b/logviewer/src/main/resources/log4j2.xml index 4659cb8b17..3f0f6b7f9e 100644 --- a/logviewer/src/main/resources/log4j2.xml +++ b/logviewer/src/main/resources/log4j2.xml @@ -19,5 +19,7 @@ + +