Skip to content

Commit 1c87616

Browse files
committed
Migrate forwarding of Logs to UI and Backend using Pax logging
1 parent eee2c7d commit 1c87616

File tree

12 files changed

+374
-385
lines changed

12 files changed

+374
-385
lines changed

edge/src/io/openems/core/utilities/websocket/WebsocketLogAppender.java

-39
This file was deleted.

io.openems.common/src/io/openems/common/websocket/DefaultMessages.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.openems.common.websocket;
22

3-
import java.util.Map.Entry;
43
import java.util.Optional;
54
import java.util.UUID;
65

@@ -9,10 +8,6 @@
98

109
import io.openems.common.exceptions.OpenemsException;
1110
import io.openems.common.session.Role;
12-
import io.openems.common.types.ChannelAddress;
13-
import io.openems.common.types.FieldValue;
14-
import io.openems.common.types.NumberFieldValue;
15-
import io.openems.common.types.StringFieldValue;
1611
import io.openems.common.utils.JsonUtils;
1712

1813
public class DefaultMessages {
@@ -277,7 +272,7 @@ public static JsonObject currentDataSubscribe(JsonObject jMessageId, JsonObject
277272
* {
278273
* messageId: {},
279274
* log: {
280-
* times: number,
275+
* time: number,
281276
* level: string,
282277
* source: string,
283278
* message: string

io.openems.edge.application/src/io/openems/edge/application/EdgeApp.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void activate() {
2828
try {
2929
config = cm.getConfiguration("org.ops4j.pax.logging", null);
3030
Hashtable<String, Object> log4j = new Hashtable<>();
31-
log4j.put("log4j.rootLogger", "INFO, CONSOLE");
31+
log4j.put("log4j.rootLogger", "INFO, CONSOLE, osgi:*");
3232
log4j.put("log4j.appender.CONSOLE", "org.apache.log4j.ConsoleAppender");
3333
log4j.put("log4j.appender.CONSOLE.layout", "org.apache.log4j.PatternLayout");
3434
log4j.put("log4j.appender.CONSOLE.layout.ConversionPattern", "%d{ISO8601} [%-8.8t] %-5p [%-30.30c] %m%n");

io.openems.edge.common/src/io/openems/edge/common/component/AbstractOpenemsComponent.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public Collection<Channel<?>> channels() {
113113
* @param log
114114
* @param message
115115
*/
116-
protected final void logInfo(Logger log, String message) {
116+
protected void logInfo(Logger log, String message) {
117117
log.info("[" + this.id() + "] " + message);
118118
}
119119

@@ -123,7 +123,7 @@ protected final void logInfo(Logger log, String message) {
123123
* @param log
124124
* @param message
125125
*/
126-
protected final void logWarn(Logger log, String message) {
126+
protected void logWarn(Logger log, String message) {
127127
log.warn("[" + this.id() + "] " + message);
128128
}
129129

@@ -133,7 +133,7 @@ protected final void logWarn(Logger log, String message) {
133133
* @param log
134134
* @param message
135135
*/
136-
protected final void logError(Logger log, String message) {
136+
protected void logError(Logger log, String message) {
137137
log.error("[" + this.id() + "] " + message);
138138
}
139139
}

io.openems.edge.controller.api.backend/bnd.bnd

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Private-Package: \
1919
com.google.gson,\
2020
com.google.guava,\
2121
io.openems.edge.timedata.api;version=latest,\
22-
io.openems.common;version=latest
22+
io.openems.common;version=latest,\
23+
org.ops4j.pax.logging.pax-logging-api
2324

2425
-testpath: \
2526
osgi.enroute.junit.wrapper;version=4.12, \

io.openems.edge.controller.api.backend/src/io/openems/edge/controller/api/backend/BackendApi.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import java.util.Optional;
1111
import java.util.concurrent.CopyOnWriteArrayList;
1212

13+
import org.ops4j.pax.logging.spi.PaxAppender;
14+
import org.ops4j.pax.logging.spi.PaxLoggingEvent;
1315
import org.osgi.service.cm.ConfigurationAdmin;
1416
import org.osgi.service.component.ComponentContext;
1517
import org.osgi.service.component.annotations.Activate;
@@ -32,8 +34,12 @@
3234
import io.openems.edge.timedata.api.Timedata;
3335

3436
@Designate(ocd = Config.class, factory = true)
35-
@Component(name = "Controller.Api.Backend", immediate = true, configurationPolicy = ConfigurationPolicy.REQUIRE)
36-
public class BackendApi extends AbstractOpenemsComponent implements Controller, ApiController, OpenemsComponent {
37+
@Component(name = "Controller.Api.Backend", //
38+
immediate = true, //
39+
configurationPolicy = ConfigurationPolicy.REQUIRE, //
40+
property = "org.ops4j.pax.logging.appender.name=Controller.Api.Backend")
41+
public class BackendApi extends AbstractOpenemsComponent
42+
implements Controller, ApiController, OpenemsComponent, PaxAppender {
3743

3844
protected final static int DEFAULT_CYCLE_TIME = 10000;
3945

@@ -139,4 +145,8 @@ public ConfigurationAdmin getConfigurationAdmin() {
139145
return this.configAdmin;
140146
}
141147

148+
@Override
149+
public void doAppend(PaxLoggingEvent event) {
150+
this.websocket.sendLog(event);
151+
}
142152
}

io.openems.edge.controller.api.backend/src/io/openems/edge/controller/api/backend/MyWebSocketClient.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.java_websocket.drafts.Draft;
1515
import org.java_websocket.drafts.Draft_6455;
1616
import org.java_websocket.handshake.ServerHandshake;
17+
import org.ops4j.pax.logging.spi.PaxLoggingEvent;
1718
import org.slf4j.Logger;
1819
import org.slf4j.LoggerFactory;
1920

@@ -34,7 +35,7 @@
3435
class MyWebSocketClient extends WebSocketClient {
3536

3637
private final static Draft WEBSOCKET_DRAFT = new Draft_6455();
37-
private final static int DEFAULT_WAIT_AFTER_CLOSE = 1; // 1 second
38+
private final static int DEFAULT_WAIT_AFTER_CLOSE = 5; // 1 second
3839
private final static int MAX_WAIT_AFTER_CLOSE = 60 * 3; // 3 minutes
3940

4041
private final Logger log = LoggerFactory.getLogger(MyWebSocketClient.class);
@@ -100,14 +101,18 @@ public void onError(Exception ex) {
100101
}
101102

102103
protected void deactivate() {
103-
this.handler.dispose();
104-
this.reconnectExecutor.shutdown();
105104
this.close(2000, "Disabled Backend Api Controller");
105+
this.handler.dispose();
106+
this.reconnectExecutor.shutdownNow();
107+
try {
108+
this.reconnectExecutor.awaitTermination(5, TimeUnit.SECONDS);
109+
} catch (InterruptedException e) {
110+
this.log.error("Unable to shutdown: " + e.getMessage());
111+
}
106112
}
107113

108-
// TODO implmeent log
109-
protected void sendLog(long timestamp, String level, String source, String message) {
110-
this.handler.sendLog(timestamp, level, source, message);
114+
protected void sendLog(PaxLoggingEvent event) {
115+
this.handler.sendLog(event);
111116
}
112117

113118
/**

io.openems.edge.controller.api.websocket/bnd.bnd

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ Private-Package: \
1818
io.openems.wrapper.websocket;version=latest,\
1919
com.google.gson,\
2020
io.openems.edge.timedata.api;version=latest,\
21-
io.openems.edge.api;version=latest
21+
io.openems.edge.api;version=latest,\
22+
org.ops4j.pax.logging.pax-logging-api
2223

2324
-testpath: \
2425
osgi.enroute.junit.wrapper;version=4.12, \

0 commit comments

Comments
 (0)