Skip to content

Commit aa68787

Browse files
Loggers are used consistently from fields now
instead of being created on the fly
1 parent aabdb29 commit aa68787

File tree

6 files changed

+25
-16
lines changed

6 files changed

+25
-16
lines changed

logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/Application.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.vaadin.flow.component.page.Push;
66
import com.vaadin.flow.server.PWA;
77
import com.vaadin.flow.theme.Theme;
8+
import org.slf4j.Logger;
89
import org.slf4j.LoggerFactory;
910
import org.springframework.boot.SpringApplication;
1011
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -34,9 +35,11 @@
3435
@Push
3536
public class Application extends SpringBootServletInitializer implements AppShellConfigurator {
3637

38+
private static final Logger applicationLogger = LoggerFactory.getLogger(Application.class);
39+
3740
public static void main(String[] args) {
3841
if (args.length > 1) {
39-
LoggerFactory.getLogger(Application.class)
42+
applicationLogger
4043
.error("Usage: Provide a single Argument, containing the Path to the Config-File");
4144
System.exit(1);
4245
}

logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/DatabaseProvider.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.togetherjava.tjbot.logwatcher;
22

3+
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
45
import org.springframework.beans.FatalBeanException;
56
import org.springframework.beans.factory.config.BeanDefinition;
@@ -21,6 +22,8 @@ public class DatabaseProvider {
2122
private final Database db;
2223
private final Config config;
2324

25+
private static final Logger logger = LoggerFactory.getLogger(DatabaseProvider.class);
26+
2427
public DatabaseProvider(final Config config) {
2528
this.config = config;
2629
this.db = createDB();
@@ -34,8 +37,7 @@ private Database createDB() {
3437
try {
3538
return new Database("jdbc:sqlite:%s".formatted(dbPath.toAbsolutePath()));
3639
} catch (final SQLException e) {
37-
LoggerFactory.getLogger(DatabaseProvider.class)
38-
.error("Exception while creating Database.", e);
40+
logger.error("Exception while creating Database.", e);
3941
throw new FatalBeanException("Could not create Database.", e);
4042
}
4143
}
@@ -46,8 +48,7 @@ private Path getDBPath() {
4648
try {
4749
Files.createDirectories(dbPath.getParent());
4850
} catch (final IOException e) {
49-
LoggerFactory.getLogger(DatabaseProvider.class)
50-
.error("Exception while creating Database-Path.", e);
51+
logger.error("Exception while creating Database-Path.", e);
5152
}
5253

5354
return dbPath;

logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/MainLayout.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.vaadin.flow.component.html.*;
1111
import com.vaadin.flow.router.PageTitle;
1212
import com.vaadin.flow.router.RouterLink;
13+
import org.slf4j.Logger;
1314
import org.slf4j.LoggerFactory;
1415
import org.togetherjava.tjbot.logwatcher.accesscontrol.AllowedRoles;
1516
import org.togetherjava.tjbot.logwatcher.accesscontrol.Role;
@@ -33,6 +34,7 @@ public class MainLayout extends AppLayout {
3334

3435
private final transient AuthenticatedUser authenticatedUser;
3536
private H1 viewTitle;
37+
private static final Logger logger = LoggerFactory.getLogger(MainLayout.class);
3638

3739
public MainLayout(AuthenticatedUser authUser) {
3840
this.authenticatedUser = authUser;
@@ -124,8 +126,7 @@ private boolean checkAccess(MenuItemInfo menuItemInfo) {
124126
final AllowedRoles annotation = view.getAnnotation(AllowedRoles.class);
125127

126128
if (annotation == null) {
127-
LoggerFactory.getLogger(MainLayout.class)
128-
.warn("Class {} not properly secured with Annotation", view);
129+
logger.warn("Class {} not properly secured with Annotation", view);
129130
return false;
130131
}
131132

logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/logs/LogsView.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.vaadin.flow.router.PageTitle;
1414
import com.vaadin.flow.router.Route;
1515
import com.vaadin.flow.router.RouteAlias;
16+
import org.slf4j.Logger;
1617
import org.slf4j.LoggerFactory;
1718
import org.togetherjava.tjbot.logwatcher.accesscontrol.AllowedRoles;
1819
import org.togetherjava.tjbot.logwatcher.accesscontrol.Role;
@@ -43,6 +44,8 @@
4344
@PermitAll
4445
public class LogsView extends VerticalLayout {
4546

47+
private static final Logger logger = LoggerFactory.getLogger(LogsView.class);
48+
4649
private static final Pattern LOGLEVEL_MATCHER =
4750
Pattern.compile("(%s)".formatted(String.join("|", LogUtils.LogLevel.getAllNames())));
4851

@@ -156,7 +159,7 @@ private List<Path> getLogFiles() {
156159
try {
157160
return this.watcher.getLogs();
158161
} catch (final UncheckedIOException e) {
159-
LoggerFactory.getLogger(LogsView.class).error("Exception while gathering LogFiles", e);
162+
logger.error("Exception while gathering LogFiles", e);
160163
NotificationUtils.getNotificationForError(e).open();
161164
return Collections.emptyList();
162165
}
@@ -172,7 +175,7 @@ private List<String> getLogEntries(final Path logFile) {
172175
try {
173176
return this.watcher.readLog(logFile);
174177
} catch (final UncheckedIOException e) {
175-
LoggerFactory.getLogger(LogsView.class).error("Exception while gathering LogFiles", e);
178+
logger.error("Exception while gathering LogFiles", e);
176179
NotificationUtils.getNotificationForError(e).open();
177180
return Collections.emptyList();
178181
}

logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/views/usermanagement/UserManagement.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import com.vaadin.flow.data.provider.Query;
1616
import com.vaadin.flow.router.PageTitle;
1717
import com.vaadin.flow.router.Route;
18+
import org.slf4j.Logger;
1819
import org.slf4j.LoggerFactory;
1920
import org.togetherjava.tjbot.logwatcher.accesscontrol.AllowedRoles;
2021
import org.togetherjava.tjbot.logwatcher.accesscontrol.Role;
@@ -42,6 +43,7 @@ public class UserManagement extends VerticalLayout {
4243

4344
private final transient UserRepository repo;
4445
private final Grid<UserWrapper> grid = new Grid<>(UserWrapper.class, false);
46+
private static final Logger logger = LoggerFactory.getLogger(UserManagement.class);
4547

4648
public UserManagement(UserRepository repository) {
4749
this.repo = repository;
@@ -81,8 +83,7 @@ private Stream<UserWrapper> onAll(Query<UserWrapper, Void> query) {
8183
.map(user -> new UserWrapper(user.getDiscordid(), user.getUsername(),
8284
this.repo.fetchRolesForUser(user)));
8385
} catch (final DatabaseException e) {
84-
LoggerFactory.getLogger(UserManagement.class)
85-
.error("Exception occurred while fetching.", e);
86+
logger.error("Exception occurred while fetching.", e);
8687
NotificationUtils.getNotificationForError(e).open();
8788
return Stream.empty();
8889
}
@@ -133,8 +134,7 @@ private void doUpdate(UserWrapper user) {
133134

134135
UserManagement.this.repo.saveRolesForUser(toSave, user.getRoles());
135136
} catch (DatabaseException e) {
136-
LoggerFactory.getLogger(UserManagement.class)
137-
.error("Exception occurred while saving.", e);
137+
logger.error("Exception occurred while saving.", e);
138138
NotificationUtils.getNotificationForError(e).open();
139139
}
140140

@@ -193,8 +193,7 @@ private void doRemove(UserWrapper user) {
193193
try {
194194
UserManagement.this.repo.delete(new Users(user.getDiscordID(), user.getUserName()));
195195
} catch (DatabaseException e) {
196-
LoggerFactory.getLogger(UserManagement.class)
197-
.error("Exception occurred while removing.", e);
196+
logger.error("Exception occurred while removing.", e);
198197
NotificationUtils.getNotificationForError(e).open();
199198
}
200199
}

logviewer/src/main/java/org/togetherjava/tjbot/logwatcher/watcher/StreamWatcher.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.togetherjava.tjbot.logwatcher.watcher;
22

3+
import org.slf4j.Logger;
34
import org.slf4j.LoggerFactory;
45

56
import java.util.Map;
@@ -10,6 +11,7 @@ public class StreamWatcher {
1011
private static final int EXPECTED_CONCURRENT_LOG_WATCHERS = 3;
1112
private static final Map<UUID, Runnable> consumerMap =
1213
new ConcurrentHashMap<>(EXPECTED_CONCURRENT_LOG_WATCHERS);
14+
private static final Logger logger = LoggerFactory.getLogger(StreamWatcher.class);
1315

1416
private StreamWatcher() {}
1517

@@ -48,7 +50,7 @@ private static void notifySubscriber(Runnable run) {
4850
try {
4951
run.run();
5052
} catch (final Exception e) {
51-
LoggerFactory.getLogger(StreamWatcher.class).error("Runnable threw Exception.", e);
53+
logger.error("Runnable threw Exception.", e);
5254
}
5355
}
5456
}

0 commit comments

Comments
 (0)